我用C语言写了一个Dll,编译成功。
是一个非常简单的加密算法,代码如下:
#include<stdio.h>
__declspec(dllexport) void Encipher(char a[16])
{
int i,len;
int s=1;
len=strlen(a);
for(i=0;i<len;i++)
{
a[i]=a[i]+s;
s=s+2;
}
}现在我在Delphi中如何调用它并实现这样的功能,点击Button1,获取edit1.text的内容,存于数组中,然后调用Dll中的函数,进行简单加密,然后将结果赋给edit2.text。

解决方案 »

  1.   

    把你dll里的api导入进来就可以了。。
    比如静态导入dll中api,

    interface 里
    声明:
    procedure ex_Encipher(array [0..15] of char); stdcall;initialization 里导入dll对应函数
      procedure ex_Encipher; external 'Encipher.dll' name 'Encipher;delphi程序里使用ex_Encipher就可以了
      

  2.   

    procedure ex_Encipher(var array [0..15] of char); stdcall; 
      

  3.   

    按照楼上的方法,
    type
      TArrChar = array[0..15] of char; 
    procedure Encipher(a:TArrChar); stdcall;external 'Encph.dll' name 'Encipher';用户加密后可以存进数据库:
        CopyMemory(@a,@Edit2.text[1],16);
        Encipher(a);
        Adotable1.Insert;
        Adotable1.FieldByName('username').AsString:=trim(edit1.Text);
        Adotable1.FieldByName('password').AsString:=a;
        Adotable1.Post;但是,用户登录时在Edit2中输入密码,后
        CopyMemory(@a,@Edit2.text[1],16);
        Encipher(a);
        if Adotable1.FieldByName('password').AsString=a then
        begin
              //进入子系统
        end
       else messagedlg('密码错误!',mterror,[mbok],0);可以运行,但是为什么提示密码错误呢?当初是这样考虑的,为了使加密算法不可逆,我添加了一些中文。DLL中部分代码是这样的:
    void Encipher(char a[16])
    {
    int i,len;
    int s=1;
    len=strlen(a);
    for(i=0;i<len;i++)
    {
    if(a[i]=='N') a[i]='你';
    else 
    if(a[i]=='H') a[i]='好';
    else
    a[i]=a[i]+s;
    s=s+2;
    }
    }
      

  4.   

    if(a[i]=='N') a[i]='你'; 
    else 
    if(a[i]=='H') a[i]='好'; a只是char数组,"你好"中文的gb2312是两个字节的编码
    你这句a[i]='你'和a[i]='好'只能把两个汉字的低字节赋值给a[i],
    是不是这个使你计算的结果和运算的结果不一样.先把a[i]='你'和a[i]='好'替换一下.if(a[i]=='N') a[i]=0xFF; 
    else 
    if(a[i]=='H') a[i]=0xFF; 
     
      

  5.   

    既然都是0xFF,那还有什么区别呢?