0

我试图让 as3crypto 在 AES-128 模式下与 Gibberish 或 EzCrypto 一起玩得很好。无论我使用哪种设置组合,我都无法让一个来解密另一个,并且通常会在 ruby​​ 中收到“解密错误”消息。每个包含的环境都可以解密它自己加密的数据,但其中一个似乎无法解密另一个。有没有人能让两者一起工作?

这是我尝试过的一种变体:

在 Actionscript 方面,使用 as3crypto:

//define the encryption key
var key:ByteArray = Hex.toArray("password");

//put plaintext into a bytearray
var plainText:ByteArray = Hex.toArray(Hex.fromString("this is a secret!"));

//set the encryption key
var aes:AESKey = new AESKey(key);

//encrypt the text
aes.encrypt( plainText );
trace(Base64.encode(Hex.fromArray(plainText))); 
//encrypted value is N2QwZmI0YWQ4NzhmNDNhYjYzM2QxMTAwNGYzNDI1ZGUyMQ==

在红宝石方面,使用胡言乱语:

// also tried the default size (256)
cipher = Gibberish::AES.new("password",128)

// raises the following exception: OpenSSL::Cipher::CipherError: wrong final block length
cipher.dec("N2QwZmI0YWQ4NzhmNDNhYjYzM2QxMTAwNGYzNDI1ZGUyMQ==")

我尝试了各种不同的方法,都产生上述异常或“错误加密”

4

1 回答 1

0

终于自己弄明白了。问题是 Gibberish 和 EzCrypto 似乎都没有提供指定 IV 的方法,这是使用 aes-cbc 时需要的。诀窍是从 as3crypto 生成的加密数据的前 16 个字节中提取 iv。

这是 as3 代码,它也有一点变化:

// there are other ways to create the key, but this works well
var key:ByteArray = new ByteArray();
key.writeUTFBytes(MD5.encrypt("password"));

// encrypt the data. simple-aes-cbc is equiv. to aes-256-cbc in openssl/ruby, if your key is
// long enough (an MD5 is 32 bytes long)
var data:ByteArray = Hex.toArray(Hex.fromString("secret"));
var mode:ICipher= Crypto.getCipher("simple-aes-cbc", key) ;
mode.encrypt(data);

// the value here is base64, 32 bytes long. the first 16 bytes are the IV, needed to decrypt
// the data in ruby
// e.g: sEFOIF57LVGC+HMEI9EMTpcJdcu4J3qJm0PDdHE/OSY=
trace(Base64.encodeByteArray(data));

ruby 部分使用名为encryptor的 gem来提供 iv。您也可以直接使用 OpenSSL,这非常简单:

key = Digest::MD5.hexdigest("password")
// decode the base64 encoded data back to binary:
encrypted_data = Base64.decode64("sEFOIF57LVGC+HMEI9EMTpcJdcu4J3qJm0PDdHE/OSY=")
// the tricky part: extract the IV from the decoded data
iv = encrypted_data.slice!(0,16)
// decrypt!
Encryptor.decrypt(encrypted_data,:key=>key,:iv=>iv)
// should output "secret"
于 2011-06-28T14:19:11.913 回答