Perl & Ruby exchange AES encrypted information

What is the equivalent of Crypt :: CBC in Perl for Ruby?

Note. This problem is similar to php / perl on https://stackoverflow.com/a/21293416/11 .

Perl Version

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"); 

My ruby ​​version looks like this:

 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= 

I'm sure Base64 works in Ruby and Perl. Anyone tell me how to do it right?

Update (solution)

 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" 
+4
source share
1 answer

I have one thing for Perl and 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

and there is a tip for you:

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

the key must be 16. and better iv is also 16 characters long.

Hope this helps.

Thanks.

+6
source

All Articles