我正在尝试从javascript实现中对协议进行反向工程,而我却坚持使用AES加密方法。我正在使用Crypto ++库在C ++中编程。它正在使用SJCL库,并调用ciphertext = (new sjcl.cipher.aes(key)).encrypt(plaintext)
。从SJCL文档中,我可以看到这是一个低级接口。 ciphertext
,key
和plaintext
都是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