AS3Crypto& CryptoPP Rijndael ECB

时间:2014-01-17 07:19:42

标签: c++ actionscript-3 encryption aes crypto++

如果我使用此CryptoPP函数加密数据,我想知道如何编写AS3解密方法:

std::string encrypt(const std::string& str_in, const std::string& key, const std::string& iv)
{
std::string str_out;
CryptoPP::ECB_Mode<CryptoPP::Rijndael>::Encryption encryption((byte*)key.c_str(), key.length());

CryptoPP::StringSource encryptor(str_in, true,
    new CryptoPP::StreamTransformationFilter(encryption,
        new CryptoPP::Base64Encoder(
            new CryptoPP::StringSink(str_out),
            false // do not append a newline
        ),
        CryptoPP::StreamTransformationFilter::PKCS_PADDING
    )
);
return str_out;
}

我在这里给出了原始文本的前几个字符,并以一些随机字节结束。

var type = "aes-128-ecb";  // I've tried aes, aes-128-ecb
var key = Hex.toArray(Hex.fromString("xxxxxxxxxxxxx"));

function decryptByteArray(data:ByteArray):String
{
    //var data:ByteArray = Base64.decodeToByteArray(txt);
    var pad:IPad = new NullPad();  // i've also tried PKCS5
    var mode:ICipher = Crypto.getCipher(type, key, pad);
    var ivmode:IVMode = mode as IVMode;
    ivmode.IV = Hex.toArray(Hex.fromString(""));
    pad.setBlockSize(mode.getBlockSize());
    mode.decrypt(data);
    return Hex.toString(Hex.fromArray(data));
}

我也尝试过PKCS5作为填充方法,但是出现了这个错误:

Error: PKCS#5:unpad: Invalid padding value. expected [166], found [229]
at com.hurlant.crypto.symmetric::PKCS5/unpad()
at com.hurlant.crypto.symmetric::CBCMode/decrypt()
at DecryptionTestAS3Crypto_fla::MainTimeline/decryptByteArray()
at DecryptionTestAS3Crypto_fla::MainTimeline/loaderComplete()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()

不确定这里有什么问题。任何帮助将不胜感激!!

0 个答案:

没有答案