1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
| #include<cstdio>
#define ulonglong unsigned long long
#define longlong long long
using namespace std;
void encrypt(char *ciphertext,char *plain)
{
ulonglong p1,p2;
ulonglong *in_a2;
int cycle;
ulonglong t1,t2,c1,c2;
p1 = *(ulonglong *)plain;
p2 = *(ulonglong *)(plain + 8);
//printf("P1 %llX P2 %llX\n",p1,p2);
t2 = 0;
t1 = 0;
c2 = ((p2>> 8) + (p2<< 0x38) + p1) ^ t2;
c1 = ((p1 >> 0x3d) + (p1 << 3)) ^ c2;
cycle = 0;
while (cycle < 0x1f) {
t1 = ((t1 >> 8) + (t1 << 0x38) + t2) ^ (longlong)cycle;
t2 = ((t2 >> 0x3d) + (t2 << 3)) ^ t1;
//printf("%llX %llX %llX\n",c1,c2,t2);
c2 = ((c2 >> 8) + (c2 << 0x38) + c1) ^ t2;
c1 = ((c1 >> 0x3d) + (c1 << 3)) ^ c2;
cycle = cycle + 1;
}
printf("%llX %llX %llX %llX\n",c1,c2,t1,t2);
*(ulonglong *)ciphertext = c1;
*(ulonglong *)(ciphertext + 8) = c2;
return;
}
ulonglong t1s[40],t2s[40];
void genFlows(ulonglong k1,ulonglong k2)
{
ulonglong t2 = k2,t1 = k1,cycle = 0;
while (cycle < 0x1f) {
t1 = (t1 >> 8) + (t1 << 0x38) + t2 ^ (longlong)cycle;
t2 = ((t2 >> 0x3d) + (t2 << 3)) ^ t1;
t1s[cycle]=t1;
t2s[cycle]=t2;
cycle+=1;
//printf("%d\n",cycle);
}
// printf("Get Key Flows\n");
}
void decrypt(ulonglong k1,ulonglong k2,char *ciphertext,char *plain)
{
genFlows(k1,k2);
ulonglong c1,c2;
c1 = *(ulonglong *)ciphertext;
c2 = *(ulonglong *)(ciphertext + 8);
longlong cycle = 0x1f;
while (cycle > 0) {
/*c2 = (c2 >> 8) + (c2 << 0x38) + c1 ^ t2s[cycle];
c1 = ((c1 >> 0x3d) + (c1 << 3)) ^ c2;*/
cycle = cycle - 1;
c1 ^= c2;
c1 = ((c1 << 0x3d) + (c1 >> 3));
c2 ^= t2s[cycle];
c2 -= c1;
c2 = (c2 << 8) + (c2 >> 0x38);
/*t1 = ((t1 >> 8) + (t1 << 0x38) + t2) ^ (longlong)cycle;
t2 = ((t2 >> 0x3d) + (t2 << 3)) ^ t1;*/
}
//c2 = (p2>> 8) + (p2<< 0x38) + p1 ^ t2;
//c1 = (p1 >> 0x3d) + (p1 << 3) ^ c2;
c1 ^= c2;
c1 = ((c1 << 0x3d) + (c1 >> 3));
c2 ^= k2;
c2 -= c1;
c2 = (c2 << 8) + (c2 >> 0x38);
*(ulonglong *)plain = c1;
*(ulonglong *)(plain + 8) = c2;
}
unsigned char cipherText[100]{
0xc1,0x77,0x1d,0xe1,0x2b,0xf8,0x00,0x2a,0xf4,0x91,0xd5,0x23,0xfc,0x71,0xb1,0xc3,
//0x57,0x59,0x88,0xc2,0x8b,0x1e,0xf1,0x30,0x75,0xeb,0x2f,0x42,0x77,0xab,0x94,0xd5
//0xb9,0x98,0x6e,0x46,0xf0,0x76,0x5d,0xe1,0xf2,0x36,0x77,0x5d,0xb5,0xfd,0x51,0xb6
};
char dePlain[100]={0};
int main()
{
for(ulonglong i=0;i<=0xFFFF;i++)
{
ulonglong k1=0,k2=(i<<48);
decrypt(k1,k2,(char*)cipherText,dePlain);
for(int i=0;i<16;i++)
{
printf("%s\n",dePlain);
}
}
return 0;
}
|