source: titan/titan/ocrypt.h @ 39052

Last change on this file since 39052 was 17243, checked in by nit, 12 years ago

[titan] add screen serial

File size: 4.5 KB
Line 
1#ifndef OCRYPT_H
2#define OCRYPT_H
3
4struct ocrypt
5{
6        unsigned short ax, bx, cx, dx, si, tmp, x1a2, x1a0[16], res, i, inter, cfc, cfd, compte;
7        unsigned char cle[33];
8        short c;
9        int c1, count;
10        short d, e;
11} oc;
12
13int occode()
14{
15        oc.dx = oc.x1a2 + oc.i;
16        oc.ax = oc.x1a0[oc.i];
17        oc.cx = 0x015a;
18        oc.bx = 0x4e35;
19
20        oc.tmp = oc.ax;
21        oc.ax = oc.si;
22        oc.si = oc.tmp;
23
24        oc.tmp = oc.ax;
25        oc.ax = oc.dx;
26        oc.dx = oc.tmp;
27
28        if(oc.ax != 0)
29                oc.ax = oc.ax * oc.bx;
30
31        oc.tmp = oc.ax;
32        oc.ax = oc.cx;
33        oc.cx = oc.tmp;
34
35        if(oc.ax != 0)
36        {
37                oc.ax = oc.ax * oc.si;
38                oc.cx = oc.ax + oc.cx;
39        }
40
41        oc.tmp = oc.ax;
42        oc.ax = oc.si;
43        oc.si = oc.tmp;
44        oc.ax = oc.ax * oc.bx;
45        oc.dx = oc.cx + oc.dx;
46
47        oc.ax = oc.ax + 1;
48
49        oc.x1a2 = oc.dx;
50        oc.x1a0[oc.i] = oc.ax;
51
52        oc.res = oc.ax ^ oc.dx;
53        oc.i = oc.i + 1;
54
55        return 0;
56}
57
58int ocassemble()
59{
60        oc.x1a0[0] = (oc.cle[0] * 256) + oc.cle[1];
61        occode();
62        oc.inter = oc.res;
63
64        oc.x1a0[1] = oc.x1a0[0] ^ ((oc.cle[2] * 256) + oc.cle[3]);
65        occode();
66        oc.inter = oc.inter ^ oc.res;
67
68        oc.x1a0[2] = oc.x1a0[1] ^ ((oc.cle[4] * 256) + oc.cle[5]);
69        occode();
70        oc.inter = oc.inter ^ oc.res;
71
72        oc.x1a0[3] = oc.x1a0[2] ^ ((oc.cle[6] * 256) + oc.cle[7]);
73        occode();
74        oc.inter = oc.inter ^ oc.res;
75
76        oc.x1a0[4] = oc.x1a0[3] ^ ((oc.cle[8] * 256) + oc.cle[9]);
77        occode();
78        oc.inter = oc.inter ^ oc.res;
79
80        oc.x1a0[5] = oc.x1a0[4] ^ ((oc.cle[10] * 256) + oc.cle[11]);
81        occode();
82        oc.inter = oc.inter ^ oc.res;
83
84        oc.x1a0[6] = oc.x1a0[5] ^ ((oc.cle[12] * 256) + oc.cle[13]);
85        occode();
86        oc.inter = oc.inter ^ oc.res;
87
88        oc.x1a0[7] = oc.x1a0[6] ^ ((oc.cle[14] * 256) + oc.cle[15]);
89        occode();
90        oc.inter = oc.inter ^ oc.res;
91
92        oc.x1a0[8] = oc.x1a0[7] ^ ((oc.cle[16] * 256) + oc.cle[17]);
93        occode();
94        oc.inter = oc.inter ^ oc.res;
95
96        oc.x1a0[9] = oc.x1a0[8] ^ ((oc.cle[18] * 256) + oc.cle[19]);
97        occode();
98        oc.inter = oc.inter ^ oc.res;
99
100        oc.x1a0[10] = oc.x1a0[9] ^ ((oc.cle[20] * 256) + oc.cle[21]);
101        occode();
102        oc.inter = oc.inter ^ oc.res;
103
104        oc.x1a0[11] = oc.x1a0[10] ^ ((oc.cle[22] * 256) + oc.cle[23]);
105        occode();
106        oc.inter = oc.inter ^ oc.res;
107
108        oc.x1a0[12] = oc.x1a0[11] ^ ((oc.cle[24] * 256) + oc.cle[25]);
109        occode();
110        oc.inter = oc.inter ^ oc.res;
111
112        oc.x1a0[13] = oc.x1a0[12] ^ ((oc.cle[26] * 256) + oc.cle[27]);
113        occode();
114        oc.inter = oc.inter ^ oc.res;
115
116        oc.x1a0[14] = oc.x1a0[13] ^ ((oc.cle[28] * 256) + oc.cle[29]);
117        occode();
118        oc.inter = oc.inter ^ oc.res;
119
120        oc.x1a0[15] = oc.x1a0[14] ^ ((oc.cle[30] * 256) + oc.cle[31]);
121        occode();
122        oc.inter = oc.inter ^ oc.res;
123
124        oc.i = 0;
125
126        return 0;
127}
128
129int ocfin()
130{
131        for(oc.compte = 0; oc.compte <= 31; oc.compte++)
132                oc.cle[oc.compte] = 0;
133
134        oc.ax = 0; oc.bx = 0; oc.cx = 0; oc.dx = 0;
135        oc.si = 0; oc.tmp = 0; oc.x1a2 = 0;
136        oc.x1a0[0] = 0; oc.x1a0[1] = 0; oc.x1a0[2] = 0;
137        oc.x1a0[3] = 0; oc.x1a0[4] = 0;
138        oc.res = 0; oc.i = 0; oc.inter = 0; oc.cfc = 0;
139        oc.cfd = 0; oc.compte = 0; oc.c = 0;
140
141        return 0;
142}
143
144unsigned char* oencrypt(char* pw, char* buf, int len)
145{
146        unsigned long long pos = 0;
147        unsigned char* outbuf = NULL;
148
149        if(pw == NULL || buf == NULL) return NULL;
150
151        outbuf = calloc(1, (len * 2) + 1);
152        if(outbuf == NULL)
153        {
154                err("no mem");
155                return NULL;
156        }
157
158        strcpy((char*)oc.cle, "abcdefghijklmnopqrstuvwxyz012345");
159        strcpy((char*)oc.cle, pw);
160
161        while(pos < len)
162        {
163                oc.c = buf[pos];
164
165                ocassemble();
166                oc.cfc = oc.inter >> 8;
167                oc.cfd = oc.inter & 255;
168
169                for(oc.compte = 0; oc.compte <= 31; oc.compte++)
170                        oc.cle[oc.compte] = oc.cle[oc.compte] ^ oc.c;
171                oc.c = oc.c ^ (oc.cfc ^ oc.cfd);
172
173                oc.d = (oc.c >> 4);
174                oc.e = (oc.c & 15);
175
176                outbuf[pos * 2] = 0x61 + oc.d;
177                outbuf[pos * 2 + 1] = 0x61 + oc.e;
178                pos++;
179        }
180        ocfin();
181
182        return outbuf;
183}
184
185unsigned char* odecrypt(char* pw, char* buf, int len)
186{
187        unsigned long long pos = 0;
188        unsigned char* outbuf = NULL;
189
190        if(pw == NULL || buf == NULL) return NULL;
191
192        outbuf = calloc(1, (len / 2) + 1);
193        if(outbuf == NULL)
194        {
195                err("no mem");
196                return NULL;
197        }
198
199        strcpy((char*)oc.cle, "abcdefghijklmnopqrstuvwxyz012345");
200        strcpy((char*)oc.cle, pw);
201
202        while(pos < len)
203        {
204                oc.d = buf[pos];
205                oc.e = buf[pos + 1];
206
207                oc.d = oc.d - 0x61;
208                oc.d = oc.d << 4;
209
210                oc.e = oc.e - 0x61;
211                oc.c = oc.d + oc.e;
212
213                ocassemble();
214                oc.cfc = oc.inter >> 8;
215                oc.cfd = oc.inter & 255;
216
217                oc.c = oc.c ^ (oc.cfc ^ oc.cfd);
218
219                for(oc.compte = 0; oc.compte <= 31; oc.compte++)
220                        oc.cle[oc.compte] = oc.cle[oc.compte] ^ oc.c;
221
222                outbuf[pos / 2] = oc.c;
223                pos = pos + 2;
224        }
225        ocfin();
226
227        return outbuf;
228}
229
230#endif
Note: See TracBrowser for help on using the repository browser.