【问题标题】:Perl & Ruby exchange AES encrypted informationPerl & Ruby 交换 AES 加密信息
【发布时间】:2009-10-27 03:09:35
【问题描述】:

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==

红宝石

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"

【问题讨论】:

    标签: ruby perl cryptography aes rijndael


    【解决方案1】:

    我对 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 个字符。

    希望对你有帮助。

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      • 2013-08-24
      • 2017-11-11
      • 2011-10-24
      • 2014-04-30
      • 2018-03-22
      相关资源
      最近更新 更多