本文介绍了加密密码 - 尝试以与Perl相同的方式使用PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在PERL中: $在Perl中,我已经在Perl中编写了加密功能, b $ b #!/ usr / bin / perl 使用strict; 使用警告; 使用Crypt :: CBC; 使用Crypt :: Rijndael; 我的$ cryptkey ='_PRIVATE_'; 我的$ cipher = Crypt :: CBC-> new(-key => $ cryptkey, -salt => 1, -cipher => Rijndael',); 我的$ data =hello; 我的$ ciphertext = $ cipher-> encrypt_hex($ data); printHEX_KEY:'$ ciphertext'\\\; 输出: 我正试图在PHP中工作并输出相同HEX但是不一样,出了什么问题? class Test { public function Encypt ($ data,$ cryptkey){ $ encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$ cryptkey,$ data,MCRYPT_MODE_CBC); 返回bin2hex($ encrypted); } } $ data =hello; $ test = new Test(); $ cryptkey =_PRIVATE_; $ hex = $ test-> Encypt($ data,$ cryptkey); echo $ hex; 输出 2bab1b8874692176d213e4c23565b304 解决方案 Crypt :: CBC 和 mcrypt_encrypt 使用不同的默认值,导致这种不匹配。 对于 mcrypt_encrypt ,文档提供了以下信息:对于 Crypt :: CBC ,我们得到这个行为:请注意, RIJNDAEL_128 意味着键长为16,而code> Crypt :: CBC 假设keilength 32 。 使用 Crypt :: Rijndael 没有 Crypt :: CBC wrapper可能更为可取,因为这样可以让我们轻松地将所需的选项设置为PHP使用的相同的默认值: 使用Crypt :: Rijndael; 我的$ key =_PRIVATE_; 我的$ data =hello; #将$密钥填充到16个字节 $ key。=\0x(16 - 长度$ key); #将$数据填充到16个字节的倍数: if(my $ distance = length($ data)%16){ $ data。=\0x 16 - $ distance); } 我的$ crypt = Crypt :: Rijndael-> new($ key,Crypt :: Rijndael :: MODE_CBC); $ crypt-> set_iv(\0x 16); 我的$ binary = $ crypt-> encrypt($ data); print unpack(H *,$ binary),\\\; 然后输出 2bab1b8874692176d213e4c23565b304 。 I have written encryption functionality in Perl and I am trying to get to work same way in PHP.In PERL:#!/usr/bin/perluse strict;use warnings;use Crypt::CBC;use Crypt::Rijndael;my $cryptkey = '_PRIVATE_';my $cipher = Crypt::CBC->new( -key => $cryptkey, -salt => 1, -cipher => 'Rijndael', );my $data = "hello";my $ciphertext = $cipher->encrypt_hex($data);print "HEX_KEY: '$ciphertext' \n";Output: I am trying to get to work in PHP and output same HEX but it is not the same, what went wrong?class Test { public function Encypt($data, $cryptkey) { $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $cryptkey, $data, MCRYPT_MODE_CBC); return bin2hex($encrypted); }}$data = "hello";$test = new Test();$cryptkey = "_PRIVATE_";$hex = $test->Encypt($data, $cryptkey);echo $hex;Output 解决方案 Crypt::CBC and mcrypt_encrypt use different defaults, which lead to this mismatch.For mcrypt_encrypt, the documentation offers this information:For Crypt::CBC, we get this behaviour instead:Note further that RIJNDAEL_128 implies a keylength of 16, whereas Crypt::CBC assumes a keylength of 32.Using Crypt::Rijndael without the Crypt::CBC wrapper is probably preferable, because this allows us to easily set the required options to the same defaults which PHP uses:use Crypt::Rijndael;my $key = "_PRIVATE_";my $data = "hello";# pad the $key to 16 bytes$key .= "\0" x (16 - length $key);# pad the $data to a multiple of 16 bytes:if (my $distance = length($data) % 16) { $data .= "\0" x (16 - $distance);}my $crypt = Crypt::Rijndael->new($key, Crypt::Rijndael::MODE_CBC);$crypt->set_iv("\0" x 16);my $binary = $crypt->encrypt($data);print unpack("H*", $binary), "\n";Which then outputs 2bab1b8874692176d213e4c23565b304. 这篇关于加密密码 - 尝试以与Perl相同的方式使用PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-01 08:57