bestlong 怕失憶論壇

標題: Delphi 如何將字串用 MD5 加密 [打印本頁]

作者: bestlong    時間: 2012-12-19 09:57     標題: Delphi 如何將字串用 MD5 加密

Delphi 從 5 ~7 版都沒有內建的函數可以直接使用。剛好有 Indy 這個元件可以幫忙解決
請去 The Indy Project 下載 Indy VCL 後安裝
然後在程式中增加下列函數
  1. //uses IdHashMessageDigest, idHash
  2. function STR2MD5(const ss : string) : string;
  3. begin
  4.   with TIdHashMessageDigest5.Create do
  5.   begin
  6.     try
  7.       result := TIdHash128.AsHex(HashValue(ss));
  8.     finally
  9.       Free;
  10.     end;
  11.   end;
  12. end;
複製代碼
這樣就可以在程式中使用了
  1. procedure TForm1.btnRunClick(Sender: TObject);
  2. begin
  3.   Memo1.Text := STR2MD5(edIn.Text);
  4.   Memo1.Lines.Append(STR2MD5('123'));
  5. end;
複製代碼





歡迎光臨 bestlong 怕失憶論壇 (http://www.bestlong.idv.tw/) Powered by Discuz! X1.5