请问如何在delphi中调用下面的加密解密函数? (50分)

  • 主题发起人 主题发起人 speedaway
  • 开始时间 开始时间
S

speedaway

Unregistered / Unconfirmed
GUEST, unregistred user!
加密:<br>BOOL WINAPI CryptEncrypt(<br>&nbsp; HCRYPTKEY hKey, <br>&nbsp; HCRYPTHASH hHash, <br>&nbsp; BOOL Final, <br>&nbsp; DWORD dwFlags, <br>&nbsp; BYTE *pbData, <br>&nbsp; DWORD *pdwDataLen, <br>&nbsp; DWORD dwBufLen <br>);<br>解密:<br>BOOL WINAPI CryptDecrypt(<br>&nbsp; HCRYPTKEY hKey, <br>&nbsp; HCRYPTHASH hHash, <br>&nbsp; BOOL Final, <br>&nbsp; DWORD dwFlags, <br>&nbsp; BYTE *pbData, <br>&nbsp; DWORD *pdwDataLen <br>);<br>请写出详细的转换后的函数,十分感谢!!!急!!!!!(特别是参数类型的转换)。<br>上面两个函数均在windows API的“advapi32.dll"动态库中。
 
加密:<br>function CryptEncrypt(<br>&nbsp; hKey: HCRYPTKEY; <br>&nbsp; hHash: HCRYPTHASH;<br>&nbsp; Final: Boolean; <br>&nbsp; dwFlags: DWORD;<br>&nbsp; pbData: PBYTE; <br>&nbsp; pdwDataLen: PDWORD; <br>&nbsp; dwBufLen: DWORD <br>):Boolean;<br>解密:<br>function CryptDecrypt(<br>&nbsp; hKey: HCRYPTKEY; <br>&nbsp; hHash: HCRYPTHASH; <br>&nbsp; Final: Boolean; <br>&nbsp; dwFlags: DWORD; <br>&nbsp; pbData: PBYTE; <br>&nbsp; pdwDataLen: PDWORD; <br>):Boolean;<br>Type<br>&nbsp; PBYTE = ^BYTE;<br>&nbsp; PDWORD = ^DWORD;<br>end;<br>
 
如果在VC中此函数已经定义我可以在DELPHI中直接调用吗?如果可以的话,<br>怎么调用?是不是要把这些函数封装起来然后调用动态链接库?如果没有<br>封装呢?也就是说我要一个一个的调用VC中的函数该如何做十分感谢各位<br>的解答!!!<br>
 
如果不把函数封装在 DLL 中是不可能在 Delphi 中调用的!<br>如果是封装好的函数,只要在 Delphi 中声明一下就可以用了。
 
For Example:<br><br>Procedure TSecMainForm.Encrypt(Buffer : PChar; BufLen : Integer);<br>Begin<br>&nbsp; If (Not CryptEncrypt(IKey,0,False,0,Buffer,BufLen,BufLen)) Then<br>&nbsp; &nbsp; Raise Exception.Create('Cannot encrypt data: '+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SysErrorMessage(GetLastError));<br>End;<br><br>Procedure TSecMainForm.Decrypt(Buffer : PChar; BufLen : Integer);<br>Begin<br>&nbsp; If (Not CryptDecrypt(IKey,0,False,0,Buffer,BufLen)) Then<br>&nbsp; &nbsp; Raise Exception.Create('Cannot decrypt data: '+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SysErrorMessage(GetLastError));<br>End;
 
varphone的声明是完全不正确的,请再仔细的想一想,不可能那么简单嘛。<br>可要积极发言哟。
 
TO YB_unique:<br>你的那个例子可以吗?怎么可能一开始就调用CryptEncrypt函数呢?<br>在这之前应该还要调用另外两个函数吧?<br>如果你真有心,就麻烦你写出完整的调用过程。比如说试着完成一些小功能:<br>写入明文,按下按钮后,输出密文。多谢指教,不胜感激。<br>
 
直接调用是不可能的,因为CryptoAPI的声明在WinCrypt.H里,想要Delphi使用必须先转换它!<br>国外有人已经将其部分转换为Delphi可以调用的形式的了!如 TCryptography类。<br>CryptoAPI concepts and develop a tCryptography class to make its use from Delphi <br>
 
能不能详细点,我也要用到这个的,<br>谢谢
 
网上有一个头文件的,cryptapi.h,搜索一下就行了。<br>使用的话,要先AcquireContext什么的,用来获得容器,<br>加密的操作只有在容器里才能执行,请参考msdn里的资料。
 
楼上的那位说得有点像了能不能再详细点呢?不胜感激。
 
下面的三个函数是加密时必须要用的:<br>BOOL WINAPI CryptAcquireContext(<br>&nbsp; HCRYPTPROV *phProv,<br>&nbsp; LPCTSTR pszContainer,<br>&nbsp; LPCTSTR pszProvider,<br>&nbsp; DWORD dwProvType,<br>&nbsp; DWORD dwFlags<br>);<br>BOOL WINAPI CryptGenKey(<br>&nbsp; HCRYPTPROV hProv, <br>&nbsp; ALG_ID Algid, <br>&nbsp; DWORD dwFlags, <br>&nbsp; HCRYPTKEY *phKey <br>);<br>BOOL WINAPI CryptEncrypt(<br>&nbsp; HCRYPTKEY hKey, <br>&nbsp; HCRYPTHASH hHash, <br>&nbsp; BOOL Final, <br>&nbsp; DWORD dwFlags, <br>&nbsp; BYTE *pbData, <br>&nbsp; DWORD *pdwDataLen, <br>&nbsp; DWORD dwBufLen <br>);<br>我该如何调用?(我问的是在DELPHI中如何调用)
 
我如下的声明为什么老是出错呢?<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br>type<br>&nbsp;pint=^integer;<br>type<br>&nbsp; TSecMainForm = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br>&nbsp; function CryptAcquireContext(hprov:pint;pszcontainer:pchar;<br>&nbsp; pszprovider:pchar;dwprovtype:dword;dwflags:dword):boolean;stdcall;external 'advapi32.dll';<br><br>var<br>&nbsp; SecMainForm: TSecMainForm;<br>implementation<br><br>{$R *.DFM}<br>procedure TSecMainForm.Button2Click(Sender: TObject);<br>var<br>hprov:pint;<br>pszcontainer:Pchar;<br>pszprovider:Pchar;<br>dwprovtype:dword;<br>dwflags:dword;<br>begin<br>new(hprov);<br>pszcontainer:='kc222';<br>pszprovider:='Microsoft Base Cryptographic Provider v1.0';<br>dwprovtype:=1;<br>dwflags:=0;<br>if CryptAcquireContext(@hprov,pszcontainer,pszprovider,dwprovtype,dwflags)=false then<br>showmessage('NIHAO')<br>else<br>showmessage('error');<br>dispose(hprov);<br>end;<br><br>end.<br>总是出现“无法定位程序输入点CryptAcquireContext于动态链接库advapi32.dll上"<br>这样的错误。为什么?告诉我。<br>
 
大家多发言呀。<br>不要光看不说嘛。<br>希望大家多说说自己的意见;<br>错了也没关系嘛,<br>本人在此给你们问好了,<br>祝大家快乐!
 
给我回答呀/
 
我翻译的函数是有问题的,因为那个函数里面的一些数据类型 Delphi 里面都没有,<br>如果想要它能够运行就必须知道那些数据类型的具体内容!
 
我如果调用win API函数是不是不用在delphi中声明呀?<br>如果要调用的话,就直接用是不是?<br>例如我如果要用到函数:<br>BOOL WINAPI CryptGenKey(<br>&nbsp; HCRYPTPROV hProv, <br>&nbsp; ALG_ID Algid, <br>&nbsp; DWORD dwFlags, <br>&nbsp; HCRYPTKEY *phKey <br>);<br>是不是直接调用就行了呀?参数类型要转换,可是我不知道如何在delphi中声明?<br>请各位大哥大姐给个指点,不胜感激!!!谢谢!!!<br>
 
多人接受答案了。
 
后退
顶部