0

PHP プログラムによって生成されたTriple DES (3DES) キーをデコードする Perl コードを書き込もうとしています。Perl でデコードを使用する例を探しましたが、Crypt::GCrypt見つかりません。

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および3DESDES_EDEと同じ) 。残りは明らかなはずです。Crypt::Cipher

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