|
这是从OpenQ上摘出来的加密的代码
在109行调用了41行的encrypt_every_8_byte函数,这个函数的第65行 crypted+=8把crypted移动了8,可是函数调用结束后,crypted又返回了原值。到底是怎么回事阿。
- #include <iostream.h>
- #include <stdlib.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/types.h>
- typedef unsigned char int8;
- typedef unsigned short int16;
- typedef unsigned int int32;
- void qq_encipher (
- unsigned long *const v,
- const unsigned long *const k,
- unsigned long *const w)
- {
- register unsigned long
- y = ntohl(v[0]),
- z = ntohl(v[1]),
- a = ntohl(k[0]),
- b = ntohl(k[1]),
- c = ntohl(k[2]),
- d = ntohl(k[3]),
- n = 0x10,
- sum = 0,
- delta = 0x9E3779B9; /* 0x9E3779B9 - 0x100000000 = -0x61C88647 */
- while (n-- > 0) {
- sum += delta;
- y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
- z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
- }// while
- w[0] = htonl(y); w[1] = htonl(z);
- }// qq_enciper
- void encrypt_every_8_byte(
- int *pos_in_byte,
- int *is_header,
- unsigned char *plain_pre_8,
- unsigned char *plain,
- unsigned char *crypted_pre_8,
- unsigned char *crypted,
- unsigned char *key,
- int *count)
- {
- for(*pos_in_byte=0; *pos_in_byte<8; (*pos_in_byte)++) {
- if(*is_header) { plain[*pos_in_byte] ^= plain_pre_8[*pos_in_byte]; }
- else { plain[*pos_in_byte] ^= crypted_pre_8[*pos_in_byte]; }
- } // prepare plain text
- qq_encipher( (unsigned long *) plain,
- (unsigned long *) key,
- (unsigned long *) crypted); // encrypt it
-
- for(*pos_in_byte=0; *pos_in_byte<8; (*pos_in_byte)++) {
- crypted[*pos_in_byte] ^= plain_pre_8[*pos_in_byte];
- }
- memcpy(plain_pre_8, plain, 8); // prepare next
-
- crypted_pre_8 = crypted; // store position of previous 8 byte
- crypted += 8; // prepare next output
- *count += 8; // outstrlen increase by 8
- *pos_in_byte = 0; // back to start
- *is_header = 0; // and exit header
- printf("crypted=%x\n",crypted);
-
- }// encrypt_every_8_byte
- int rand_0(void) { // it can be the real random seed function
- return 0xdead; } // override with number, convenient for debug
- void qq_encrypt (
- unsigned char* instr,
- int instrlen,
- unsigned char* key,
- unsigned char* outstr,
- int outstrlen_prt)
- {
- unsigned char
- plain[8], // plain text buffer
- plain_pre_8[8], // plain text buffer, previous 8 bytes
- * crypted, // crypted text
- * crypted_pre_8, // crypted test, previous 8 bytes
- * inp; // current position in instr
- int
- pos_in_byte = 1, // loop in the byte
- is_header=1, // header is one byte
- count=0, // number of bytes being crypted
- padding = 0; // number of padding stuff
-
- pos_in_byte = (instrlen + 0x0a) % 8; // header padding decided by instrlen
- if (pos_in_byte) {
- pos_in_byte = 8 - pos_in_byte;
- }
- plain[0] = (rand_0() & 0xf8) | pos_in_byte;
-
- memset(plain+1, rand_0()&0xff, pos_in_byte++);
- memset(plain_pre_8, 0x00, sizeof(plain_pre_8));
- crypted = crypted_pre_8 = outstr;
-
- padding = 1; // pad some stuff in header
- while (padding <= 2) { // at most two byte
- if(pos_in_byte < 8) { plain[pos_in_byte++] = rand_0() & 0xff; padding ++; }
- if(pos_in_byte == 8){ encrypt_every_8_byte(&pos_in_byte,&is_header,plain_pre_8,plain,crypted_pre_8,crypted,key,&count);
- printf("crypted=%x\n",crypted);
- }
- }
-
- inp = instr;
- while (instrlen > 0) {
- if (pos_in_byte < 8) { plain[pos_in_byte++] = *(inp++); instrlen --; }
- if (pos_in_byte == 8){ encrypt_every_8_byte(&pos_in_byte,&is_header,plain_pre_8,plain,crypted_pre_8,crypted,key,&count); }
- }
- padding = 1; // pad some stuff in tailer
- while (padding <= 7) { // at most sever byte
- if (pos_in_byte < 8) { plain[pos_in_byte++] = 0x00; padding ++; }
- if (pos_in_byte == 8){ encrypt_every_8_byte(&pos_in_byte,&is_header,plain_pre_8,plain,crypted_pre_8,crypted,key,&count); }
- }
-
- outstrlen_prt = count;
- }// qq_encrypt
- main()
- {
- int8 *key,key_1[]={0xe0,0xcc,0x55,0x76,0xfe,0x4d,0xc6,0x92,0x16,0x17,0xc6,0x97,0x14,0x91,0x1e,0xa7,0x1e};
- key=key_1;
- int8 *outstr=new int8[69];
- int outstrlen_ptr;
- qq_encrypt((int8 *)"",0, key, outstr, outstrlen_ptr);
- printf("this_is_raw_data\n");
- for(int k=0;outstr[k]!='\0';k++)
- printf("%x",outstr[k]);
- printf("\n");
- }
复制代码 |
|