2013-11-23

【Delphi】簡易 加/解 密字串

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


unit formmain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    function EnDeCrypt(const Value: String): String;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.EnDeCrypt(const Value : String) : String;
var
   CharIndex : integer;
begin
   Result := Value;
   for CharIndex := 1 to Length(Value) do
     Result[CharIndex] := chr(not(ord(Value[CharIndex])));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
    Edit2.Text := EnDeCrypt(Edit1.Text);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
    Edit3.Text := EnDeCrypt(Edit2.Text);
end;

end.

Delphi2010 執行結果

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

Delphi7 執行結果


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

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

沒有留言:

張貼留言