Perl和Ruby交换AES加密信息

在Perl for Ruby中,Crypt :: CBC的等价物是什么?

注意:此问题类似于stackoverflow:655691上的PHP / Perl.

Perl版本

use Crypt::CBC;
use MIME::Base64::Perl;

my $cipher = Crypt::CBC->new(
    -key    => "95A8EE8E89979B9EFDCBC6EB9797528D",-keysize => 32,-cipher => "Crypt::OpenSSL::AES"
  );  

$encypted = $cipher->encrypt("ABCDEFGH12345678");
$base64 = encode_base64($encypted);

print("$base64"); # output -> U2FsdGVkX18m1jVqRTxANhcEj6aADeOn+2cccDft2eYAMfOkYCvAAkTIOv01VHc/

$de_base64 = decode_base64($base64);
$decrypted = $cipher->decrypt($de_base64);
$c = $cipher->finish;
print("$decrypted \n");

我的ruby版本看起来像这样:

require 'openssl'
require 'base64'

aes = OpenSSL::Cipher::AES128.new("CBC")
aes.encrypt
aes.key = "95A8EE8E89979B9EFDCBC6EB9797528D"

encypted = aes.update("ABCDEFGH12345678") + aes.final
base64 = Base64.encode64(encypted)

puts base64 # outout -> gx1K24LqlRUtNNTDNUJTyn7HrVKK6UkfNA9LNpNjZeE=

我非常确定Base64在Ruby和Perl中的工作方式相同.有什么想法,正确的方法是什么?

更新(解决方案)

use Crypt::CBC;
use MIME::Base64;

my $key = "95A8EE8E89979B9E";
my $iv = "1234567890abcdef";

my $cipher = Crypt::CBC->new(
                  -key        => $key,-cipher     => 'Rijndael',-iv         => $iv,-literal_key => 1,-padding => 'null',-keysize => 128/8,-header     => 'none'
                  );  
my $plaintext = $cipher->encrypt("Hello");
print encode_base64($plaintext); # output -> kJCpQC0+iNF8exHGx3GLYw==

ruby

require 'openssl'
require 'base64'

aes = OpenSSL::Cipher::Cipher.new("aes-128-cbc")
aes.decrypt
aes.key = "95A8EE8E89979B9E"
aes.iv = "1234567890abcdef"
aes.padding = 0

base64 = Base64.decode64("kJCpQC0+iNF8exHGx3GLYw==")
decrypted = aes.update(base64)
decrypted << aes.final

puts decrypted # guess? It is "Hello"

解决方法

我对Perl和PHP有相同的东西.

http://cpansearch.perl.org/src/FAYLAND/OpenSocialX-Shindig-Crypter-0.03/sample/crypt.pl
http://cpansearch.perl.org/src/FAYLAND/OpenSocialX-Shindig-Crypter-0.03/sample/crypt.php

还有一个提示

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

key必须是16的长度.更好的iv长度也是16个字符.

我希望它有所帮助.

谢谢.

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...