本文介绍了是否有任何密码安全的PRNG库为Delphi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 任何人都可以为Delphi(Win32)推荐一个密码安全的伪随机数生成器库? 可以免费或商业化,但最好是一个活跃的项目。我希望它包含源代码。 解决方案您可以使用Windows CryptoAPI: code>使用Wcrypt2; 函数GenerateRandom(Len:Cardinal):TBytes; var hProv:HCRYPTPROV; begin 如果不是CryptAcquireContext(@hProv, nil, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)然后 CryptAcquireContext(@hProv, nil, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET + CRYPT_VERIFYCONTEXT); 如果hProv> 0 then try SetLength(Result,Len); CryptGenRandom(hProv,Len,@ Result [0]); finally CryptReleaseContext(hProv,0); 结束结束 使用上述代码的示例: 函数BytesToHex(const Bytes:TBytes):string; var i:integer; begin for i:= 0 to Length(Bytes)-1 do 结果:= Result + IntToHex(Bytes [i],2); 结束 procedure TForm1.Button1Click(Sender:TObject); begin ShowMessage(BytesToHex(GenerateRandom(16))); 结束 Can anyone recommend a cryptographically-secure pseudo random number generator library for Delphi (Win32)?Can be free or commercial, but will ideally be an active project. I'd like it to include source code. 解决方案 You can use Windows CryptoAPI:uses Wcrypt2;function GenerateRandom(Len: Cardinal): TBytes;var hProv : HCRYPTPROV;begin if not CryptAcquireContext(@hProv, nil, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) then CryptAcquireContext(@hProv, nil, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET + CRYPT_VERIFYCONTEXT); if hProv > 0 then try SetLength(Result,Len); CryptGenRandom(hProv,Len,@Result[0]); finally CryptReleaseContext(hProv,0); end;end;Example of using the above code:function BytesToHex(const Bytes: TBytes): string;var i : integer;begin for i := 0 to Length(Bytes)-1 do Result := Result + IntToHex(Bytes[i],2);end;procedure TForm1.Button1Click(Sender: TObject);begin ShowMessage(BytesToHex(GenerateRandom(16)));end; 这篇关于是否有任何密码安全的PRNG库为Delphi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-29 04:01