public static int[] decode(int[] text, int[] key) {
int j = 0x3c; //value 60 in decimal
int i = 0x33; //value 51 in decimal
int[] result;
for ( i = 0x33; ; (text[1]) ^= ((key[j]) ^ (text[4])) + i ){
j = j - 1;
text[7] ^= ((key[j--]) ^ (text[3])) + i;
text[6] ^= ((key[j--]) ^ (text[2])) + i;
text[5] ^= ((key[j]) ^ (text[1])) + i;
j = j - 1;
result = text;
(text[4]) ^= ((key[j]) ^ text[0]) + i;
if ( j <= 0 )
break;
j = j - 1;
text[0] ^= ((key[j--]) ^ (text[7])) + i;
text[3] ^= ((key[j]) ^ (text[6])) + i;
text[2] ^= ((text[4]) ^ (text[5])) + i;
j = j - 1;
//System.out.println("i " +i+" - j "+j);
}
return result;
}
,但是如果我想做相反的事情,如何构建编码函数?如何构建逆方法(从解码文本到编码文本)?
#1 楼
对于a = b ^ c
由于已知a,b,c中的任意2个值时,异或运算很容易反转,因此只需要反转完成了哪些操作。由于我不使用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::
我能够在
decode
和encode
之后取回原始阵列。
评论
谢谢@sudhackar,我已经了解了程序。代码还可以。 int * encode(int * text,int * key){int j = 0x0; int i = 0x33; //(i = 0x33 ;;)的十进制值51 {text [4] ^ =((key [j ++])^(text [0]))+ i; text [5] ^ =(((key [j ++])^(text [1]))+ i;文字[6] ^ =(
– Mark Last Jr
18/09/18在10:56
@MarkLastJr如果这是您要查找标记接受的解决方案,然后将其关闭。
– sudhackar
18-09-18在12:44