Perl& Ruby交换AES加密信息 [英] Perl & Ruby exchange AES encrypted information

查看:339
本文介绍了Perl& Ruby交换AES加密信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Perl for Ruby中相当于Crypt :: CBC是什么?

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

注意:这个问题类似于PHP / Perl在 stackoverflow:655691

Note: This problem similar to PHP/Perl at stackoverflow:655691.

Perl版本 b

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版本如下:

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=



我非常确定Base64在Ruby和Perl中的工作方式相同。任何线索,正确的方法是什么?

I'm pretty sure Base64 work same in Ruby and Perl. Any clue what is the right way to do?

更新(解决方案)

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

键的长度必须为16.而且iv的长度也为16个字符。

key must be length 16. and better the length of iv is 16 chars too.

我希望它有帮助。

谢谢。

这篇关于Perl&amp; Ruby交换AES加密信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆