不同语言中相同代码的不同行为



我正在尝试在Javascript(Node.js(中解密Block TEA。我已经尝试在C++中做同样的事情,它按预期工作:

#define DELTA 0x9e3779b9
#define MX (((z>>5^y<<2) + (y>>3^z<<4)) ^ ((sum^y) + (key[(p&3)^e] ^ z)))
#define CRYPT_WORDS (64-4)/4
#define CRYPT_OFFSET 1
void btea_decrypt(uint32_t *v, int n, int base_rounds, uint32_t const key[4])
{
uint32_t y, z, sum;
unsigned p, rounds, e;
/* Decoding Part */
rounds = base_rounds + 52/n;
sum = rounds*DELTA;
y = v[0];
do {
e = (sum >> 2) & 3;
for (p=n-1; p>0; p--) {
z = v[p-1];
y = v[p] -= MX;
}
z = v[n-1];
y = v[0] -= MX;
sum -= DELTA;
} while (--rounds);
}
int main()
{
static const uint32_t key[4] = {0x875bcc51, 0xa7637a66, 0x50960967, 0xf8536c51};
uint32_t buf[64] = {16,  23, 163, 242, 214, 213, 125,  48, 167,  44, 232,
23, 160, 192, 244, 116,  38, 255, 200,  38,  43,  57,
18, 235, 206, 103, 161, 210, 187, 164,  42, 227, 139,
248, 141, 205,  51, 132, 115, 233,  39,  53, 136, 207,
238, 190, 111,  57, 117, 233,  67, 133, 165,  84, 154,
161, 165, 173,  76, 115, 108,   0,   0,  71};
uint32_t cryptpart[CRYPT_WORDS];
// Decrypt encrypted portion
for (int i = 0; i < CRYPT_WORDS; i++) {
cryptpart[i] =
((uint32_t)buf[CRYPT_OFFSET+4*i  ]) << 0  |
((uint32_t)buf[CRYPT_OFFSET+4*i+1]) << 8  |
((uint32_t)buf[CRYPT_OFFSET+4*i+2]) << 16 |
((uint32_t)buf[CRYPT_OFFSET+4*i+3]) << 24;
}
btea_decrypt(cryptpart, CRYPT_WORDS, 1, key);
for (int i = 0; i < CRYPT_WORDS; i++) {
buf[CRYPT_OFFSET+4*i  ] = cryptpart[i] >> 0;
buf[CRYPT_OFFSET+4*i+1] = cryptpart[i] >> 8;
buf[CRYPT_OFFSET+4*i+2] = cryptpart[i] >> 16;
buf[CRYPT_OFFSET+4*i+3] = cryptpart[i] >> 24;
}
for (const auto& e : buf) {
std::cout << e << ", ";
}
}

并输出:

16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65280, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3561881601, 13913600, 54350, 212, 393407394, 1536747, 6002, 23, 3612094810, 14109745, 55116, 215, 589329, 2302, 8, 0, 5439472, 21247, 82, 0, 0, 0, 71,

但是在Node.js:中使用相同的代码(移植(

function btea_decrypt(v, n, base_rounds, key)
{
let y, z, sum;
let p, rounds, e;
/* Decoding Part */
rounds = base_rounds + 52/n;
sum = rounds*0x9e3779b9;
y = v[0];
do {
e = (sum >> 2) & 3;
for (p=n-1; p>0; p--) {
z = v[p-1];
y = v[p] -= (((z>>5^y<<2) + (y>>3^z<<4)) ^ ((sum^y) + (key[(p&3)^e] ^ z)));
}
z = v[n-1];
y = v[0] -= (((z>>5^y<<2) + (y>>3^z<<4)) ^ ((sum^y) + (key[(p&3)^e] ^ z)));
sum -= 0x9e3779b9;
} while (--rounds);
return v;
}
function main() {
let key = [0x875bcc51, 0xa7637a66, 0x50960967, 0xf8536c51];
let buf = [16,  23, 163, 242, 214, 213, 125,  48, 167,  44, 232,
23, 160, 192, 244, 116,  38, 255, 200,  38,  43,  57,
18, 235, 206, 103, 161, 210, 187, 164,  42, 227, 139,
248, 141, 205,  51, 132, 115, 233,  39,  53, 136, 207,
238, 190, 111,  57, 117, 233,  67, 133, 165,  84, 154,
161, 165, 173,  76, 115, 108,   0,   0,  71];
let cryptpart = [];
// Decrypt encrypted portion
for (let i = 0; i < (64-4)/4; i++) {
cryptpart[i] =
(buf[1+4*i  ]) << 0  |
(buf[1+4*i+1]) << 8  |
(buf[1+4*i+2]) << 16 |
(buf[1+4*i+3]) << 24;
}
cryptpart = btea_decrypt(cryptpart, (64-4)/4, 1, key);
for (let i = 0; i < (64-4)/4; i++) {
buf[1+4*i  ] = cryptpart[i] >> 0;
buf[1+4*i+1] = cryptpart[i] >> 8;
buf[1+4*i+2] = cryptpart[i] >> 16;
buf[1+4*i+3] = cryptpart[i] >> 24;
}
console.log(buf)
}

它永远停留在do ... while循环中。

我发现,Javascript和C++处理0x9e3779b9的方式不同,因为JS中的0x9e3779b9 * 15在C++中等于39816536535和1161830871。C++中的数学有什么问题?如何在JS中实现这一点?

对不起,如果我的英语不是最好的。

您的问题是由整数溢出引起的。unin32_t是一个固定大小为2^32位的整数。0x9e3779b9*15是39816536535,大约是2^35。

这意味着您会出现溢出,因为内存位置根本不够大,无法容纳您的数字。Javascript没有这个问题,因为它不是静态类型的,内存中分配的大小会动态增加以容纳它

对C++使用较大的数据类型,如unsigned longsize_t(在大多数系统上是unsigned long的别名(。最好使用auto让编译器为您决定:

auto sum = rounds*0x9e3779b9;

这将解决您的问题,并确保金额足够大,可以容纳号码

附带说明:您的C++中有相当多的C,在C++中,我们尽量避免#define,并在第一次使用时声明变量,而不是在范围的顶部。在编写C++代码时,更喜欢const auto x而不是#define x(如果是基本类型,则可能是constexpr(

相关内容

  • 没有找到相关文章