delphi 简单的字符串加密和解密  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi 简单的字符串加密和解密




unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Button2: TButton;
    Edit3: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//首先定义一个常量数组
const
      XorKey:array[0..7] of Byte=($A1,$B7,$AC,$57,$1C,$63,$3B,$81); //字符串加密用

      //在程序里加入以下两个函数,

function Enc(Str:String):String;//字符加密函數 這是用的一個異或加密
var
i,j:Integer;
begin
Result:='';
j:=0;
for i:=1 to Length(Str) do
   begin
     Result:=Result+IntToHex(Byte(Str[i]) xor XorKey[j],2);
     j:=(j+1) mod 8;
   end;
end;
function Dec(Str:String):String;//字符解密函數
var
i,j:Integer;
begin
Result:='';
j:=0;
for i:=1 to Length(Str) div 2 do
   begin
     Result:=Result+Char(StrToInt('$'+Copy(Str,i*2-1,2)) xor XorKey[j]);
     j:=(j+1) mod 8;
   end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
if edit1.Text<>'' then
begin
  Edit2.Text:= Enc(Edit1.text);
end;
end;

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

end.

https://blog.csdn.net/qq_33536143/article/details/85721391

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.14739298820496 seconds