一.dll内容
1.dll中的函数声明
extern "C" _declspec(dllexport) bool MakeMD5File(char *file,char *key);
extern "C" _declspec(dllexport) int ValidateMD5File(char *file,char *key,char *object);
extern "C" _declspec(dllexport) void EncryptString128(char *string,char *result);
extern "C" _declspec(dllexport) bool DecryptString128(char *source,char *object);
extern "C" _declspec(dllexport) int DecryptFile160(char *file,char *key,char *result);
extern "C" _declspec(dllexport) bool EncryptFile160(char *file,char *key,char *result);
extern "C" _declspec(dllexport) void Transfer40To20(char *str40, char *str20);
extern "C" _declspec(dllexport) void Transfer20To40(char *str20, char *str40);
extern "C" _declspec(dllexport) bool MakeMD5FileFrom40(char *str40, char *file);
extern "C" _declspec(dllexport) void EncryptString160(char *string,char *key,char *result);
extern "C" _declspec(dllexport) bool DecryptString160(char *source,char *key,char *object);
2.dll的接口说明
void EncryptString128(char *string,char *result)
string:要被计算的串的指针,任意长度,以'\0'结束
result:保存结果的缓冲区指针,>=33字节,结果保存在该缓冲区的前32个字节里
功能:计算出由string指向的任意长度的串的128位MD5摘要串,摘要串为这128位的16进制形式,128共16字节,每字节表示为2位16进制数,故保存结果的串指针result指向的缓冲区必 大于或等于 (128/8)*2+1=33字节
bool DecryptString128(char *source,char *object)
source:要验证的串的指针,以'\0'结束
object:16进制形式摘要串的指针,前32个字节为摘要串,长度必须>=33,object[32]='\0'
功能:验证指针source指向的串的128位摘要串是否为object指向串的前32个字节
是:返回true,否:返回false
//////////////////将string串的160位(40字节)摘要串计算出来保存在result中,key为4个字节的密码
extern "C" _declspec(dllexport) void EncryptString160(char *string,char *key,char *result)
注:输入参数result长度为>=41字节,16进制形式的摘要串保存在其前40个字节中,41字节为'\0'
bool EncryptFile160(char *file,char *key,char *result)
file:要计算摘要串的文件的路径名
key:32位扰码,为前4个字节,>=5个字节,key[4]='\0'
result:保存结果的缓冲区指针,>=41字节,结果保存在该缓冲区的前40个字节里,41字节为'\0'
功能:计算出由file指向的文件的160位(128位+32位扰码)MD5摘要串,摘要串为这160位的16进制形式,128共20字节,每字节表示为2位16进制数,故保存结果的串指针result指向的缓冲区必 大于或等于 (160/8)*2+1=41字节
计算成功返回true,出错返回false
int DecryptFile160(char *file,char *key,char *result)
file:要验证摘要串的文件的路径名
key:32位扰码,为前4个字节,>=4个字节
result:摘要串指针,>=41字节,前40个字节为摘要串,41字节为'\0'
功能:验证file指向的文件与扰码key计算得到的摘要串是否为result指向的串的前40个字节
验证成功返回1,不成功返回-1,出错返回0
bool MakeMD5File(char *file,char *key)
file:输入要计算摘要文件的文件的路径名,输出时变为对应的MD5文件名
key:32位扰码,为前4个字节,>=4个字节
功能:用扰码key和文件file计算得到160位(40字节)摘要串,并将其扩展成512字节且保存在同一目录下,文件名相同
扩展名为".MD5"
成功返回true,出错返回false
int ValidateMD5File(char *file,char *key,char *object)
file:要验证的文件
key:32位扰码,为前4个字节,>=4个字节
object:已存在的512字节摘要文件
功能:验证由file指向的文件和key指向的扰码生成的512字节摘要文件是否与object指向的摘要文件一致
一致:返回1,不一致:返回-1,出错返回0
/////////////////将40字节的16进制形式摘要串转换为原来的形式:160位20字节
extern "C" _declspec(dllexport) void Transfer40To20(char *str40, char *str20)
str40[40]='\0',str20长度为21
/////////////////将160位20字节形式转换为40字节的16进制形式摘要串
extern "C" _declspec(dllexport) void Transfer20To40(char *str20, char *str40)
str20[20]='\0',str40长度为41
//////////////从40字节的摘要串str40创建512字节的MD5文件,file为MD5 文件的路径
extern "C" _declspec(dllexport) bool MakeMD5FileFrom40(char *str40, char *file)
str40[40]='\0',前40字节为摘要串的16进制形式
二.如何使用该dll:使用DllImport
这里只举ValidateMD5File、Transfer40To20、Transfer20To40、EncryptString160四个例子,其它的接口类似:
1.声明
先导入:
Imports System.Runtime.InteropServices
再声明:
<DllImport("MD5_ENCODE.dll", CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Cdecl)> Public Shared Function ValidateMD5File(ByVal src As String, ByVal key As String, ByVal result As String) As Integer
End Function
<DllImport("MD5_ENCODE.dll", CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Cdecl)> Public Shared Sub EncryptString160(ByVal src As String, ByVal key As String, ByRef result As Byte)
End Sub
<DllImport("MD5_ENCODE.dll", CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Cdecl)> Public Shared Sub Transfer40To20(ByVal src As Byte(), ByRef result As Byte)
End Sub
<DllImport("MD5_ENCODE.dll", CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Cdecl)> Public Shared Sub Transfer20To40(ByVal src As Byte(), ByRef result As Byte)
End Sub
2.接口使用例子
为测试ValidateMD5File函数,先在程序目录下放入文件a.txt及其MD5文件a.MD5,测试代码为:
' 测试ValidateMD5File
Dim src As String = "a.txt"
Dim key As String = "1234"
Dim res As String = "a.MD5"
Dim r As Integer = 3
r = ValidateMD5File(src, key, res)
Me.Label1.Text = "测试ValidateMD5File(""a.txt"",""1234"",""a.MD5""): " _
+ r.ToString
测试得返回值r为1,改变一下a.txt内容,则返回-1过头来,测试成功
为测试Transfer40To20、Transfer20To40、EncryptString160,加入测试代码:
' 测试EncryptString128
Dim code(41) As Byte
EncryptString160(src, key, code(0))
Dim t(41) As Char
Dim i As Integer
For i = 0 To 40
t(i) = ChrW(code(i))
Next
Me.Label2.Text = "测试EncryptString160: " + t
'测试Transfer40To20和Transfer20To40
'从40字节形式转为20字节形式
Dim s20(21) As Byte
Transfer40To20(code, s20(0))
'从40字节形式转为20字节形式
Dim s40(41) As Byte
Transfer20To40(s20, s40(0))
Dim c40(41) As Char
For i = 0 To 40
c40(i) = ChrW(s40(i))
Next
Me.Label3.Text = "从40字节形式转为20字节形式,然后再转回40字节的结果:" _
+ Chr(10) + Chr(13) + c40
测试成功。