【问题标题】:Rijndael CBC encryption decryption in PerlPerl 中的 Rijndael CBC 加密解密
【发布时间】:2016-06-26 04:33:56
【问题描述】:

我在下面有用于加密/解密的 perl 代码。加密接缝正在工作。解密不是恢复原件。原文:4111111111111111 加密:IW7K95q8p1Wa89CQ2DoIxQ== 解密:§À@ŽŒ¦õúbp 我需要解密以匹配原件。有任何想法吗?还有什么建议吗?

use strict;
use warnings;
use feature qw( say );

use Crypt::CBC   qw( );
use MIME::Base64 qw( encode_base64 decode_base64 );

sub decrypt {
my $my_string=@_;
my $cipher = Crypt::CBC->new(
{
    'key'         => 'length16length16',
    'cipher'      => 'Rijndael',
    'iv'          => '1234567890abcdef',
    'literal_key' => 1,
    'padding'     => 'null',
    'header'      => 'none',
    keysize       => 128 / 8
}
);
my $return = $cipher->decrypt($my_string);
return $return;
}

sub encrypt {
my $my_string=@_;
my $cipher = Crypt::CBC->new(
{
    'key'         => 'length16length16',
    'cipher'      => 'Rijndael',
    'iv'          => '1234567890abcdef',
    'literal_key' => 1,
    'padding'     => 'null',
    'header'      => 'none',
    keysize       => 128 / 8
}
);
my $return = encode_base64($cipher->encrypt($my_string));
return $return;
}

my $cc = '4111111111111111';
my $coded = encrypt($cc);
say $coded;
my $decoded = decrypt($coded);
say $decoded;

【问题讨论】:

    标签: perl encryption base64 rijndael


    【解决方案1】:

    错误 1

    下面将@_1)中的元素个数赋值给$mystring

    my $my_string=@_;
    

    你想要:

    my ($my_string) = @_;
    

    错误 2

    您在加密期间使用 base64 对数据进行了编码,但在解密期间您没有对应的decode_base64

    错误 3

    你所拥有的非常不安全!

    通过使用带有文本密码的literal_key 和使用常量iv,您违反了许多安全机制。

    请注意,使用null 填充仅在明文不能包含 NUL 字符时才有效。这不是一个非常合适的填充方法。

    我不知道使用 16 字节密钥而不是默认的 32 字节密钥有什么影响。

    错误 4

    encode_base64 被滥用,因为您不希望在编码字符串中出现换行符。将encode_base64($s) 替换为encode_base64($s, '')

    错误 5

    你的缩进很糟糕。

    解决方案

    #!/usr/bin/perl    
    use strict;
    use warnings;
    use feature qw( say );
    
    use Crypt::CBC   qw( );
    use MIME::Base64 qw( encode_base64 decode_base64 );
    
    my $key = 'length16length16';
    
    my $cipher = Crypt::CBC->new({
        cipher => 'Rijndael',
        key    => $key,
    });
    
    sub decrypt {
        my ($my_string) = @_;
        return $cipher->decrypt(decode_base64($my_string));
    }
    
    sub encrypt {
        my ($my_string) = @_;
        return encode_base64($cipher->encrypt($my_string), '');
    }
    
    {
        my $cc = '4111111111111111';
        my $coded = encrypt($cc);
        say $coded;
        my $decoded = decrypt($coded);
        say $decoded;
    }
    

    输出:

    U2FsdGVkX1/QYQrNSEadlko4jtKdjM+yNaW0ZnCAmhyHHz0NyDL+id6BsM2kVPGw
    4111111111111111
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-08
      • 2014-12-07
      • 2010-10-13
      • 2014-10-14
      • 2012-05-06
      • 1970-01-01
      • 2023-03-03
      • 2013-08-24
      相关资源
      最近更新 更多