Perl AES与Ruby AES

Mig*_*ing -1 ruby encryption perl aes

我不能让Perl和Ruby同意CBC AES:

Perl的

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

my $cipher = Crypt::CBC->new(
        -key         => 'd2cb415e067c7b13',
        -iv          => 'e36dc751d0433f05', #random 16chars!!!!!! shold NOT repeat between requests
        -cipher      => 'OpenSSL::AES',     #this is same as Rijndael
        -literal_key => 1,        
        -header      => "none",
        -keysize     => 16
  );  

$encypted = $cipher->encrypt("a really really long long text has differrent results???");
$base64 = encode_base64($encypted);

print("Ciphertext(b64): $base64");

$de_base64 = decode_base64($base64);
$decrypted = $cipher->decrypt($de_base64);
$c = $cipher->finish;
Run Code Online (Sandbox Code Playgroud)

密文(b64):qz4eSQaFkQUkDOyJSbZf5W03HoldwtgvTLq0yJFRViKJnytf3PVSCGW2CYDjO + tRqV20oxeB2VPa 7NqN1TDSNQ ==

在该2VPa部分后面有一个换行符,在结尾处有另一个换行符

红宝石

require 'openssl'
require 'digest/sha2'
require 'base64'

message = "a really really long long text has differrent results???" 
cipher = OpenSSL::Cipher.new('aes-128-cbc')

# digest the key, iv and hmac_key so we have 16-byte length 
# also, it looks more of a funky password
# prepare cipher
cipher.encrypt
cipher.key = aes_key = "d2cb415e067c7b13"
cipher.iv = aes_iv = "e36dc751d0433f05"


encrypted = cipher.update(message)
encrypted << cipher.final()
b64_encoded = Base64.encode64(encrypted).encode('utf-8') #strict_encode64 guarantees no newlines, encode64 is default

puts "AES Key        : '#{aes_key}'"
puts "AES IV         : '#{aes_iv}'"
puts "Ciphertext(b64): '#{b64_encoded}'"
Run Code Online (Sandbox Code Playgroud)

密文(b64):'qz4eSQaFkQUkDOyJSbZf5W03HoldwtgvTLq0yJFRViKJnytf3PVSCGW2CYDj O + tRqV20oxeB2VPa7NqN1TDSNQ =='

注意newlines之后CYDj和之后的字符==

我见过以下内容:Perl和Ruby交换AES加密信息,但我没有使用padding=0

ike*_*ami 7

Base64中的换行符不重要.两种语言的结果完全相同.

虽然绝对没有理由这样做,但您可以使Perl版本返回与Ruby版本相同的字符串,如下所示:

$base64 = encode_base64($encypted, '');
$base64 =~ s/\G.{60}\K/\n/sg;
Run Code Online (Sandbox Code Playgroud)

  • 可能是因为你忘了逃避字符串里面的`@`.这样Perl会在字符串中看到一个名为`@ somelongstring`的数组并相应地进行插值.你应该真的`使用strict;`和`使用警告;`; 他们会抓住这个问题. (4认同)
  • @Miguel Ping,请不要在评论中提出新的,完全不相关的问题. (3认同)
  • 一个问题是关于base64.另一个正确构建你的字符串.只是去表明你甚至没有做最基本的调试,如果你认为他们是同一个问题:( (2认同)