寻C++、Delphi双修高手(10.01.14)

quark 2010-01-14 11:12:58
哪位高手帮忙把C翻译成delphi,多谢

static void encipher(unsigned int *const v, const unsigned int *const k, unsigned int *const w)
{
register unsigned int
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, /* do encrypt 16 (0x10) times */
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);
}

w[0] = htonl(y); w[1] = htonl(z);
}

static void decipher(unsigned int *const v, const unsigned int *const k, unsigned int *const w)
{
register unsigned int
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);
}
...全文
100 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
quark 2010-01-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sanguomi 的回复:]
翻译了一个,没测试
Delphi(Pascal) code
uses
winsock2;procedure encipher(v: PUINT;const k: PUINT; w: PUINT);var
y, Z, a, b, c,d, n, Sum, delta: UINT;
v1, k1:arrayof UINT;begin
SetLength(v1,2);
SetLength(k1,4);
Move(v^, v1[0], SizeOf(UINT)*2);
Move(k^, k1[0], SizeOf(UINT)*4);
y := ntohl(v1[0]);
z := ntohl(v1[1]);
a := ntohl(k1[0]);
b := ntohl(k1[1]);
c := ntohl(k1[2]);
d := ntohl(k1[3]);

n := $10;//*do encrypt16 (0x10) times*/
sum :=0;
delta := $9E3779B9;//* 0x9E3779B9- 0x100000000=-0x61C88647*/while (n>0)dobegin
Dec(n);
sum := sum+ delta;
y := y+ ((z shl4)+ a) xor (z+ sum) xor ((z shr5)+ b);
z := z+ ((y shl4)+ c) xor (y+ sum) xor ((y shr5)+ d);
w^ := htonl(y);
inc(w);
w^ := htonl(z);end;end;
[/Quote]

to sanguomi:

帮忙把这个也看看吧,谢了,分数送上
kfcoffe 2010-01-14
  • 打赏
  • 举报
回复
mark
sanguomi 2010-01-14
  • 打赏
  • 举报
回复
翻译了一个,没测试

uses
winsock2;

procedure encipher(v: PUINT; const k: PUINT; w: PUINT);
var
y, Z, a, b, c,d, n, Sum, delta: UINT;
v1, k1: array of UINT;
begin
SetLength(v1, 2);
SetLength(k1, 4);
Move(v^, v1[0], SizeOf(UINT) * 2);
Move(k^, k1[0], SizeOf(UINT) * 4);
y := ntohl(v1[0]);
z := ntohl(v1[1]);
a := ntohl(k1[0]);
b := ntohl(k1[1]);
c := ntohl(k1[2]);
d := ntohl(k1[3]);

n := $10; //* do encrypt 16 (0x10) times */
sum := 0;
delta := $9E3779B9; //* 0x9E3779B9 - 0x100000000 = -0x61C88647 */

while (n > 0) do
begin
Dec(n);
sum := sum + delta;
y := y + ((z shl 4) + a) xor (z + sum) xor ((z shr 5) + b);
z := z + ((y shl 4) + c) xor (y + sum) xor ((y shr 5) + d);
w^ := htonl(y);
inc(w);
w^ := htonl(z);
end;
end;
wliaoc 2010-01-14
  • 打赏
  • 举报
回复
呵呵,本人不是
帮你顶一下
quark 2010-01-14
  • 打赏
  • 举报
回复
function QQEncipher(const str, key: string): string;
var a, b, c, d, n, y, z, sum, delta: dword;
begin
y := ntohl(PLongWord(PChar(str) + 00)^);
z := ntohl(PLongWord(PChar(str) + 04)^);
a := ntohl(PLongWord(PChar(key) + 00)^);
b := ntohl(PLongWord(PChar(key) + 04)^);
c := ntohl(PLongWord(PChar(key) + 08)^);
d := ntohl(PLongWord(PChar(key) + 12)^);
n := $10; //* do encrypt 16 (0x10) times */
sum := $00;
delta := $9E3779B9; //* 0x9E3779B9 - 0x100000000 = -0x61C88647 */
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
while n > 0 do
begin
Dec(n);
sum := sum + delta;
y := y + ((z shl 4) + a) xor (z + sum) xor ((z shr 5) + b);
z := z + ((y shl 4) + c) xor (y + sum) xor ((y shr 5) + d);
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SetLength(result , 8);
PLongWord(PChar(result) + 00)^ := htonl(y);
PLongWord(PChar(result) + 04)^ := htonl(z);
end;


参考sanguomi,自己翻译了一个

16,749

社区成员

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

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