寻C++、Delphi双修高手(10.01.15)
quark 2010-01-14 11:17:56 哪位高手帮忙把C翻译成delphi,多谢
void qqencrypt( unsigned char* instr, int instrlen, unsigned char* key,
unsigned char* outstr, int* outstrlen_ptr)
{
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*/
//void encrypt_every_8_byte (void);
/*** we encrypt every eight byte ***/
#define encrypt_every_8_byte() \
{\
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*/\
encipher( (unsigned int *) plain,\
(unsigned int *) key, \
(unsigned int *) 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*/\
}/* encrypt_every_8_byte*/
pos_in_byte = (instrlen + 0x0a) % 8; /* header padding decided by instrlen*/
if (pos_in_byte) {
pos_in_byte = 8 - pos_in_byte;
}
plain[0] = (random() & 0xf8) | pos_in_byte;
memset(plain+1, random()&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++] = random() & 0xff; padding ++; }
if(pos_in_byte == 8){ encrypt_every_8_byte(); }
}
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(); }
}
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(); }
}
*outstrlen_ptr = count;
}