一个简单的加密解密算法

来源:互联网 发布:mac os server工具 编辑:程序博客网 时间:2024/05/20 07:20
//前面定义好常量
const
  C1 = 123456;
  C2 = 234567;
  PASSKEY = '66666';


 //加密函数
function Encrypt(const S: String; Key: Word): String;
var
  I: byte;
begin
  Result:=S;
  for I:= 1 to Length(S) do begin
    Result[I]:= char(byte(S[I]) xor (Key shr 8));
    Key:=(byte(Result[I]) + Key) * C1 + C2;
  end;
end;

//解密函数
function Decrypt(const S: String; Key: Word): String;
var
  I: byte;
begin
  Result:=S;
  for I := 1 to Length(S) do begin
    Result[I]:= char(byte(S[I]) xor (Key shr 8));
    Key := (byte(S[I]) + Key) * C1 + C2;
  end;
end;


//使用方法------------------------------------------------

tmp := Encrypt('需要加密的内容', StrToInt(PASSKEY));
tmp := Decrypt('需要解密的内容', StrToInt(PASSKEY));