为了
a = b ^ c
由于当 a、b、c 中的任何 2 个值已知时,XOR 操作是微不足道的,因此只需要您反转操作的顺序即可。因为我不使用 Java,所以我必须用 C 来做这件事。
int *encode(int *text, int *key) {
int j = 0x0;
int i = 0x33; // value 51 in decimal
for (i = 0x33;;) {
text[4] ^= ((key[j++]) ^ (text[0])) + i;
text[5] ^= ((key[j++]) ^ (text[1])) + i;
text[6] ^= ((key[j++]) ^ (text[2])) + i;
text[7] ^= ((key[j++]) ^ (text[3])) + i;
text[1] ^= ((key[j++]) ^ (text[4])) + i;
if (j >= 0x3c)
break;
text[2] ^= ((text[4]) ^ (text[5])) + i;
text[3] ^= ((key[j++]) ^ (text[6])) + i;
text[0] ^= ((key[j++]) ^ (text[7])) + i;
}
return text;
}
这是有效的完整代码。
#include <stdio.h>
int *decode(int *text, int *key) {
int j = 0x3c; // value 60 in decimal
int i = 0x33; // value 51 in decimal
for (i = 0x33;;) {
text[1] ^= ((key[j--]) ^ (text[4])) + i;
text[7] ^= ((key[j--]) ^ (text[3])) + i;
text[6] ^= ((key[j--]) ^ (text[2])) + i;
text[5] ^= ((key[j--]) ^ (text[1])) + i;
text[4] ^= ((key[j--]) ^ (text[0])) + i;
if (j <= 0)
break;
text[0] ^= ((key[j--]) ^ (text[7])) + i;
text[3] ^= ((key[j--]) ^ (text[6])) + i;
text[2] ^= ((text[4]) ^ (text[5])) + i;
}
return text;
}
int *encode(int *text, int *key) {
int j = 0x0;
int i = 0x33; // value 51 in decimal
for (i = 0x33;;) {
text[4] ^= ((key[j++]) ^ (text[0])) + i;
text[5] ^= ((key[j++]) ^ (text[1])) + i;
text[6] ^= ((key[j++]) ^ (text[2])) + i;
text[7] ^= ((key[j++]) ^ (text[3])) + i;
text[1] ^= ((key[j++]) ^ (text[4])) + i;
if (j >= 0x3c)
break;
text[2] ^= ((text[4]) ^ (text[5])) + i;
text[3] ^= ((key[j++]) ^ (text[6])) + i;
text[0] ^= ((key[j++]) ^ (text[7])) + i;
}
return text;
}
int key[] = {0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41};
int text[] = {0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68};
int main(int argc, char **argv) {
int i, *d, *e;
for (i = 0; i < 8; i++) {
printf("%x::", text[i]);
}
putchar(10);
d = decode(text, key);
for (i = 0; i < 8; i++) {
printf("%x::", d[i]);
}
putchar(10);
e = encode(text, key);
for (i = 0; i < 8; i++) {
printf("%x::", e[i]);
}
putchar(10);
}
这会产生以下输出
61::62::63::64::65::66::67::68::
65a::3e9::64a::bd6::5c2::11::1a9::85e::
61::62::63::64::65::66::67::68::
我能够在 a decode
and之后取回原始数组encode
。