Mr. Bless
2008-Dec-13 02:37 UTC
Need Help in converting php encryption decryption code to ruby on rails
Hi guys i found this encryption decryption in php and try to convert it in rails but i am unable to successfully convert it. So plz help me. I you write the whole conversion code then it will be great. PHP code is like this ===============# #/********************************************** #** #** MD5 block cypher #** #** Author..: leapinglangoor [ leapinglangoor-/E1597aS9LQxFYw1CcD5bw@public.gmane.org ] #** Date....: 30th Apr 2005 #** version.: v1.00 #** #** Desc....: Below is MD5-based block cypher ( MDC-like ), #** which works in 128bit CFB mode. It is very useful to #** encrypt secret data before transfer it over the network. #** #** $iv_len - initialization vector''s length. #** 0 <= $iv_len <= 512 #** #************************************************/ # function get_rnd_iv( $iv_len ) { $iv = ''''; while ( $iv_len-- > 0 ) { $iv .= chr( mt_rand( ) & 0xff ); } return $iv; } function md5_encrypt( $plain_text, $password, $iv_len = 16 ) { $plain_text .= "\x13"; $n = strlen( $plain_text ); if ( $n % 16 ) { $plain_text .= str_repeat( "\0", 16 - ( $n % 16 ) ); } $i = 0; $enc_text = get_rnd_iv( $iv_len ); $iv = substr( $password ^ $enc_text, 0, 512 ); while ( $i < $n ) { $block = substr( $plain_text, $i, 16 ) ^ pack( ''H*'', md5 ( $iv ) ); $enc_text .= $block; $iv = substr( $block . $iv, 0, 512 ) ^ $password; $i += 16; } return base64_encode( $enc_text ); } function md5_decrypt( $enc_text, $password, $iv_len = 16 ) { $enc_text = base64_decode( $enc_text ); $n = strlen( $enc_text ); $i = $iv_len; $plain_text = ''''; $iv = substr( $password ^ substr( $enc_text, 0, $iv_len ), 0, 512 ); while ( $i < $n ) { $block = substr( $enc_text, $i, 16 ); $plain_text .= $block ^ pack( ''H*'', md5( $iv ) ); $iv = substr( $block . $iv, 0, 512 ) ^ $password; $i += 16; } return preg_replace( ''/\\x13\\x00*$/'', '''', $plain_text ); } and I Tried like this ==============def get_rnd_iv(iv_len) iv='''' while (iv_len>0) do iv_len -=1 iv +=rand(255).chr end return iv end def md5_encrypt(plain_text,password,iv_len=16) plain_text +="\x13" n=plain_text.size if(n%16) plain_text +="\0"*(16-(n%16)) end i=0 enc_text=get_rnd_iv(iv_len) enc='''' pass='''' for i in 0..password.size-1 pass +=password[i].to_s(2) end for i in 0..enc_text.size-1 enc +=enc_text[i].to_s(2) end intervalue=(pass.to_i^enc.to_i).to_s iv=password^enc_text.split(0,512) iv=intervalue[0,512] while ( $i < $n ) { $block = substr( $plain_text, $i, 16 ) ^ pack( ''H*'', md5 ( $iv ) ); $enc_text .= $block; $iv = substr( $block . $iv, 0, 512 ) ^ $password; $i += 16; } while(i<n) do pp_text=plain_text[i,16] pp='''' for j in 0..pp_text.size-1 pp+=pp_text.to_s(2) end block=pp^pack(''H*'',Digest::MD5.hexdigest(iv)) enc_text +=block intermedi=block+iv kk='''' for j in 0..intermedi.size-1 kk +=intermedi[j].to_s(2) end iv=kk.to_i^pass.to_i i +=16 end return Base64.encode64(enc_text) end # def md5_decrypt(enc_text,password,iv_len=16) enc_text=Base64.decode64( enc_text) n=enc_text.size i=iv_len plain_text='''' pass='''' for ii in 0..password.size-1 pass +=password[ii].to_s(2) end lmp=enc_text[0,iv_len] enc='''' for k in 0..lmp.size-1 enc=lmp[k].to_s(2) end intermedi=(pass.to_i ^lmp.to_i) .to_s iv=intermedi[0,512] while(i<n) do block = enc_text[ i, 16 ] plain_text += block ^ pack( ''H*'', Digest::MD5.hexdigest ( iv ) ) # plain_text += block cc=block+iv iv = cc[ 0, 512].to_i ^ pass.to_i i += 16 end return preg_replace( ''/\\x13\\x00*$/'', '''', $plain_text ); end --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Andrius Chamentauskas
2008-Dec-13 13:52 UTC
Re: Need Help in converting php encryption decryption code to ruby on rails
Even though i think you should use some known encryption algorythm, here''s translated source (not tested): require ''digest/md5'' require ''base64'' class String def ^(value) rez = "" self.length.times { |i| rez << (self[i] ^ value[i % value.length].to_i) } rez end end def get_rnd_iv(iv_len) chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a iv = "" iv_len.times { iv << chars[rand(chars.size)] } return iv end def md5_encrypt(plain_text, password, iv_len = 16 ) plain_text += "\x13"; # for some reason i believe that it should be \n instead of \x13, a bug in a script? plain_text += "\0" * (16 - plain_text.length % 16) enc_text = get_rnd_iv(iv_len); iv = (password ^ enc_text)[0, 512]; (plain_text.length % 16).times do |i| block = plain_text[i * 16, 16] ^ Digest::MD5.hexdigest(iv) enc_text += block iv = (block + iv)[0, 512] ^ password end return Base64.encode64(enc_text); end def md5_decrypt(enc_text, password, iv_len = 16) enc_text = Base64.decode64(enc_text); plain_text = ''''; iv = (password ^ enc_text[0, iv_len])[0, 512]; i, n = iv.length, enc_text.length while i < n block = enc_text[i, 16] plain_text += block ^ Digest::MD5.hexdigest(iv) iv = (block + iv)[0, 512] ^ password i += 16 end return plain_text.gsub(/\x13\x00*$/, ''''); end On Dec 13, 4:37 am, "Mr. Bless" <rananirv...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi guys i found this encryption decryption in php and try to convert > it in rails but i am unable to successfully convert it. So plz help > me. I you write the whole conversion code then it will be great. > > PHP code is like this > ===============> # > #/********************************************** > #** > #** MD5 block cypher > #** > #** Author..: leapinglangoor [ leapinglang...-/E1597aS9LQxFYw1CcD5bw@public.gmane.org ] > #** Date....: 30th Apr 2005 > #** version.: v1.00 > #** > #** Desc....: Below is MD5-based block cypher ( MDC-like ), > #** which works in 128bit CFB mode. It is very useful to > #** encrypt secret data before transfer it over the network. > #** > #** $iv_len - initialization vector''s length. > #** 0 <= $iv_len <= 512 > #** > #************************************************/ > # > function get_rnd_iv( $iv_len ) > { > > $iv = ''''; > while ( $iv_len-- > 0 ) > { > $iv .= chr( mt_rand( ) & 0xff ); > } > > return $iv; > > } > > function md5_encrypt( $plain_text, $password, $iv_len = 16 ) > { > > $plain_text .= "\x13"; > $n = strlen( $plain_text ); > if ( $n % 16 ) > { > $plain_text .= str_repeat( "\0", 16 - ( $n % 16 ) ); > } > > $i = 0; > $enc_text = get_rnd_iv( $iv_len ); > $iv = substr( $password ^ $enc_text, 0, 512 ); > while ( $i < $n ) > { > $block = substr( $plain_text, $i, 16 ) ^ pack( ''H*'', md5 > ( $iv ) ); > $enc_text .= $block; > $iv = substr( $block . $iv, 0, 512 ) ^ $password; > $i += 16; > } > > return base64_encode( $enc_text ); > > } > > function md5_decrypt( $enc_text, $password, $iv_len = 16 ) > { > > $enc_text = base64_decode( $enc_text ); > $n = strlen( $enc_text ); > $i = $iv_len; > $plain_text = ''''; > $iv = substr( $password ^ substr( $enc_text, 0, $iv_len ), 0, > 512 ); > while ( $i < $n ) > { > $block = substr( $enc_text, $i, 16 ); > $plain_text .= $block ^ pack( ''H*'', md5( $iv ) ); > $iv = substr( $block . $iv, 0, 512 ) ^ $password; > $i += 16; > } > > return preg_replace( ''/\\x13\\x00*$/'', '''', $plain_text ); > > } > > and I Tried like this > ==============> def get_rnd_iv(iv_len) > iv='''' > while (iv_len>0) do > iv_len -=1 > iv +=rand(255).chr > end > return iv > end > > def md5_encrypt(plain_text,password,iv_len=16) > plain_text +="\x13" > n=plain_text.size > if(n%16) > plain_text +="\0"*(16-(n%16)) > end > i=0 > enc_text=get_rnd_iv(iv_len) > enc='''' > pass='''' > for i in 0..password.size-1 > pass +=password[i].to_s(2) > end > for i in 0..enc_text.size-1 > enc +=enc_text[i].to_s(2) > end > intervalue=(pass.to_i^enc.to_i).to_s > iv=password^enc_text.split(0,512) > iv=intervalue[0,512] > while ( $i < $n ) > { > $block = substr( $plain_text, $i, 16 ) ^ pack( ''H*'', md5 > ( $iv ) ); > $enc_text .= $block; > $iv = substr( $block . $iv, 0, 512 ) ^ $password; > $i += 16; > } > while(i<n) do > pp_text=plain_text[i,16] > pp='''' > for j in 0..pp_text.size-1 > pp+=pp_text.to_s(2) > end > block=pp^pack(''H*'',Digest::MD5.hexdigest(iv)) > enc_text +=block > intermedi=block+iv > kk='''' > for j in 0..intermedi.size-1 > kk +=intermedi[j].to_s(2) > end > iv=kk.to_i^pass.to_i > i +=16 > end > return Base64.encode64(enc_text) > end > # > > def md5_decrypt(enc_text,password,iv_len=16) > enc_text=Base64.decode64( enc_text) > > n=enc_text.size > i=iv_len > plain_text='''' > pass='''' > for ii in 0..password.size-1 > pass +=password[ii].to_s(2) > end > lmp=enc_text[0,iv_len] > enc='''' > for k in 0..lmp.size-1 > enc=lmp[k].to_s(2) > end > > intermedi=(pass.to_i ^lmp.to_i) .to_s > iv=intermedi[0,512] > while(i<n) do > > block = enc_text[ i, 16 ] > plain_text += block ^ pack( ''H*'', Digest::MD5.hexdigest > ( iv ) ) > # plain_text += block > cc=block+iv > iv = cc[ 0, 512].to_i ^ pass.to_i > i += 16 > end > > return preg_replace( ''/\\x13\\x00*$/'', '''', $plain_text ); > end--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
mrbless
2008-Dec-15 02:49 UTC
Re: Need Help in converting php encryption decryption code to ruby on rails
I will try it and let you know. Thanks for support. On Dec 13, 8:52 am, Andrius Chamentauskas <sinsil...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Even though i think you should use some known encryption algorythm, > here''s translated source (not tested): > > require ''digest/md5'' > require ''base64'' > > class String > def ^(value) > rez = "" > self.length.times { |i| rez << (self[i] ^ value[i % > value.length].to_i) } > rez > end > end > > def get_rnd_iv(iv_len) > chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a > iv = "" > iv_len.times { iv << chars[rand(chars.size)] } > return iv > end > > def md5_encrypt(plain_text, password, iv_len = 16 ) > plain_text += "\x13"; # for some reason i believe that it should be > \n instead of \x13, a bug in a script? > plain_text += "\0" * (16 - plain_text.length % 16) > > enc_text = get_rnd_iv(iv_len); > iv = (password ^ enc_text)[0, 512]; > > (plain_text.length % 16).times do |i| > block = plain_text[i * 16, 16] ^ Digest::MD5.hexdigest(iv) > enc_text += block > iv = (block + iv)[0, 512] ^ password > end > > return Base64.encode64(enc_text); > end > > def md5_decrypt(enc_text, password, iv_len = 16) > enc_text = Base64.decode64(enc_text); > plain_text = ''''; > iv = (password ^ enc_text[0, iv_len])[0, 512]; > > i, n = iv.length, enc_text.length > while i < n > block = enc_text[i, 16] > plain_text += block ^ Digest::MD5.hexdigest(iv) > iv = (block + iv)[0, 512] ^ password > i += 16 > end > > return plain_text.gsub(/\x13\x00*$/, ''''); > end > > On Dec 13, 4:37 am, "Mr. Bless" <rananirv...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi guys i found this encryption decryption in php and try to convert > > it in rails but i am unable to successfully convert it. So plz help > > me. I you write the whole conversion code then it will be great. > > > PHP code is like this > > ===============> > # > > #/********************************************** > > #** > > #** MD5 block cypher > > #** > > #** Author..: leapinglangoor [ leapinglang...-/E1597aS9LQxFYw1CcD5bw@public.gmane.org ] > > #** Date....: 30th Apr 2005 > > #** version.: v1.00 > > #** > > #** Desc....: Below is MD5-based block cypher ( MDC-like ), > > #** which works in 128bit CFB mode. It is very useful to > > #** encrypt secret data before transfer it over the network. > > #** > > #** $iv_len - initialization vector''s length. > > #** 0 <= $iv_len <= 512 > > #** > > #************************************************/ > > # > > function get_rnd_iv( $iv_len ) > > { > > > $iv = ''''; > > while ( $iv_len-- > 0 ) > > { > > $iv .= chr( mt_rand( ) & 0xff ); > > } > > > return $iv; > > > } > > > function md5_encrypt( $plain_text, $password, $iv_len = 16 ) > > { > > > $plain_text .= "\x13"; > > $n = strlen( $plain_text ); > > if ( $n % 16 ) > > { > > $plain_text .= str_repeat( "\0", 16 - ( $n % 16 ) ); > > } > > > $i = 0; > > $enc_text = get_rnd_iv( $iv_len ); > > $iv = substr( $password ^ $enc_text, 0, 512 ); > > while ( $i < $n ) > > { > > $block = substr( $plain_text, $i, 16 ) ^ pack( ''H*'', md5 > > ( $iv ) ); > > $enc_text .= $block; > > $iv = substr( $block . $iv, 0, 512 ) ^ $password; > > $i += 16; > > } > > > return base64_encode( $enc_text ); > > > } > > > function md5_decrypt( $enc_text, $password, $iv_len = 16 ) > > { > > > $enc_text = base64_decode( $enc_text ); > > $n = strlen( $enc_text ); > > $i = $iv_len; > > $plain_text = ''''; > > $iv = substr( $password ^ substr( $enc_text, 0, $iv_len ), 0, > > 512 ); > > while ( $i < $n ) > > { > > $block = substr( $enc_text, $i, 16 ); > > $plain_text .= $block ^ pack( ''H*'', md5( $iv ) ); > > $iv = substr( $block . $iv, 0, 512 ) ^ $password; > > $i += 16; > > } > > > return preg_replace( ''/\\x13\\x00*$/'', '''', $plain_text ); > > > } > > > and I Tried like this > > ==============> > def get_rnd_iv(iv_len) > > iv='''' > > while (iv_len>0) do > > iv_len -=1 > > iv +=rand(255).chr > > end > > return iv > > end > > > def md5_encrypt(plain_text,password,iv_len=16) > > plain_text +="\x13" > > n=plain_text.size > > if(n%16) > > plain_text +="\0"*(16-(n%16)) > > end > > i=0 > > enc_text=get_rnd_iv(iv_len) > > enc='''' > > pass='''' > > for i in 0..password.size-1 > > pass +=password[i].to_s(2) > > end > > for i in 0..enc_text.size-1 > > enc +=enc_text[i].to_s(2) > > end > > intervalue=(pass.to_i^enc.to_i).to_s > > iv=password^enc_text.split(0,512) > > iv=intervalue[0,512] > > while ( $i < $n ) > > { > > $block = substr( $plain_text, $i, 16 ) ^ pack( ''H*'', md5 > > ( $iv ) ); > > $enc_text .= $block; > > $iv = substr( $block . $iv, 0, 512 ) ^ $password; > > $i += 16; > > } > > while(i<n) do > > pp_text=plain_text[i,16] > > pp='''' > > for j in 0..pp_text.size-1 > > pp+=pp_text.to_s(2) > > end > > block=pp^pack(''H*'',Digest::MD5.hexdigest(iv)) > > enc_text +=block > > intermedi=block+iv > > kk='''' > > for j in 0..intermedi.size-1 > > kk +=intermedi[j].to_s(2) > > end > > iv=kk.to_i^pass.to_i > > i +=16 > > end > > return Base64.encode64(enc_text) > > end > > # > > > def md5_decrypt(enc_text,password,iv_len=16) > > enc_text=Base64.decode64( enc_text) > > > n=enc_text.size > > i=iv_len > > plain_text='''' > > pass='''' > > for ii in 0..password.size-1 > > pass +=password[ii].to_s(2) > > end > > lmp=enc_text[0,iv_len] > > enc='''' > > for k in 0..lmp.size-1 > > enc=lmp[k].to_s(2) > > end > > > intermedi=(pass.to_i ^lmp.to_i) .to_s > > iv=intermedi[0,512] > > while(i<n) do > > > block = enc_text[ i, 16 ] > > plain_text += block ^ pack( ''H*'', Digest::MD5.hexdigest > > ( iv ) ) > > # plain_text += block > > cc=block+iv > > iv = cc[ 0, 512].to_i ^ pass.to_i > > i += 16 > > end > > > return preg_replace( ''/\\x13\\x00*$/'', '''', $plain_text ); > > end--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Maybe Matching Threads
- Calling a method directly from a test => no method error
- How to get the value for casting in a bitcast instruction more efficiently?
- How to get the value for casting in a bitcast instruction more efficiently?
- How to get the value for casting in a bitcast instruction more efficiently?
- How to get the value for casting in a bitcast instruction more efficiently?