dotws86260 2011-10-29 22:26
浏览 82

Perl中的TripleDES ECB加密,与PHP mcrypt的实现兼容

I'm trying to create a package in perl that is equivalent to our pre-existing class in php for handling data encryption.

The encryption type seems to be TripleDES in ECB mode however I cannot replicated the resulting cypher text using Crypt::CBC or Crypt::TripleDES.

I think the issue is to do with either padding or the key format (binary vs hex etc) yet searching though the documentation isn't helping me find an answer.

The current PHP class is as follows (trimmed down but with the same core functionality):

<?php
class Encryption {
    private $encryption_key;
    private $encryption_td;
    private $encryption_iv;

    private function __construct() {
        $this->encryption_key = 'abc123';
        $this->encryption_td = mcrypt_module_open( 'tripledes', '', 'ecb', '' );
        $this->encryption_iv = mcrypt_create_iv ( mcrypt_enc_get_iv_size( $this->encryption_td ), MCRYPT_RAND );

        mcrypt_generic_init( $this->encryption_td, $this->encryption_key, $this->encryption_iv );
    }

    public function __destruct() {
        mcrypt_generic_deinit( $this->encryption_td );
        mcrypt_module_close( $this->encryption_td );
    }   

    public static function instance($key = null, $iv = null, $algorithm = null, $mode = null) {
        static $instance;

        if (! $instance) {
            $instance = new Encryption( $key, $iv, $algorithm, $mode );
        }

        return $instance;
    }

    public function encrypt( $password ) {
        return base64_encode( mcrypt_generic( $this->encryption_td, $password ) );
    }

    public function decrypt( $password ) {
        return trim(mdecrypt_generic( $this->encryption_td, base64_decode( $password ) ) );
    }
}

function decrypt_password( $password ) {
    return Encryption::instance()->decrypt( $password );
}

function encrypt_password( $password ) {
    return Encryption::instance()->encrypt( $password );
}

print encrypt_password( 'wibblewobble123' ) . "
";
print decrypt_password( encrypt_password( 'wibblewobble123' ) ) . "
";
?>

My current perl package is as follows:

package Encryption;
use warnings;
use strict;
use MIME::Base64;
use Crypt::TripleDES;

sub new {
    my $class = shift;
    my $self = {};
    $self->{'encryption_key'} = 'abc123';
    $self->{'td'} = Crypt::TripleDES->new();
    bless( $self, $class );
    return $self;
}

sub decrypt {
    my( $self, $encrypted_password ) = @_;
    $encrypted_password = decode_base64( $encrypted_password );
    my $password = $self->{'td'}->decrypt3( $encrypted_password, $self->{'encryption_key'} );
    chomp( $password );
    return $password;
}

sub encrypt {
    my( $self, $password ) = @_;
    my $encrypted_password = $self->{'td'}->encrypt3( $password, $self->{'encryption_key'} );
    $encrypted_password = encode_base64( $encrypted_password );
    chomp( $encrypted_password );
    undef( $password );
    return $encrypted_password;
}

1;

And the test code:

use warnings;
use strict;

use Encryption;

sub decrypt {
    my $password = shift;
    my $eh = Encryption->new();
    return $eh->decrypt( $password );
}

sub encrypt {
    my $password = shift;
    my $eh = Encryption->new();
    return $eh->encrypt( $password );
}

print encrypt( 'wibblewobble123' ) . "
";
print decrypt( encrypt( 'wibblewobble123' ) ) . "
";

The output from each is as follows:

php:

xuRt3xxjPO1GUz+DccTVKw==
wibblewobble123

perl:

mmWuKkpvveHvnUsQ2NC6DA==
wibblewobble123

The expected result is perl's encryption subroutine returns the same output as php's encryption function with decrypt doing the same but in reverse.

If Crypt::TripleDES is the wrong way to be attacking this problem then I'm happy to use something else - This code is going to get re-written into something neater anyway.

As a side note this will need to work with multiple key lengths, so if it is a padding issue please explain how to calculate the correct padding based upon key length.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记