VC++ or C++ Builder 的dll 其中有些函数delphi调用时,IDE环境正常,但执行EXE出错?(100分)

  • 主题发起人 主题发起人 wangxq01
  • 开始时间 开始时间
W

wangxq01

Unregistered / Unconfirmed
GUEST, unregistred user!
本人将以前在c语言环境使用的des加,解密函数移植到C++ Builder 中制作成dll以便供
delphi调用.下面是其原码.编译后使用C++ Builder 进行调用一切正常.而使用delphi进行
调用时在开发环境编译,运行正常但退出开发环境执行生成的exe文件却出错.出错信息:"
Access violation at address 00CB1352 in moudle 'test.dll'.write of address
00440498." . 另外dll中还有一个测试函数调用正常. 请各位高手指点迷津.
C++ Builder dll 原码:
//---------------------------------------------------------------------------

#include <windows.h>
#include <stdio.h>
#include <malloc.h>
#include <fcntl.h>
#ifndef _SEC_SDES_ALGO_H_
#define _SEC_SDES_ALGO_H_

#define SEC_SDES_OK 0
#define SEC_SDES_ERR -999

#endif


#pragma argsused


int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
{
return 1;
}
extern "C" __declspec(dllexport) __stdcall void SEC_SDES_Encrypt( unsigned char *,
unsigned char *,
unsigned char * );
extern "C" __declspec(dllexport) __stdcall void SEC_SDES_Decrypt( unsigned char *,
unsigned char *,
unsigned char * );
//----------------------------------------------------
double dblValue(double);
double halfValue(double);
extern "C" __declspec(dllexport) __stdcall double changeValue(double,bool);
//extern "C" __declspec(dllexport) __stdcall void LC();
double dblValue(double value)
{
return value*value;
}
double halfValue(double value)
{
return value/2.0;
}
double __stdcall changeValue(double value,bool whichOp)
{
return whichOp ? dblValue(value) : halfValue(value);
}

//---------------------------------------------------------------------------

/* sec_sdes_algo.c
**
** 本文件提供了DES算法的基本功能的函数.
**
*/

char sbox1[] =
{
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
};


char sbox2[] =
{
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
};




char sbox3[] =
{
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
};




char sbox4[] =
{
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
};




char sbox5[] =
{
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
};




char sbox6[] =
{
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
};





char sbox7[] =
{
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
};




char sbox8[] =
{
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
};



char *sbox[] =
{
sbox1,
sbox2,
sbox3,
sbox4,
sbox5,
sbox6,
sbox7,
sbox8
};


/*
** for initial permutation
*/
char table1[] =
{
58,50,42,34,26,18,10, 2,60,52,44,36,28,20,12, 4,
62,54,46,38,30,22,14, 6,64,56,48,40,32,24,16, 8,
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
};



/*
** for permutation choice 1
*/
char table2[] =
{
57,49,41,33,25,17, 9, 1,58,50,42,34,26,18,
10, 2,59,51,43,35,27,19,11, 3,60,52,44,36,
63,55,47,39,31,23,15, 7,62,54,46,38,30,22,
14, 6,61,53,45,37,29,21,13, 5,28,20,12, 4
};



/*
** for expansion function
*/
char table3[] =
{
32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9,10,11,
12,13,12,13,14,15,16,17,16,17,18,19,20,21,20,21,
22,23,24,25,24,25,26,27,28,29,28,29,30,31,32, 1
};



/*
** for permutation choice 2
*/
char table4[] =
{
14,17,11,24, 1, 5, 3,28,15, 6,21,10,23,19,12, 4,
26, 8,16, 7,27,20,13, 2,41,52,31,37,47,55,30,40,
51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32
};




/*
** for inverse of initial permutation
*/
char table5[] =
{
40, 8,48,16,56,24,64,32,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
};




/*
** for permutation function P
*/
char table6[] =
{
16, 7,20,21,29,12,28,17, 1,15,23,26, 5,18,31,10,
2, 8,24,14,32,27, 3, 9,19,13,30, 6,22,11, 4,25
};




/*
** for selection function
*/
char table7[] =
{
1, 6, 2, 1, 7,12, 8, 5,13,18,14, 9,19,24,20,13,
25,30,26,17,31,36,32,21,37,42,38,25,43,48,44,29
};



/*
** for shift left 1
*/
char table8[] =
{
2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,
18,19,20,21,22,23,24,25,26,27,28, 1,30,31,32,33,
34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,
50,51,52,53,54,55,56,29
};




/*
** for shift left 2
*/
char table9[] =
{
3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,
19,20,21,22,23,24,25,26,27,28, 1, 2,31,32,33,34,
35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,
51,52,53,54,55,56,29,30
};



/*
** for shift right 1
*/
char table10[] =
{
28, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,
16,17,18,19,20,21,22,23,24,25,26,27,56,29,30,31,
32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
48,49,50,51,52,53,54,55
};




/*
** for shift right 2
*/
char table11[] =
{
27,28, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
15,16,17,18,19,20,21,22,23,24,25,26,55,56,29,30,
31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,
47,48,49,50,51,52,53,54
};





/*
** content is table index,total count
*/
char *table[] =
{
table1,table2,table3,table4, table5,table6,
table7,table8,table9,table10,table11
};



unsigned char initial_permutation[] = { 0, 64 };
unsigned char permutation_c1[] = { 1, 56 };
unsigned char expansion_function[] = { 2, 48 };
unsigned char permutation_c2[] = { 3, 48 };
unsigned char inverse_ip[] = { 4, 64 };
unsigned char permutation_fp[] = { 5, 32 };
unsigned char shift_left1[] = { 7, 56 };
unsigned char shift_left2[] = { 8, 56 };
unsigned char shift_right1[] = { 9, 56 };
unsigned char shift_right2[] = { 10, 56 };
unsigned char x64[8],i64[8],k64[8],k56[7],k48[6],l32[4],r48[6],r32[4];





void transdata( unsigned char *input,
unsigned char *output,
unsigned char *function )
{
int i;
unsigned char out[8];

/*
** initial value with 0
*/
for ( i=0
i<(int)(function[1]>>3)
i++ )
{
out=0;
}

for ( i=0
i<(int)function[1]
i++ )
{
out[i>>3] |= ( unsigned char )( ( input[table[function[0]]-1 >> 3]
&amp
( 0x80 >> ( table[function[0]]-1 &amp
0x7 ) ) )
<< ( table[function[0]]-1 &amp
0x7 ) ) >> (i&amp;0x7);
}

/*
** initial value with 0
*/
for ( i=0
i<(int)(function[1]>>3)
i++ )
{
output=out;
}
}




void select( unsigned char *input, unsigned char *output )
{
int idx,i,j,k,cnt;

/*
** initial value with 0
*/
for ( i=0
i<4
i++ )
{
output = 0;
}

cnt = 0;
for ( i=0
i<8
i++ )
{
for ( j=5, k=0, idx=0
j>=0
j-- )
{
idx+=((input[table7[cnt]-1+k>>3] &amp
(0x80>>(table7[cnt]-1+k &amp
0x7)))
? 1 <<j : 0);

if ( j>3 )
{
cnt++;
}
else
{
k++;
}
}

cnt++;
for ( j=0
j<4
j++ )
{
output[table7[cnt]+j-1 >> 3] |= ((sbox[idx] &amp
(0x08 >> j)) << 4+j)
>> (table7[cnt]+j-1 &amp
0x7);
}
cnt++;
}
}



/*
** io->r32
** input->k56
*/
void fnct( unsigned char *io, unsigned char *input )
{
unsigned char k48[6],r48[6],s32[4];
int i;

/*
** 28+28 to 48
*/
transdata( input, k48, permutation_c2 )


/*
** 32 to 48
*/
transdata( io, r48, expansion_function )


/*
** expn xor key
*/
for ( i=0
i<6
i++ )
{
r48 ^= k48;
}

select( r48, s32 );

transdata( s32, io, permutation_fp );
}



/*
** input->x64
** key->k64
** output->i64
*/
void __stdcall SEC_SDES_Encrypt( unsigned char *input,
unsigned char *key,
unsigned char *output )
{
unsigned char i64[8],k56[7],t32[4];

transdata( input, i64, initial_permutation );
transdata( key, k56, permutation_c1 );

/*
** shift left 1
*/
transdata( k56, k56, shift_left1 );

t32[0] = i64[4]

t32[1] = i64[5]

t32[2] = i64[6]

t32[3] = i64[7];

fnct( i64+4, k56 );

i64[4] ^= i64[0]

i64[5] ^= i64[1]

i64[6] ^= i64[2]

i64[7] ^= i64[3];
i64[0] = t32[0]

i64[1] = t32[1]

i64[2] = t32[2]

i64[3] = t32[3];

/*
** shift left 1
*/
transdata( k56, k56, shift_left1 );

t32[0] = i64[4]

t32[1] = i64[5]

t32[2] = i64[6]

t32[3] = i64[7];

fnct( i64+4, k56 );

i64[4] ^= i64[0]

i64[5] ^= i64[1]

i64[6] ^= i64[2]

i64[7] ^= i64[3];
i64[0] = t32[0]

i64[1] = t32[1]

i64[2] = t32[2]

i64[3] = t32[3];

/*
** shift left 2
*/
transdata(k56,k56,shift_left2);

t32[0] = i64[4]

t32[1] = i64[5]

t32[2] = i64[6]

t32[3] = i64[7];

fnct( i64+4, k56 );

i64[4] ^= i64[0]

i64[5] ^= i64[1]

i64[6] ^= i64[2]

i64[7] ^= i64[3];
i64[0] = t32[0]

i64[1] = t32[1]

i64[2] = t32[2]

i64[3] = t32[3];

/*
** shift left 2
*/
transdata( k56, k56, shift_left2 );

t32[0] = i64[4]

t32[1] = i64[5]

t32[2] = i64[6]

t32[3] = i64[7];

fnct(i64+4,k56);

i64[4] ^= i64[0]

i64[5] ^= i64[1]

i64[6] ^= i64[2]

i64[7] ^= i64[3];
i64[0] = t32[0]

i64[1] = t32[1]

i64[2] = t32[2]

i64[3] = t32[3];

/*
** shift left 2
*/
transdata( k56, k56, shift_left2 );

t32[0] = i64[4]

t32[1] = i64[5]

t32[2] = i64[6]

t32[3] = i64[7];

fnct( i64+4, k56 );

i64[4] ^= i64[0]

i64[5] ^= i64[1]

i64[6] ^= i64[2]

i64[7] ^= i64[3];
i64[0] = t32[0]

i64[1] = t32[1]

i64[2] = t32[2]

i64[3] = t32[3];

/*
** shift left 2
*/
transdata( k56, k56, shift_left2 );

t32[0] = i64[4]

t32[1] = i64[5];
t32[2] = i64[6];
t32[3] = i64[7];

fnct( i64+4, k56 );

i64[4] ^= i64[0]

i64[5] ^= i64[1]

i64[6] ^= i64[2]

i64[7] ^= i64[3];
i64[0] = t32[0]

i64[1] = t32[1]

i64[2] = t32[2]

i64[3] = t32[3];

/*
** shift left 2
*/
transdata( k56, k56, shift_left2 );

t32[0] = i64[4];
t32[1] = i64[5];
t32[2] = i64[6];
t32[3] = i64[7];

fnct(i64+4,k56);

i64[4] ^= i64[0];
i64[5] ^= i64[1];
i64[6] ^= i64[2];
i64[7] ^= i64[3];
i64[0] = t32[0];
i64[1] = t32[1];
i64[2] = t32[2];
i64[3] = t32[3];

/*
** shift left 2
*/
transdata( k56, k56, shift_left2 );

t32[0] = i64[4]

t32[1] = i64[5]

t32[2] = i64[6]

t32[3] = i64[7];

fnct( i64+4, k56 );

i64[4] ^= i64[0]

i64[5] ^= i64[1]

i64[6] ^= i64[2]

i64[7] ^= i64[3];
i64[0] = t32[0]

i64[1] = t32[1]

i64[2] = t32[2]

i64[3] = t32[3];

/*
** shift left 1
*/
transdata( k56, k56, shift_left1 );

t32[0] = i64[4];
t32[1] = i64[5];
t32[2] = i64[6];
t32[3] = i64[7];

fnct( i64+4, k56 );

i64[4] ^= i64[0];
i64[5] ^= i64[1];
i64[6] ^= i64[2];
i64[7] ^= i64[3];
i64[0] = t32[0];
i64[1] = t32[1];
i64[2] = t32[2];
i64[3] = t32[3];

/*
** shift left 2
*/
transdata( k56, k56, shift_left2 );

t32[0] = i64[4];
t32[1] = i64[5];
t32[2] = i64[6];
t32[3] = i64[7];

fnct( i64+4, k56 );

i64[4] ^= i64[0];
i64[5] ^= i64[1];
i64[6] ^= i64[2];
i64[7] ^= i64[3];
i64[0] = t32[0];
i64[1] = t32[1];
i64[2] = t32[2];
i64[3] = t32[3];

/*
** shift left 2
*/
transdata( k56, k56, shift_left2 );

t32[0] = i64[4];
t32[1] = i64[5];
t32[2] = i64[6];
t32[3] = i64[7];

fnct( i64+4, k56 );

i64[4] ^= i64[0];
i64[5] ^= i64[1];
i64[6] ^= i64[2];
i64[7] ^= i64[3];
i64[0] = t32[0];
i64[1] = t32[1];
i64[2] = t32[2];
i64[3] = t32[3];

/*
** shift left 2
*/
transdata( k56, k56, shift_left2 );

t32[0] = i64[4];
t32[1] = i64[5];
t32[2] = i64[6];
t32[3] = i64[7];

fnct( i64+4, k56 );

i64[4] ^= i64[0];
i64[5] ^= i64[1];
i64[6] ^= i64[2];
i64[7] ^= i64[3];
i64[0] = t32[0];
i64[1] = t32[1];
i64[2] = t32[2];
i64[3] = t32[3];

/*
** shift left 2
*/
transdata( k56, k56, shift_left2 );

t32[0] = i64[4];
t32[1] = i64[5];
t32[2] = i64[6];
t32[3] = i64[7];

fnct( i64+4, k56 );

i64[4] ^= i64[0];
i64[5] ^= i64[1];
i64[6] ^= i64[2];
i64[7] ^= i64[3];
i64[0] = t32[0];
i64[1] = t32[1];
i64[2] = t32[2];
i64[3] = t32[3];

/*
** shift left 2
*/
transdata( k56, k56, shift_left2 );

t32[0] = i64[4];
t32[1] = i64[5];
t32[2] = i64[6];
t32[3] = i64[7];

fnct( i64+4, k56 );

i64[4] ^= i64[0];
i64[5] ^= i64[1];
i64[6] ^= i64[2];
i64[7] ^= i64[3];
i64[0] = t32[0];
i64[1] = t32[1];
i64[2] = t32[2];
i64[3] = t32[3];

/*
** shift left 2
*/
transdata( k56, k56, shift_left2 );

t32[0] = i64[4];
t32[1] = i64[5];
t32[2] = i64[6];
t32[3] = i64[7];

fnct( i64+4, k56 );

i64[4] ^= i64[0];
i64[5] ^= i64[1];
i64[6] ^= i64[2];
i64[7] ^= i64[3];
i64[0] = t32[0];
i64[1] = t32[1];
i64[2] = t32[2];
i64[3] = t32[3];

/*
** shift left 1
*/
transdata( k56, k56, shift_left1 );

t32[0] = i64[4];
t32[1] = i64[5];
t32[2] = i64[6];
t32[3] = i64[7];

fnct( i64+4, k56 );

i64[4] ^= i64[0];
i64[5] ^= i64[1];
i64[6] ^= i64[2];
i64[7] ^= i64[3];
i64[0] = t32[0];
i64[1] = t32[1];
i64[2] = t32[2];
i64[3] = t32[3];

/*
** substitute
*/
t32[0] = i64[4];
t32[1] = i64[5];
t32[2] = i64[6];
t32[3] = i64[7];

i64[4] = i64[0];
i64[5] = i64[1];
i64[6] = i64[2];
i64[7] = i64[3];

i64[0] = t32[0];
i64[1] = t32[1];
i64[2] = t32[2];
i64[3] = t32[3];

transdata( i64, output, inverse_ip );
}




/*
** input->x64
** key->k64
** output->i64
*/
void __stdcall SEC_SDES_Decrypt( unsigned char *input,
unsigned char *key,
unsigned char *output )
{
unsigned char i64[8],k56[7],t32[4];

transdata( input, i64, initial_permutation );

/*
** substitute
*/
t32[0] = i64[4];
t32[1] = i64[5];
t32[2] = i64[6];
t32[3] = i64[7];

i64[4] = i64[0];
i64[5] = i64[1];
i64[6] = i64[2];
i64[7] = i64[3];

i64[0] = t32[0];
i64[1] = t32[1];
i64[2] = t32[2];
i64[3] = t32[3];

transdata( key, k56, permutation_c1 );

/*
** shift right 1
*/
t32[0] = i64[0];
t32[1] = i64[1];
t32[2] = i64[2];
t32[3] = i64[3];

fnct( i64, k56 );

i64[0] ^= i64[4];
i64[1] ^= i64[5];
i64[2] ^= i64[6];
i64[3] ^= i64[7];
i64[4] = t32[0];
i64[5] = t32[1];
i64[6] = t32[2];
i64[7] = t32[3];

transdata( k56, k56, shift_right1 );

/*
** shift right 2
*/
t32[0] = i64[0];
t32[1] = i64[1];
t32[2] = i64[2];
t32[3] = i64[3];

fnct( i64, k56 );

i64[0] ^= i64[4];
i64[1] ^= i64[5];
i64[2] ^= i64[6];
i64[3] ^= i64[7];
i64[4] = t32[0];
i64[5] = t32[1];
i64[6] = t32[2];
i64[7] = t32[3];

transdata( k56, k56, shift_right2 );

/*
** shift right 2
*/
t32[0] = i64[0];
t32[1] = i64[1];
t32[2] = i64[2];
t32[3] = i64[3];

fnct( i64, k56 );

i64[0] ^= i64[4];
i64[1] ^= i64[5];
i64[2] ^= i64[6];
i64[3] ^= i64[7];
i64[4] = t32[0];
i64[5] = t32[1];
i64[6] = t32[2];
i64[7] = t32[3];

transdata( k56, k56, shift_right2 );

/*
** shift right 2
*/
t32[0] = i64[0];
t32[1] = i64[1];
t32[2] = i64[2];
t32[3] = i64[3];

fnct( i64, k56 );

i64[0] ^= i64[4];
i64[1] ^= i64[5];
i64[2] ^= i64[6];
i64[3] ^= i64[7];
i64[4] = t32[0];
i64[5] = t32[1];
i64[6] = t32[2];
i64[7] = t32[3];

transdata( k56, k56, shift_right2 );

/*
** shift right 2
*/
t32[0] = i64[0];
t32[1] = i64[1];
t32[2] = i64[2];
t32[3] = i64[3];

fnct( i64, k56 );

i64[0] ^= i64[4];
i64[1] ^= i64[5];
i64[2] ^= i64[6];
i64[3] ^= i64[7];
i64[4] = t32[0];
i64[5] = t32[1];
i64[6] = t32[2];
i64[7] = t32[3];

transdata( k56, k56, shift_right2 );

/*
** shift right 2
*/
t32[0] = i64[0];
t32[1] = i64[1];
t32[2] = i64[2];
t32[3] = i64[3];

fnct( i64, k56 );

i64[0] ^= i64[4];
i64[1] ^= i64[5];
i64[2] ^= i64[6];
i64[3] ^= i64[7];
i64[4] = t32[0];
i64[5] = t32[1];
i64[6] = t32[2];
i64[7] = t32[3];

transdata( k56, k56, shift_right2 );

/*
** shift right 2
*/
t32[0] = i64[0];
t32[1] = i64[1];
t32[2] = i64[2];
t32[3] = i64[3];

fnct( i64, k56 );

i64[0] ^= i64[4];
i64[1] ^= i64[5];
i64[2] ^= i64[6];
i64[3] ^= i64[7];
i64[4] = t32[0];
i64[5] = t32[1];
i64[6] = t32[2];
i64[7] = t32[3];

transdata( k56, k56, shift_right2 );

/*
** shift right 1
*/
t32[0] = i64[0];
t32[1] = i64[1];
t32[2] = i64[2];
t32[3] = i64[3];

fnct(i64,k56);

i64[0] ^= i64[4];
i64[1] ^= i64[5];
i64[2] ^= i64[6];
i64[3] ^= i64[7];
i64[4] = t32[0];
i64[5] = t32[1];
i64[6] = t32[2];
i64[7] = t32[3];

transdata( k56, k56, shift_right1 );

/*
** shift right 2
*/
t32[0] = i64[0];
t32[1] = i64[1];
t32[2] = i64[2];
t32[3] = i64[3];

fnct( i64, k56 );

i64[0] ^= i64[4];
i64[1] ^= i64[5];
i64[2] ^= i64[6];
i64[3] ^= i64[7];
i64[4] = t32[0];
i64[5] = t32[1];
i64[6] = t32[2];
i64[7] = t32[3];

transdata( k56, k56, shift_right2 );

/*
** shift right 2
*/
t32[0] = i64[0];
t32[1] = i64[1];
t32[2] = i64[2];
t32[3] = i64[3];

fnct( i64, k56 );

i64[0] ^= i64[4];
i64[1] ^= i64[5];
i64[2] ^= i64[6];
i64[3] ^= i64[7];
i64[4] = t32[0];
i64[5] = t32[1];
i64[6] = t32[2];
i64[7] = t32[3];

transdata( k56, k56, shift_right2 );

/*
** shift right 2
*/
t32[0] = i64[0];
t32[1] = i64[1];
t32[2] = i64[2];
t32[3] = i64[3];

fnct( i64, k56 );

i64[0] ^= i64[4];
i64[1] ^= i64[5];
i64[2] ^= i64[6];
i64[3] ^= i64[7];
i64[4] = t32[0];
i64[5] = t32[1];
i64[6] = t32[2];
i64[7] = t32[3];

transdata( k56, k56, shift_right2 );

/*
** shift right 2
*/
t32[0] = i64[0];
t32[1] = i64[1];
t32[2] = i64[2];
t32[3] = i64[3];

fnct( i64, k56 );

i64[0] ^= i64[4];
i64[1] ^= i64[5];
i64[2] ^= i64[6];
i64[3] ^= i64[7];
i64[4] = t32[0];
i64[5] = t32[1];
i64[6] = t32[2];
i64[7] = t32[3];

transdata( k56, k56, shift_right2 );

/*
** shift right 2
*/
t32[0] = i64[0];
t32[1] = i64[1];
t32[2] = i64[2];
t32[3] = i64[3];

fnct( i64, k56 );

i64[0] ^= i64[4];
i64[1] ^= i64[5];
i64[2] ^= i64[6];
i64[3] ^= i64[7];
i64[4] = t32[0];
i64[5] = t32[1];
i64[6] = t32[2];
i64[7] = t32[3];

transdata( k56, k56, shift_right2 );

/*
** shift right 2
*/
t32[0] = i64[0];
t32[1] = i64[1];
t32[2] = i64[2];
t32[3] = i64[3];

fnct( i64, k56 );

i64[0] ^= i64[4];
i64[1] ^= i64[5];
i64[2] ^= i64[6];
i64[3] ^= i64[7];
i64[4] = t32[0];
i64[5] = t32[1];
i64[6] = t32[2];
i64[7] = t32[3];

transdata( k56, k56, shift_right2 );

/*
** shift right 1
*/
t32[0] = i64[0];
t32[1] = i64[1];
t32[2] = i64[2];
t32[3] = i64[3];

fnct( i64, k56 );

i64[0] ^= i64[4];
i64[1] ^= i64[5];
i64[2] ^= i64[6];
i64[3] ^= i64[7];
i64[4] = t32[0];
i64[5] = t32[1];
i64[6] = t32[2];
i64[7] = t32[3];

transdata( k56, k56, shift_right1 );

/*
** shift right 1
*/
t32[0] = i64[0];
t32[1] = i64[1];
t32[2] = i64[2];
t32[3] = i64[3];

fnct( i64, k56 );

i64[0] ^= i64[4];
i64[1] ^= i64[5];
i64[2] ^= i64[6];
i64[3] ^= i64[7];
i64[4] = t32[0];
i64[5] = t32[1];
i64[6] = t32[2];
i64[7] = t32[3];

transdata( k56, k56, shift_right1 );

/*
** substitute
*/
transdata( i64, output, inverse_ip );

}

/*
** end of file sec_sdes_algo.c
*/

使用delphi调用unit:
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
EDLLLoadError = class(Exception);
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
libhd:THandle;
des:procedure(s1:PChar;s2:PChar;s3:PChar);stdcall;
undes:procedure(s1:PChar;s2:PChar;s3:PChar);stdcall;
changevalue:function(v:double;i:bool):double;
implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
s1,s2,s3,s0:string;
v1:Double;

begin
s1:='12345678';
s2:='11111111';
s3:=' ';
s0:=' ';
try
//libhd:=loadlibrary(PChar('bisacom.dll'));
libhd:=loadlibrary(PChar('test1.dll'));

if Libhd = 0 then
raise EDLLLoadError.Create('Unable to Load DLL');
@des:=getprocaddress(libhd,'SEC_SDES_Encrypt');
if @des<>nil then
begin
des(PChar(s1),PChar(s2),PChar(s3));
label3.Caption:=format('pswd:%d-%d-%d-%d-%d-%d-%d-%d',
[ord(s3[1]),ord(s3[2]),ord(s3[3]),ord(s3[4]),ord(s3[5])
,ord(s3[6]),ord(s3[7]),ord(s3[8])]);

end else
RaiseLastWin32Error;

@undes:=getprocaddress(libhd,'SEC_SDES_Decrypt');
if @undes<>nil then
begin
undes(PChar(s3),PChar(s2),PChar(s0));
label1.Caption:=s0;
label4.Caption:=format('pswd:%d-%d-%d-%d-%d-%d-%d-%d',
[ord(s0[1]),ord(s0[2]),ord(s0[3]),ord(s0[4]),ord(s0[5])
,ord(s0[6]),ord(s0[7]),ord(s0[8])]);
end else
begin
RaiseLastWin32Error;
end;

@changevalue:=getprocaddress(libhd,'changeValue');
if @changevalue<>nil then
begin
v1:=changevalue(12.0,true);
label2.Caption:=format('value is:%f',[v1]);
end else
begin
RaiseLastWin32Error;
end;
finally
freelibrary(libhd);
end;

end;

end.




 
不要读你这么长的代码了,90%的情况是的在DLL中的代码访问内存越界。
因为在IDE中的调式环境有很多的保护,导致了你的问题没有显现出来,
但独立运行的话,操作系统是没有保护的。。。
 
看看有没有内存漏洞,最好用static变量。
 
也不说说哪一个调用出错,如果调试时正常,那么把几个调用的地方 try 起来,在 except
里面显示一下提示信息也好啊。
 
此DLL用C++builder调用却一切正常.
C++builder 数据类型 unsigned char * 与delphi的Pchar对应是否合适?
 
应该是对应的,但是delphi要对pchar和string类型的转换做相应处理
 
如果你的调用是异步的,最好把变量定义成 Global ,代码太长,看不清楚的说!
 
过程声明:
procedure des(s1:PChar;s2:PChar;s3:PChar);
pascal
external 'Test.DLL';
 
多人接受答案了。
 
后退
顶部