如何使用aes-256-ecb加密的as3crypto字符串解密?

时间:2012-12-04 22:06:39

标签: php actionscript-3 aes ecb as3crypto

字符串使用php加密为here。 它可以由this使用参数解密:Rijndael-256,ECB和Base64。但它无法通过我的ActionScript代码解密:

var text:String = "Some data";
var key:ByteArray = new ByteArray();
key.writeUTFBytes("SomePassword");
key.position = key.length;
for (var i:int = key.length; i < 256 / 8; i++) {
    key.writeByte(0);
}
var pad:IPad = new NullPad();
var cipher:ICipher = Crypto.getCipher("aes-256-ecb", key, pad);
var data:ByteArray = Base64.decodeToByteArray(text);
cipher.decrypt(data);
trace(data.toString());

UPD:

&#34;无法解密&#34;意味着我得到错误的纯文本。

在php中,纯文本首先由aes-256-ecb加密。然后它由Base64编码。在ActionScript中,这些步骤以相反的顺序完成。

UPD2:

测试编码解码:

var key:ByteArray = new ByteArray();
key.writeUTFBytes("Some password");
key.position = key.length;
for (var i:int = key.length; i < 256 / 8; i++) {
    key.writeByte(0);
}
trace(key.toString());
var data:ByteArray = new ByteArray();//plain text
data.writeUTFBytes("Hello, my dear World! Hello, the Sun! Hello, the Moon! Hello! Hello! Hello!");
var pad:IPad = new NullPad();
var cipher:ICipher = Crypto.getCipher("aes-256-ecb", key, pad);
trace(data.toString());//trace plain text
cipher.encrypt(data);
trace(data.toString());//trace encrypted text
cipher.decrypt(data);
trace(data.toString());//trace decrypted text

cihper.encrypt(data)暂停后的输出是:

Some password
Hello, my dear World! Hello, the Sun! Hello, the Moon! Hello! Hello! Hello!
[Fault] exception, information=Error: Error #2030: End of file found.

UPD3:

它适用于PKCS5填充:

var pad:IPad = new PKCS5();
var cipher:ICipher = Crypto.getCipher("aes-256-ecb", key, pad);
pad.setBlockSize(cipher.getBlockSize());

输出是:

Some password
Hello, my dear World! Hello, the Sun! Hello, the Moon! Hello! Hello! Hello!
$ú^{±àÙ[pm|x¿9¡ÃZsI D¾`©4¾þÂõ,J
('èfÑk1ôì&­ªQƆfbÇåòþ§VµÄs   ¦p<iÿ
Hello, my dear World! Hello, the Sun! Hello, the Moon! Hello! Hello! Hello!

UPD4:

对于从php获得的数据(作为here)代码,PKCS5填充运行暂停here。并且使用Null填充它不会停止但解密数据是错误的。

1 个答案:

答案 0 :(得分:2)

在上一篇文章中引用代码:

return trim(
    base64_encode(
        mcrypt_encrypt(
            MCRYPT_RIJNDAEL_256,
            $sSecretKey, $sValue, 
            MCRYPT_MODE_ECB, 
            mcrypt_create_iv(
                mcrypt_get_iv_size(
                    MCRYPT_RIJNDAEL_256, 
                    MCRYPT_MODE_ECB
                ), 
                MCRYPT_RAND)
            )
        )
    );

MCRYPT_RIJNDAEL_256不是AES。 MCRYPT_RIJNDAEL_128是。 Rijndael具有可变块大小。 AES是Rijndael的一个子集,具有固定的块大小。

MCRYPT_RIJNDAEL_ *之后的数字是指块大小。所以MCRYPT_RIJNDAEL_256是Rijndael,具有256位块大小。 MCRYPT_RIJNDAEL_128是Rijndael,具有128块大小,即AES。

另外,如果你要使用MCRYPT_MODE_ECB,我会用一串空字节的空字符串替换mcrypt_create_iv()调用。 ECB作为块模式不使用IV。

最后,我个人的建议,因为这对mcrypt来说非常直观,就是使用phpseclib, a pure PHP AES implementation