Crypto ++低级AES API(一个SJCL)

时间:2013-01-27 01:43:29

标签: javascript c++ encryption aes crypto++

我正在尝试从javascript实现中对协议进行反向工程,而我却坚持使用AES加密方法。我正在使用Crypto ++库在C ++中编程。它正在使用SJCL库,并调用ciphertext = (new sjcl.cipher.aes(key)).encrypt(plaintext)。从SJCL文档中,我可以看到这是一个低级接口。 ciphertextkeyplaintext都是32位整数的4元素数组。据我所知,并且可以在谷歌上找到,Crypto ++只提供高级接口。有没有办法让Crypto ++做SJCL正在做的事情?此外,encrypt方法究竟做了什么?

http://bitwiseshiftleft.github.com/sjcl/doc/symbols/sjcl.cipher.aes.html

编辑:我注意到javascript代码从字符串转换为plantext到int,我想我尝试了改变每个变量的endianess的每个组合。无论如何,我尝试了所有有意义的组合。我还尝试创建int32_t的数组,并用输入到该行的javascript中初始化它们。这就是我现在所拥有的:

void crypto()
{
    SecByteBlock key(16);
    int32_t plain[4] = { 0x93C467E3, 0x7DB0C7A4, 0xD1BE3F81, 0x0152CB56 }, cipher[4];
    int32_t* keyBuf = (int32_t*) key.BytePtr();

    keyBuf[0] = 1885434739;
    keyBuf[1] = 2003792484;
    keyBuf[2] = 0;
    keyBuf[3] = 0;

    cout << "plain = [" << plain[0] << ", " << plain[1] << ", " << plain[2] << ", " << plain[3] << "]\n";

    cout << "key = [" << keyBuf[0] << ", " << keyBuf[1] << ", " << keyBuf[2] << ", " << keyBuf[3] << "]\n";

    ECB_Mode<AES>::Encryption e;
    e.SetKey(key, key.size());

    StringSource((const byte*) plain, 16, true, new StreamTransformationFilter( e, new ArraySink((byte*)cipher, 16) ) );

    cout << "cipher = [" << cipher[0] << ", " << cipher[1] << ", " << cipher[2] << ", " << cipher[3] << "]\n";

}

function crypto()
{
    var key = [1885434739, 2003792484, 1885434739, 2003792484];
    var plain = [0x93C467E3,0x7DB0C7A4,0xD1BE3F81,0x0152CB56];
    console.log("plain = "+plain);
    console.log("key = "+key);
    var cipher = (new sjcl.cipher.aes(key)).encrypt(plain);
    console.log("cipher = "+cipher);
}

这是C ++版本的输出:

plain = [2479122403, 2108737444, 3518906241, 22203222]
key = [1885434739, 2003792484, 1885434739, 2003792484]
cipher = [3437909595, 1341853431, 2532744872, 2416113380]

和JavaScript:

plain = 2479122403,2108737444,3518906241,22203222
key = 1885434739,2003792484,1885434739,2003792484
cipher = -1974659585,-1567997661,-1863224381,-318378846

1 个答案:

答案 0 :(得分:2)

是的,您应该使用整数作为字节数组的大端表示。所以最高字节应该在左边。然后你得到16字节的输入/输出,用于128位AES。您可以在ECB模式下使用Crypto ++或其他更高级别的加密库来执行块加密/解密。

请注意,SJCL aes类仅实现原语。它在SJCL中用作ccmocb2加密模式之一的实现。这些模式反过来被便利库使用,但是 - 如果我没记错的话 - 那就是直接使用JSON编码/解码,这可能对你来说太过分了。