2011-06-27 72 views
0

我试图让as3crypto在AES-128模式下和Gibberish或EzCrypto一起玩。 无论我使用什么设置组合,我都无法让其中一个解密另一个,并且通常会在ruby中获得“坏解密”消息。每个包含的环境都可以解密它自己加密的数据,但似乎无法解密另一个。 有没有人能够让这两个一起工作?让as3crypto与ruby一起工作(Gibberish/EzCrypto)

这是我试过的变化之一:

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

而关于Ruby端,使用的废话

// 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==") 

我已经尝试了各种不同的方法,都产生了上述例外或“坏加密”

+0

你为什么不张贴您使用的代码的相关部分,使人们可以看看吗? – Mat

回答

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

红宝石部分使用了一个名为encryptor供应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" 
+0

你确定AS3部分编译好吗? as3crypto MD5没有静态加密功能,我也试过: var md5:MD5 = new MD5(); \t \t \t \t \t \t \t \t \t \t \t \t变种的src:的ByteArray = Hex.toArray( “密码”); \t \t \t \t \t \t var digest:ByteArray = md5.hash(src); \t \t \t \t \t \t key.writeUTFBytes(Hex.fromArray(digest)); – simo

+0

但是,它没有工作,我得到错误:TypeError:错误#1009:无法访问空对象引用的属性或方法。 \t at com.hurlant.crypto :: Crypto $/getCipher()[D:\ my projects \ dcaclab \ activation \ src \ com \ hurlant \ crypto \ Crypto.as:106] – simo

+0

请分享您的最终代码这个 ?它非常有帮助,我们都会欣赏它:-) – simo