2013-11-23

【Delphi】簡易 加/解 密字串

 新建一個 project,在 Form 上放置 3 個 TEdit,2 個 TButton,源碼如下:


  1. unit formmain;
  2.  
  3. interface
  4.  
  5. uses
  6. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7. Dialogs, StdCtrls;
  8.  
  9. type
  10. TForm1 = class(TForm)
  11. Edit1: TEdit;
  12. Edit2: TEdit;
  13. Edit3: TEdit;
  14. Button1: TButton;
  15. Button2: TButton;
  16. Label1: TLabel;
  17. Label2: TLabel;
  18. Label3: TLabel;
  19. procedure Button1Click(Sender: TObject);
  20. procedure Button2Click(Sender: TObject);
  21. function EnDeCrypt(const Value: String): String;
  22. private
  23. { Private declarations }
  24. public
  25. { Public declarations }
  26. end;
  27.  
  28. var
  29. Form1: TForm1;
  30.  
  31. implementation
  32.  
  33. {$R *.dfm}
  34.  
  35. function TForm1.EnDeCrypt(const Value : String) : String;
  36. var
  37. CharIndex : integer;
  38. begin
  39. Result := Value;
  40. for CharIndex := 1 to Length(Value) do
  41. Result[CharIndex] := chr(not(ord(Value[CharIndex])));
  42. end;
  43.  
  44. procedure TForm1.Button1Click(Sender: TObject);
  45. begin
  46. Edit2.Text := EnDeCrypt(Edit1.Text);
  47. end;
  48.  
  49. procedure TForm1.Button2Click(Sender: TObject);
  50. begin
  51. Edit3.Text := EnDeCrypt(Edit2.Text);
  52. end;
  53.  
  54. end.

Delphi2010 執行結果

因為 Delphi2010 已支援 unicode,所以加密後變成顯示日文字了;如果是 Delphi7,就沒法正常顯示,因為 Delphi7TEdit 只支援 ASCII...

Delphi7 執行結果


資料來源 ----
Encrypting and Decrypting strings in Delphi

相關筆記 ----
【PHP】簡易 加/解 密字串

沒有留言:

張貼留言