|
问题描述:
函数qq_decipher((unsigned long *) instr, (unsigned long *) key, (unsigned long *) decrypted); 执行之后,decrypted数组的值没有改变,为什么?
------------------------------CODE----------------------------
#define DECRYPT 0x00
#define ENCRYPT 0x01
#include <string.h>
#include "stdio.h"
/*****************************************************************************/
void qq_decipher(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 = 0xE3779B90, // why this ? must be related with n value
delta = 0x9E3779B9;
/* sum = delta<<5, in general sum = delta * n */
while (n-- > 0) {
z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
sum -= delta;
}
w[0] = htonl(y);
w[1] = htonl(z);
} // qq_decipher
int qq_decrypt(unsigned char *instr, int instrlen, unsigned char *key, unsigned char *outstr, int *outstrlen_ptr)
{
unsigned char decrypted[8], m[8], *crypt_buff, *crypt_buff_pre_8, *outp;
int count, context_start, pos_in_byte, padding;
int decrypt_every_8_byte(void) {
for (pos_in_byte = 0; pos_in_byte < 8; pos_in_byte++) {
if (context_start + pos_in_byte >= instrlen)
return 1;
decrypted[pos_in_byte] ^= crypt_buff[pos_in_byte];
}
qq_decipher((unsigned long *) decrypted, (unsigned long *) key, (unsigned long *) decrypted);
context_start += 8;
crypt_buff += 8;
pos_in_byte = 0;
return 1;
} // decrypt_every_8_byte
// at least 16 bytes and %8 == 0
if ((instrlen % 8) || (instrlen < 16))
return 0;
// get information from header
《《这里》》qq_decipher((unsigned long *) instr, (unsigned long *) key, (unsigned long *) decrypted);
pos_in_byte = decrypted[0] & 0x7;
count = instrlen - pos_in_byte - 10; // this is the plaintext length
// return if outstr buffer is not large enought or error plaintext length
if (*outstrlen_ptr < count || count < 0)
return 0;
memset(m, 0, 8);
crypt_buff_pre_8 = m;
*outstrlen_ptr = count; // everything is ok! set return string length
crypt_buff = instr + 8; // address of real data start
context_start = 8; // context is at the second 8 byte
pos_in_byte++; // start of paddng stuff
padding = 1; // at least one in header
while (padding <= 2) { // there are 2 byte padding stuff in header
if (pos_in_byte < 8) { // bypass the padding stuff, none sense data
pos_in_byte++;
padding++;
}
if (pos_in_byte == 8) {
crypt_buff_pre_8 = instr;
if (!decrypt_every_8_byte())
return 0;
}
} // while
outp = outstr;
while (count != 0) {
if (pos_in_byte < 8) {
*outp = crypt_buff_pre_8[pos_in_byte] ^ decrypted[pos_in_byte];
outp++;
count--;
pos_in_byte++;
}
if (pos_in_byte == 8) {
crypt_buff_pre_8 = crypt_buff - 8;
if (!decrypt_every_8_byte())
return 0;
}
} // while
for (padding = 1; padding < 8; padding++) {
if (pos_in_byte < 8) {
if (crypt_buff_pre_8[pos_in_byte] ^ decrypted[pos_in_byte])
return 0;
pos_in_byte++;
}
if (pos_in_byte == 8) {
crypt_buff_pre_8 = crypt_buff;
if (!decrypt_every_8_byte())
return 0;
}
} // for
return 1;
} // qq_decrypt
int qq_crypt(unsigned char flag,
unsigned char *instr, int instrlen, unsigned char *key, unsigned char *outstr, int *outstrlen_ptr)
{
return qq_decrypt(instr, instrlen, key, outstr, outstrlen_ptr);
return 1; // flag must be DECRYPT or ENCRYPT
} // qq_crypt
int main(){
unsigned long v[4]={0x4fb5bfcc,0x959b2ad7,0x29dc3e6b,0xb7e3e3c7};
unsigned long k[4]={0xd7877ef4,0x2e9e2677,0x76941e42,0x3c0f81d6};
unsigned long out[4]={6};
int outstr_len_ptr=16;
printf("%d \n",qq_crypt(DECRYPT,(unsigned char *)v,16,(unsigned char *)k,(unsigned char *)out,&outstr_len_ptr));
printf("OUT=%lx%lx %lx%lx\n",out[0],out[1],out[2],out[3]);
return 0;
}
END OF FILE
问题描述:
函数qq_decipher((unsigned long *) instr, (unsigned long *) key, (unsigned long *) decrypted); 执行之后,decrypted数组的值没有改变,为什么? |
|