0

我正在尝试编写一些 Perl 代码来解码PHP 程序生成的三重 DES (3DES) 密钥。我已经寻找Crypt::GCrypt在 Perl 中使用解码的示例,但找不到。

我需要一个等效于 PHP 的 Perl openssl_decode(),但即使是字符串的加密/解密也不匹配。

这是我的测试代码。所有值都是用于测试的样本。

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;
4

1 回答 1

0

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

创建新对象时需要指定DES_EDE(与Triple DES3DESCrypt::Cipher相同) ,其余部分应该很明显

Crypt::Cipher->new('DES_EDE', $key);
于 2016-06-20T02:26:56.443 回答