在Perl中模仿PHP openssl_encrypt()和openssl_decrypt()调用

时间:2016-06-19 23:27:16

标签: php perl libgcrypt

我正在尝试编写一些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;

1 个答案:

答案 0 :(得分:0)

我建议您使用优秀的Crypt::Cipher库套件。它涵盖了大量的密码,它独立于任何其他模块,我使用Visual C以及gcc和g ++成功编译它没有任何问题

创建新的DES_EDE对象时,您需要指定Crypt::Cipher(与 Triple DES 3DES 相同),其余应该是显而易见的

Crypt::Cipher->new('DES_EDE', $key);