Vc++转delphi ( 积分: 200 )

  • 主题发起人 主题发起人 tanshikao
  • 开始时间 开始时间
T

tanshikao

Unregistered / Unconfirmed
GUEST, unregistred user!
int ChkData(char *data,int len)
{
char t;
int i;
for(i=0;i<len;i++){
t=toupper(data);
if(t>='0' &amp;&amp
t<='9')continue;
if(t>='A' &amp;&amp
t<='F')continue;
return 1;
}
return 0;
}
//
int __stdcall TriDESEncrypt_Hex(char *key,char *ptrSource, int msgLen, char *ptrDest)
{
int i;
BYTE len;
char *data0;
char *data1;
if(msgLen%8!=0) return(-0x87);
if(ChkData(ptrSource,msgLen*2)) return -0x88;
if(ChkData(key,32)) return -0x89;
data0 = (char *)malloc(msgLen+/*9*/msgLen+9);
if(data0==NULL) return -0x86;
data1 = (char *)malloc(msgLen+/*9*/msgLen+9);
if(data1==NULL) return -0x86;
for(i=0;i<msgLen*2;i++)data0 = ptrSource;
//
len = msgLen*2 / 16;
for (i=0;i<len;i++){
TriDES(0,data0+i*16,key,data1+i*16);
}
memcpy(ptrDest,data1,msgLen*2);
free(data0);
free(data1);
return 0;
}
怎么把这段VC转换成delphi代码,请各位大侠帮忙!谢谢!
 
function ChkData(data: PChar
len :Integer):Integer;
var
t: char;
I: Integer;
function toupper(const chr: char):char;
begin
Result := chr;
if (chr >= 'a') and (chr <= 'z') then
Dec(Result, $20);
end;
begin
Result := 0;
for I := 0 to len - 1 do
begin
t := toupper(data);
if (t >= '0') and (t <= '9') then
continue;
if (t >= 'A') and (t <= 'F') then
continue;
Result := 1;
break;
end;
end;

function TriDESEncrypt_Hex(key: PChar
ptrSource: PChar
msglen: Integer
ptrDest: PChar):Integer;stdcall;
var
I: Integer;
len: Byte;
data0: PChar;
data1: PChar;
begin
Result := -$87;
if msglen mod 8 <> 0 then
Exit;
Result := -$88;
if ChkData(ptrSource, msgLen*2) <> 0 then
Exit;
Result := -$89;
if ChkData(key,32) <> 0 then
Exit;
Result := -$86;
data0 := Allocmem(msgLen + msgLen + 9);
if data0 = nil then
Exit;
data1 := Allocmem(msgLen + msgLen + 9);
if data1 = nil then
Exit;
for I := 0 to msgLen*2 - 1 do
data0 := ptrSource;
len := msgLen * 2 div 16;
for I :=0 to len - 1 do
TriDES(0, Pointer(Integer(data0) + I * 16), key, Pointer(Integer(data1) + I * 16));
Move(data1^, ptrDest^, msglen * 2);
Freemem(data0);
Freemem(data1);
Result := 0;
end;
 
谢谢!虽然问题没有解决,但还是给你加分吧
 
我给你转的不对吗?
 
其实我要转的不止这些,还有很多,所以我现在也不知道对不对,不好意思!
void str2hex(BYTE *str, char *hex,int strlen)
{
int i;
char ddl,ddh;
//hex[strlen*2] = 0;
for (i=0;i<strlen;i++){
ddh = 48 + str / 16;
ddl = 48 + str % 16;
if (ddh > 57) ddh = ddh + 7;
if (ddl > 57) ddl = ddl + 7;
hex[i*2] = ddh;
hex[i*2+1] = ddl;

}
}
//
void hex2str(char *hex, BYTE *str,int strlen)
{
char h1,h2;
BYTE i,s1,s2;
for (i=0;i<strlen;i++){
h1 = hex[2*i];
h2 = hex[2*i+1];
s1 = toupper(h1) - 0x30;
if (s1 > 9) s1 -= 7;
s2 = toupper(h2) - 0x30;
if (s2 > 9) s2 -= 7;
str = s1*16 + s2;
}
//str[strlen]='/0';
}

void tonot(char *hh, char *dd)
{
BYTE i,s1[8],s2[2];

hex2str(hh,s1,8);
for (i=0;i<8;i++) s2 = ~s1;
str2hex(s2,dd,8);
}
/**/
void str_xor(BYTE *src, BYTE *dst, int len)
{
int i;

for (i=0
i<len
i++)
dst = (src + dst) % 2;
}

void str_cpy(BYTE *src, BYTE *dst, int len)
{
int i;

for (i=0
i<len
i++)
dst = src;
}

void circle(BYTE *str, int leng) // to complete left circel shift
{ // 1 bit per time
BYTE tmp;
int i;
tmp = str[0];
for (i=0
i<leng-1
i++)
str = str[i+1];
str[leng-1] = tmp;
}

void transfer(BYTE *src, BYTE *dst, BYTE *table)
{
int table_leng, i;
table_leng = 0;
while (table[table_leng] != 255)
table_leng++;
for (i=0
i<table_leng
i++) {
dst = src[table];
}
}

void key_generate(BYTE *key_in, BYTE *key_sub, int cnt)
{
BYTE buffer[56];
BYTE C0[28], D0[28];
int i;
BYTE shift[] = {
1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28,};
BYTE PC_1[] = {
56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17,
9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35,
62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21,
13, 5, 60, 52, 44, 36, 28, 20, 12, 4, 27, 19, 11, 3,
255};
BYTE PC_2[] = {
13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9, 22, 18, 11, 3,
25, 7, 15, 6, 26, 19, 12, 1, 40, 51, 30, 36, 46, 54, 29, 39,
50, 44, 32, 47, 43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31,
255};
transfer(key_in, buffer, PC_1);
for (i=0
i<28
i++) {
C0 = buffer;
D0 = buffer[i+28];
}
for (i=0
i<shift[cnt]
i++) {
circle(C0, 28);
circle(D0, 28);
}
for (i=0
i<28
i++) {
buffer = C0;
buffer[i+28] = D0;
}
transfer(buffer, key_sub, PC_2);
}

void S_change(BYTE *str)
{
BYTE src[8][6], dst[8][4];
int i, j, adr;
BYTE S[8][64] = {
{14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
},

{15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
},

{10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
},

{ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
},

{ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
},

{12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
},
{ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
},

{13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
}};

for (i=0
i<8
i++)
for (j=0
j<6
j++)
src[j] = str[i*6+j]; // seperate to 8 parts

for (i=0
i<8
i++) {
j = src[1]*8+src[2]*4+src[3]*2+src[4];
adr = (src[0]*2+src[5])*16 + j;
j = S[adr];
dst[0] = j / 8;
j %= 8;
dst[1] = j / 4;
j %= 4;
dst[2] = j / 2;
dst[3] = j % 2
// tansfer 6 bits to 4 bits per part
}

for (i=0
i<8
i++)
for (j=0
j<4
j++)
str[i*4+j] = dst[j]
// link them to 32 bits
}

void des_algo(BYTE *src, BYTE *dst, BYTE *key, int flag)
{
BYTE sub_key[48], tmp[32], buffer[48];//, cal_tmp[32];
BYTE left[32], right[32];
int i;
BYTE IP[] = {
57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7,
56, 48, 40, 32, 24, 16, 8, 0, 58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6,
255};
BYTE IP_1[] = {
39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25, 32, 0, 40, 8, 48, 16, 56, 24,
255};
BYTE E[] = {
31, 0, 1, 2, 3, 4, 3, 4, 5, 6, 7, 8, 7, 8, 9, 10,
11, 12, 11, 12, 13, 14, 15, 16, 15, 16, 17, 18, 19, 20, 19, 20,
21, 22, 23, 24, 23, 24, 25, 26, 27, 28, 27, 28, 29, 30, 31, 0,
255};
BYTE P[] = {
15, 6, 19, 20, 28, 11, 27, 16, 0, 14, 22, 25, 4, 17, 30, 9,
1, 7, 23, 13, 31, 26, 2, 8, 18, 12, 29, 5, 21, 10, 3, 24,
255};

transfer(src, dst, IP);

for (i=0
i<32
i++) {
left = dst;
right = dst[i+32]; // generate L0, R0
}

for (i=0
i<16
i++) { // the main circle
if (flag == 0)
key_generate(key, sub_key, i)
// generate the sub key
else // flag = 0 is encypher
1 is decypher
key_generate(key, sub_key, 15-i);
str_cpy(right, tmp, 32); // save RIGHT_half to tmp

transfer(right, buffer, E); // R(i-1) from 32 bit to 48 bit
str_xor(sub_key, buffer, 48); // then XOR with k(i)
S_change(buffer)
// change result with S-table
transfer(buffer, right, P); // transfer using P-table ==> f

str_xor(left, right, 32); // generate R(i)
str_cpy(tmp, left, 32); // generate L(i)
}

for (i=0
i<32
i++) {
src = right;
src[32+i] = left;
} // generate source(R[15]L[15])

transfer(src, dst, IP_1); // get result in dst
}

void dtob(BYTE dd,BYTE *hh)
{
int i;

for (i=0;i<8;i++){
hh = 0;
if (dd > 0x7F) hh = 1;
dd = dd << 1;
}
}
//
void des0(int argc, BYTE *src, BYTE *key, BYTE *result)
{
BYTE source[64], dest[64], key_main[64];
int flag, i, j;

if (argc == 0) flag = 0;
else flag = 1 ;

for (i=0
i<8
i++){
dtob(src,source+i*8);
dtob(key,key_main+i*8);
}
des_algo(source, dest, key_main, flag);
for (i=0
i<8
i++){
result = 0;
for (j=0;j<8;j++) result |= (1<<(7-j))*dest[8*i+j];
}
}
//
void trides0(int argc,BYTE *src,BYTE *key,BYTE *result)
{
BYTE src0[8],key0[8];
int i,flag;
BYTE data[8];
memcpy(data,result,8);
if (argc == 0)
flag=0;
else
flag=1;
for (i=0;i<8;i++){
src0 = src;
key0 = key;
}
des0(flag,src0,key0,data);

flag ^= 0x0001;
for (i=0;i<8;i++){
src0 = data;
key0 = key[i+8];
}
des0(flag,src0,key0,data);

flag ^= 0x0001;
for (i=0;i<8;i++){
src0 = data;
key0 = key;
}
des0(flag,src0,key0,data);
memcpy(result,data,8);
}
///////////////////////////////////////////////////////////////////////////////
//
void DES(int mode, char *data, char *key, char *result)
{
BYTE Sou[8],Key[8],Res[8],resu[16];
hex2str(data,Sou,8);
hex2str(key,Key,8);
des0(mode,Sou,Key,Res);
str2hex(Res,(char*)resu,8);
memcpy(result,resu,16);
}
//
void TriDES(int mode, char *data, char *key, char *result)
{
BYTE Sou[8],Key[16],Res[8];
hex2str(data,Sou,8);
hex2str(key,Key,16);
trides0(mode,Sou,Key,Res);
str2hex(Res,result,8);
}
void SMAC(BYTE *data,int length,BYTE *key,BYTE *rand,BYTE *mac)
{
int i,j,len;
BYTE sou[8];

//for (i=0;i<8;i++) mac = 0x0;
//for (i=4;i<8;i++) mac = 0x0;

for (i=0;i<4;i++) mac = rand;
for (i=4;i<8;i++) mac = 0;


data[length] = 0x80;
for (i=length+1;i<length+8;i++)
data = 0;
len = length / 8 +1;
for (i=0;i<len;i++)
{
for (j=0;j<8;j++) sou[j] = mac[j] ^ data[i*8+j];
des0(0,sou,key,mac);
}

/* for (j=0;j<8;j++) sou[j] = mac[j];
des0(1,sou,key+8,mac);

for (j=0;j<8;j++) sou[j] = mac[j];
des0(0,sou,key,mac);*/

}
void MAC(BYTE *data,int length,BYTE *key,/*BYTE *rand,*/BYTE *mac)
{
int i,j,len;
BYTE sou[8];

for (i=0;i<8;i++) mac = 0x0;
for (i=4;i<8;i++) mac = 0x0;

//for (i=0;i<4;i++) mac = rand;
//for (i=4;i<8;i++) mac = 0;


data[length] = 0x80;
for (i=length+1;i<length+8;i++)
data = 0;
len = length / 8 +1;
for (i=0;i<len;i++)
{
for (j=0;j<8;j++) sou[j] = mac[j] ^ data[i*8+j];
des0(0,sou,key,mac);
}

for (j=0;j<8;j++) sou[j] = mac[j];
des0(1,sou,key+8,mac);

for (j=0;j<8;j++) sou[j] = mac[j];
des0(0,sou,key,mac);

}

void cMAC(BYTE *data, int length, BYTE *key, BYTE *rand, BYTE *mac)
{
int i,j,len;
BYTE sou[8];

for (i=0;i<4;i++) mac = rand;
for (i=4;i<8;i++) mac = 0;

//for (i=0;i<8;i++) mac = rand;


data[length] = 0x80;
for (i=length+1;i<length+8;i++) data = 0;
len = length / 8 +1;

for (i=0;i<len;i++)
{
for (j=0;j<8;j++)
sou[j] = mac[j] ^ data[i*8+j];
des0(0,sou,key,mac);
}
for (j=0;j<8;j++) sou[j] = mac[j];
des0(1,sou,key+8,mac);
for (j=0;j<8;j++) sou[j] = mac[j];
des0(0,sou,key,mac);
}


//
BYTE Encrypt(BYTE *data, int length, BYTE *key)
{
int i;
BYTE len;
BYTE *data0= (BYTE *)malloc(length+9)
//new BYTE [length+9];
data0[0] = (BYTE) length;
for (i=0;i<length;i++)
data0[i+1] = data;
length = length + 1;
data0[length] = 0x80;
for (i=length+1;i<length+8;i++) data0 = 0;
len = length / 8 +1;
for (i=0;i<len;i++) {
des0(0,data0+i*8,key,data+i*8);
}
return len*8;


/*
int i;
BYTE len;
BYTE *data0= (BYTE *)malloc(length+9)
//new BYTE [length+9];
data0[0] = (BYTE) length;
for (i=0;i<length;i++)
data0[i+1] = data;
length = length + 1;
data0[length] = 0x80;
for (i=length+1;i<length+8;i++) data0 = 0;
len = length / 8 +1;
for (i=0;i<len;i++) {
des0(0,data0+i*8,key,data+i*8);
}
return len*8;

*/
}


////////////////

BYTE Decrypt(BYTE *data,int length,BYTE *key)
{
int i;
BYTE len;
BYTE *data0= (BYTE *)malloc(length+9)
//new BYTE [length+9];
//data0[0] = (BYTE) length;
for (i=0;i<length;i++) data0 = data;
//length = length + 1;
//data0[length] = 0x80;
//for (i=length+1;i<length+8;i++) data0 = 0;
len = length / 8;//+1;
for (i=0;i<len;i++) {
des0(1,data0+i*8,key,data+i*8);
}
return data[0];

}


//
//
//
//
int ChkData(char *data,int len)
{
char t;
int i;
for(i=0;i<len;i++){
t=toupper(data);
if(t>='0' &amp;&amp
t<='9')continue;
if(t>='A' &amp;&amp
t<='F')continue;
return 1;
}
return 0;
}



//
int __stdcall TriDESEncrypt_Hex(char *key,char *ptrSource, int msgLen, char *ptrDest)
{
int i;
BYTE len;
char *data0;
char *data1;
if(msgLen%8!=0) return(-0x87);
if(ChkData(ptrSource,msgLen*2)) return -0x88;
if(ChkData(key,32)) return -0x89;
data0 = (char *)malloc(msgLen+/*9*/msgLen+9);
if(data0==NULL) return -0x86;
data1 = (char *)malloc(msgLen+/*9*/msgLen+9);
if(data1==NULL) return -0x86;
for(i=0;i<msgLen*2;i++)data0 = ptrSource;
//
len = msgLen*2 / 16;
for (i=0;i<len;i++){
TriDES(0,data0+i*16,key,data1+i*16);
}
memcpy(ptrDest,data1,msgLen*2);
free(data0);
free(data1);
return 0;
}
//
int __stdcall TriDESDecrypt_Hex(char *key,char *ptrSource, int msgLen, char *ptrDest)
{
int i;
BYTE len;
char *data0;
char *data1;
if(msgLen%8!=0) return(-0x87);
if(ChkData(ptrSource,msgLen*2)) return -0x88;
if(ChkData(key,32)) return -0x89;
data0 = (char *)malloc(msgLen+msgLen+9);
if(data0==NULL) return -0x86;
data1 = (char *)malloc(msgLen+msgLen+9);
if(data1==NULL) return -0x86;
for(i=0;i<msgLen*2;i++)data0 = ptrSource;
//
len = msgLen*2 / 16;
for (i=0;i<len;i++) {
TriDES(1,data0+i*16,key,data1+i*16);
}
memcpy(ptrDest,data1,msgLen*2);
free(data0);
free(data1);
return 0;
}
//


int __stdcall DESEncrypt_Hex(char *key,char *ptrSource, int msgLen, char *ptrDest)
{
int i;
BYTE len;
char *data0;
char *data1;
if(msgLen%8!=0) return(-0x87);
if(ChkData(ptrSource,msgLen*2)) return -0x88;
if(ChkData(key,16)) return -0x89;
data0 = (char *)malloc(msgLen+9);
if(data0==NULL) return -0x86;
data1 = (char *)malloc(msgLen+9);
if(data1==NULL) return -0x86;
for(i=0;i<msgLen*2;i++)data0 = ptrSource;
//
len = msgLen*2 / 16;
for (i=0;i<len;i++){
DES(0,data0+i*16,key,data1+i*16);
}
memcpy(ptrDest,data1,msgLen*2);
free(data0);
free(data1);
return 0;
}
//
int __stdcall DESDecrypt_Hex(char *key,char *ptrSource, int msgLen, char *ptrDest)
{
int i;
BYTE len;
char *data0;
char *data1;
if(msgLen%8!=0) return(-0x87);
if(ChkData(ptrSource,msgLen*2)) return -0x88;
if(ChkData(key,16)) return -0x89;
data0 = (char *)malloc(msgLen+9);
if(data0==NULL) return -0x86;
data1 = (char *)malloc(msgLen+9);
if(data1==NULL) return -0x86;
for(i=0;i<msgLen*2;i++)data0 = ptrSource;
//
len = msgLen*2 / 16;
for (i=0;i<len;i++) {
DES(1,data0+i*16,key,data1+i*16);
}
memcpy(ptrDest,data1,msgLen*2);
free(data0);
free(data1);
return 0;
}
 
再帮帮把全部的转过来,好嘛?谢谢!
 
接受答案了.
再帮帮把全部的转过来,好嘛?谢谢!
 
后退
顶部