我正在尝试从 javascript 实现对协议进行逆向工程,但我被困在 AES 加密方法上。我在 C++ 中使用 Crypto++ 库对此进行编程。它使用 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
从字符串转换为整数,我想我尝试了改变每个变量的字节顺序的每一种组合。无论如何,我尝试了所有有意义的组合。我还尝试创建int32_t
's 的数组并使用输入到该行 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