[英]Mimicking PHP openssl_encrypt() and openssl_decrypt() call in Perl
我正在尝试编写一些Perl代码来解码PHP程序生成的Triple DES (3DES)密钥。 我寻找了一个在Perl中使用Crypt::GCrypt
解码的示例,但是找不到。
我需要一个与PHP的openssl_decode()
等效的Perl,但即使是字符串的加密/解密也不匹配。
这是我的测试代码。 所有数值均为测试样品。
use strict;
{
# match PHP's openssl_encrypt($plain_text, 'des3', $pw, 0, $iv );
# openssl_decrypt($cipher_text,'des3', $pw, 0, $iv );
# $ee = Exxx->new( %args );
# $cipher_text = $ee->encode( $plain_text );
# $plain_text = $ee->decode( $cipher_text );
package Exxx;
use Crypt::GCrypt;
use HTML::Entities;
use MIME::Base64;
my $ex_ = 0;
my $exxx_method = $ex_ ++;
my $exxx_password = $ex_ ++;
my $exxx_iv = $ex_ ++;
my $exxx_cipher = $ex_ ++;
sub new {
my ( $class, $method, $passwd, $iv ) = @_;
my $type = ref($class) ? ref($class) : __PACKAGE__;
my $this = [];
$this->[$exxx_method] = $method ? $method : '3des';
$this->[$exxx_method] = '3des' # map any PHP name to a Perl name
if $this->[$exxx_method] eq 'des3';
$this->[$exxx_password] = pack(
'H*',
$passwd ? $passwd
: '0123456789ABCDEF' . # key 1
'FEDCBA9876543210' . # key 2
'3243F6A8885A308D' ); # key 3
$this->[$exxx_iv] = pack(
'H*',
$iv ? $iv
: '2b7e151628aed2a6' );
bless $this, $type;
return $this;
}
sub _cipher {
my ( $this, $encrypting_decrypting ) = @_;
my $cipher = Crypt::GCrypt->new(
type => 'cipher',
algorithm => $this->[$exxx_method],
mode => 'cbc',
padding => 'standard',
);
$cipher->start($encrypting_decrypting);
$cipher->setiv( $this->[$exxx_iv] );
return $cipher;
}
sub encrypt {
my ( $this, $plain_text ) = @_;
my $cipher = $this->_cipher("encrypting");
$cipher->encrypt($plain_text);
my $encrypted = $cipher->finish();
return encode_base64( $encrypted, "" );
}
sub encrypt_html {
my ( $this, $plain_html ) = @_;
return $this->encrypt( decode_entities($plain_html) );
}
sub decrypt {
my ( $this, $crypt_text ) = @_;
my $cipher = $this->_cipher("decrypting");
my $plain_text = $cipher->decrypt( decode_base64($crypt_text) );
my $f = $cipher->finish();
return $plain_text;
}
}
############################
my $exit = 0;
my $plain = 'Hello, world'; #clear text for both PHP and PERL
my $encrypted_php = 'c0WJDTwtcBsj1vTfTi7jwA=='; #crypted from PHP program
my $exxx = Exxx->new('des3');
my $encrypted = $exxx->encrypt($plain);
if ( $encrypted eq $encrypted_php ) {
print "PASS encrypt: (perl)$encrypted eq (php)$encrypted_php\n";
}
else { #trouble.... does not match PHP
print STDERR "ERROR encrypt: (perl)$encrypted ne (php)$encrypted_php\n";
$exit = 1;
}
my $decrypted = $exxx->decrypt($encrypted);
if ( $plain eq $decrypted ) { #trouble.... does not match PHP
print "PASS decrypt: (perl)$plain == (perl)$decrypted\n";
$exit = 1;
}
{ #trouble.... does not match PHP
print STDERR "ERROR decrypt: (perl)$plain ne (perl)$decrypted\n";
$exit = 1;
}
exit $exit;
我建议您使用出色的Crypt::Cipher
库套件。 它涵盖了大量密码,它独立于任何其他模块,并且我已经使用Visual C以及gcc和g ++成功编译了它,没有任何问题
创建新的Crypt::Cipher
对象时,需要指定DES_EDE
(与Triple DES和3DES相同),其余的应该很明显
Crypt::Cipher->new('DES_EDE', $key);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.