德尔福 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位windows”编译,一切OK。在检查字符串 1 和 2 中显示大数字 ID,例如。882345。
使用目标平台“64 位 Windows”进行编译,工作正常,但在检查 2 中显示“0”(检查 1 = ok,big num)。为什么?
ps 我将 hProv 变量类型更改为 ULong64(在 proc.Test64 var.和 GuCryptAcquireContext 定义中),但没有结果。
对不起英语不好。