寻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;

}
...全文
226 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
柯本 2010-01-16
  • 打赏
  • 举报
回复
终于改好了,语法上我已测试,其它的你自己试试吧:
procedure qqencrypt( instr:pbyte; instrlen:integer; key:pbyte; outstr:pbyte; outstrlen_ptr:pinteger);
var
plain:array [0..8] of byte;
plain_pre_8:array [0..8] of byte;
crypted:pbyte;
crypted_pre_8:pbyte;
inp:pbyte;
pos_in_byte :integer;
is_header:integer;
count:integer;
padding :integer;
temp_ptr:pbyte;


begin

pos_in_byte := 1;
is_header:=1;
count:=0;
padding := 0;


pos_in_byte := (instrlen + $0a) mod 8;
if (pos_in_byte<>0) then
begin
pos_in_byte := 8 - pos_in_byte;
end ;

plain[0] := (random(maxint) and $f8) or pos_in_byte;
fillchar(plain[1], pos_in_byte,integer(random(maxint)) and $ff);
inc(pos_in_byte);
fillchar(plain_pre_8, sizeof(plain_pre_8),$0);
crypted := outstr;
crypted_pre_8 := outstr ;
padding := 1;
while (padding <= 2) do
begin
if(pos_in_byte < 8) then
begin
plain[pos_in_byte] :=integer(random(maxint)) and $ff;
inc(pos_in_byte);
inc(padding);
end;
if(pos_in_byte = 8) then
begin
for pos_in_byte:=0 to 7 do
begin
if(is_header<>0) then
begin
plain[pos_in_byte] := plain[pos_in_byte] xor plain_pre_8[pos_in_byte];
end else
begin
temp_ptr:=crypted_pre_8;
inc( temp_ptr,pos_in_byte);
plain[pos_in_byte] :=plain[pos_in_byte] xor (temp_ptr)^;
end;
end;
encipher(pword(@plain),pword( key),pword( crypted));
for pos_in_byte:=0 to 7 do
begin
temp_ptr:=crypted;
inc( temp_ptr,pos_in_byte);
temp_ptr^ := temp_ptr^ xor plain_pre_8[pos_in_byte];
end;
move(plain_pre_8, plain, 8);
crypted_pre_8 := crypted;
inc(crypted ,8);
count := count+8;
pos_in_byte := 0;
is_header := 0;
end;
end;
inp := instr;
while (instrlen > 0) do
begin
if (pos_in_byte < 8) then
begin
plain[pos_in_byte] := inp^;
inc(inp);
inc(pos_in_byte);
dec(instrlen);
end;
if (pos_in_byte = 8) then
begin
for pos_in_byte:=0 to 7 do
begin
if(is_header<>0) then
begin
plain[pos_in_byte] :=plain[pos_in_byte] xor plain_pre_8[pos_in_byte];
end else
begin
temp_ptr:=crypted_pre_8;
inc(temp_ptr,pos_in_byte);
plain[pos_in_byte] := plain[pos_in_byte] xor temp_ptr^;
end;
end;
encipher(pword(@plain),pword( key),pword( crypted));
for pos_in_byte:=0 to 7 do
begin
temp_ptr:=crypted ;
inc(temp_ptr,pos_in_byte );
temp_ptr^ := temp_ptr^ xor plain_pre_8[pos_in_byte];
end;
move(plain_pre_8, plain, 8);
crypted_pre_8 := crypted;
inc(crypted , 8);
count :=count+ 8;
pos_in_byte := 0;
is_header := 0;
end;
end;
padding := 1;
while (padding <= 7) do
begin
if (pos_in_byte < 8) then
begin
plain[pos_in_byte] := $0;
inc(pos_in_byte);
inc(padding) ;
end;
if (pos_in_byte = 8) then
begin
for pos_in_byte:=0 to 7 do
begin
if(is_header<>0) then
begin
plain[pos_in_byte] := plain[pos_in_byte] xor plain_pre_8[pos_in_byte];
end else
begin
temp_ptr:=crypted_pre_8;
inc(temp_ptr,pos_in_byte);
plain[pos_in_byte] := plain[pos_in_byte] xor temp_ptr^;
end;
end ;
encipher(pword(@plain),pword( key),pword( crypted));
for pos_in_byte:=0 to 7 do
begin
temp_ptr:=crypted;
inc(temp_ptr,pos_in_byte);
temp_ptr^ := temp_ptr^ xor plain_pre_8[pos_in_byte];
end ;
move(plain_pre_8, plain, 8);
crypted_pre_8 := crypted;
inc(crypted, 8);
count := count+8;
pos_in_byte := 0;
is_header := 0;
end;
end;
outstrlen_ptr^ := count;
end;
xsnbzj 2010-01-16
  • 打赏
  • 举报
回复
Learn ING
柯本 2010-01-15
  • 打赏
  • 举报
回复
大致看了一下,应该没难度,如果明天我有空且没人写的话,我可以试一下.
Dolphin_001 2010-01-15
  • 打赏
  • 举报
回复
4各月没碰delphi,发现生疏了好多
chhrsas 2010-01-15
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 sonicer 的回复:]
男女双修还差不多,这个C屁屁就不行了
[/Quote]
【文明用语】
wsxcdx 2010-01-15
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 sonicer 的回复:]
男女双修还差不多,这个C屁屁就不行了
[/Quote]
...
林石公 2010-01-15
  • 打赏
  • 举报
回复
男女双修还差不多,这个C屁屁就不行了
虎滴小猪猪 2010-01-15
  • 打赏
  • 举报
回复
我不得已双修,但我不是高手哈哈
xiaowei_001 2010-01-15
  • 打赏
  • 举报
回复
没有mfc的话编译个dll吧。这样你也能熟悉下c++
封装个dll就一会的功夫,楼主为何不试试呢!
sunnauq 2010-01-14
  • 打赏
  • 举报
回复
看着有点儿晕,帮顶,呵呵
kfcoffe 2010-01-14
  • 打赏
  • 举报
回复
mark
mjp1234airen4385 2010-01-14
  • 打赏
  • 举报
回复
呵呵回复错了。
4楼的办法是最省事的办法了。
如果改装的话,也不是很难,就是一个定义到另一个函数内部。
就好了。
变量也可以实现共享的。
但我确实是没有时间呀。
写个框架吧。
procedure qqencrypt( var instr; integer instrlen; var key;
var outstr; var int outstrlen_ptr)
var
plain: array[0..7] of uchar;
plain_pre_8: array[0..7] of uchar;
crypted, crypted_pre_8: uchar;
inp, pos_in_byte, is_header, count, padding : integer;
proecedure encrypt_every_8_byte()
begin
pos_in_byte:=0;
begin
end;
end;
麦客来了 2010-01-14
  • 打赏
  • 举报
回复
mark
mjp1234airen4385 2010-01-14
  • 打赏
  • 举报
回复
ttttt
sandyandy 2010-01-14
  • 打赏
  • 举报
回复
把这个做成dll, 用delphi来调用就行了
sanguomi 2010-01-14
  • 打赏
  • 举报
回复
你这代码本身就有问题吧

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧