Delphi Xe4。在Win7Pro x64,Win8Ent x64,WinSrv2012上进行测试;
Const
CGuAdvapi32dll=WinApi.Windows.Advapi32;
CALG_MD2 = 32769;
CALG_MD4 = 32770;
CALG_MD5 = 32771;
CALG_SHA = 32772;
CALG_SHA_1 = 32772; // CALG_SHA
CALG_SHA_256 = 32780;
CALG_SHA_384 = 32781;
CALG_SHA_512 = 32782;
CALG_DES = 26113;
CALG_RC2 = 26114;
CALG_3DES = 26115;
CALG_3DES_112 = 26121;
CALG_AES_128 = 26126;
CALG_AES_192 = 26127;
CALG_AES_256 = 26128;
CALG_AES = 26128; // CALG_AES_256
CALG_RC4 = 26625;
PROV_RSA_AES = 24; // The PROV_SSL provider type supports US AES crypto-algorithm
CRYPT_VERIFYCONTEXT = $F0000000;
{S} Function GuCryptAcquireContext(hProv: PULong; Container: LPCTSTR; Provider: LPCTSTR; ProvType: DWord; Flags: DWord): Bool; StdCall; External CGuAdvapi32dll Name 'CryptAcquireContextW';
{S} Function GuCryptReleaseContext(hProv: ULong; Flags: DWord): Bool; StdCall; External CGuAdvapi32dll Name 'CryptReleaseContext';
{S} Function GuCryptCreateHash(hProv: ULong; AlgId: ULong; hKey: ULong; Flags: DWord; Hash: PULong): Bool; StdCall; External CGuAdvapi32dll Name 'CryptCreateHash';
{S} Function GuCryptHashData(hHash: ULong; Data: PByte; DataLen: DWord; Flags: DWord): Bool; StdCall; External CGuAdvapi32dll Name 'CryptHashData';
{S} Function GuCryptDeriveKey(hProv: ULong; AlgId: ULong; hData: ULong; Flags: DWord; Key: PULong): Bool; StdCall; External CGuAdvapi32dll Name 'CryptDeriveKey';
{S} Function GuCryptDestroyHash(hHash: ULong): Bool; StdCall; External CGuAdvapi32dll Name 'CryptDestroyHash';
{S} Function GuCryptDestroyKey(hKey: ULong): Bool; StdCall; External CGuAdvapi32dll Name 'CryptDestroyKey';
...
Procedure Test64;
var hProv, hKey, hHash: Ulong; key:string; CGuSoC:dword;
begin
key:='my test password';CGuSoC:=sizeof(char);
if not GuCryptAcquireContext(@hProv, nil, nil, PROV_RSA_AES, CRYPT_VERIFYCONTEXT) then MessageBox(0, 'Not CAC', 0, 0);
MessageBox(0, PChar(IntToStr(hprov)), 0, 0); // < --- 1 check !
if not GuCryptCreateHash(hProv, CALG_MD2{MD5, SHA}, 0, 0, @hHash) then MessageBox(0, 'Not CCH', 0, 0);
if not GuCryptHashData(hHash, @key[1], Length(key)*CGuSoC, 0) then MessageBox(0, 'Not CHD', 0, 0);
if not GuCryptDeriveKey(hProv, CALG_RC4{RC2, AES}, hHash, 0, @hKey) then MessageBox(0, 'Not CDK', 0, 0);
if not GuCryptDestroyHash(hHash) then MessageBox(0, 'Not CDH', 0, 0);
MessageBox(0, PChar(IntToStr(hprov)), 0, 0); // < --- 2 check !
if not GuCryptDestroyKey(hKey) then MessageBox(0, 'Not CDK', 0, 0);
if hProv=0 then MessageBox(0, 'hProv=0!', 0, 0) else if not GuCryptReleaseContext(hProv, 0) then MessageBox(0, 'Not CRC', 0, 0);
end;
问:用目标平台编译“32位窗口”,一切都好。在检查字符串1和2中显示大数字ID,例如。 882345。
编译目标平台“64位窗口”,工作正常,但在检查2中显示“0”(检查1 =确定,大数)。的为什么吗
P.S。我将hProv变量类型更改为ULong64(在proc.Test64 var。和GuCryptAcquireContext中定义),但没有结果。
抱歉英语不好。
答案 0 :(得分:6)
这里的根本问题是您已将HCRYPTPROV
,HCRYPTKEY
和HCRYPTHASH
翻译为32位整数。但它们实际上是指针大小的整数。您应该将它们声明为NativeUInt
。
您的翻译还有其他一些小问题。我的意见:
CryptEncrypt
,则还应使用该名称。否则会导致混淆。var
参数而不是指针。例如,CryptAcquireContext
的第一个参数作为var
参数会更好。我的最终建议是使用API的现有翻译,一个经过试用和测试的翻译。我希望你会在JEDI代码中找到一个。也就是说,我对JEDI项目感到绝望,并且无法让它被发现。