字符串加密解密

时间:2010-02-27 18:00:06  来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:           = 5432;   /// 常量,你可以修改/// 对数组加密function Crypt(const s: TDynByteArray; Key: Word; const bEncrypt: boolean = true): TDynByteArray; overload;var  i               &n...
  正文:type
  TDynByteArray = array of byte;

const
  SeedA             = 5678;   /// 常量,你可以修改
  SeedB             = 5432;   /// 常量,你可以修改

/// 对数组加密
function Crypt(const s: TDynByteArray; Key: word; const bEncrypt: boolean = true): TDynByteArray; overload;
var
  i                 : integer;
begin
  SetLength(Result, Length(s));
  for i := Low(s) to High(s) do
  begin
    Result[i] := s[i] xor (key shr 8);
    if bEncrypt then
      Key := (Result[i] + key) * SeedA + SeedB
    else
      Key := (s[i] + Key) * SeedA + SeedB;
  end;
end;

/// 字符串
function Crypt(const s: string; Key: Word; const bEncrypt: boolean = True): string; overload;
var
  i                 : integer;
  photoshop/ target=_blank class=infotextkey>ps, pr            : ^byte;
begin
  SetLength(Result, Length(s));
  ps := @s[1];
  pr := @Result[1];
  for i := 1 to length(s) do
  begin
    pr^ := ps^ xor (Key shr 8);
    if bEncrypt then
      Key := (pr^ + Key) * SeedA + SeedB
    else
      Key := (ps^ + Key) * SeedA + SeedB;
    pr := pointer(integer(pr) + 1);
    ps := pointer(integer(ps) + 1);
  end
end;

/// 也可以对记录进行加密,只要把TResultData改成你的记录类型即可!!!!!!
function Crypt(const s: TResultData; Key: Word; const bEncrypt: boolean = True): TResultData; overload;
var
  i                 : integer;
  ps, pr            : ^byte;
begin
  ps := @s;
  pr := @Result;
  for i := 1 to SizeOf(s) do
  begin
    pr^ := ps^ xor (Key shr 8);
    if bEncrypt then
      Key := (pr^ + Key) * SeedA + SeedB
    else
      Key := (ps^ + Key) * SeedA + SeedB;
    pr := pointer(integer(pr) + 1);
    ps := pointer(integer(ps) + 1);
  end;
end;
***************************
function cryptstr(const s:string; stype: dword):string;
var
  i: integer;
  fkey: integer;
begin
  result:='';
  case stype of
  0:    
    begin
      randomize;
      fkey := random($ff);
      for i:=1 to length(s) do
       result := result+chr( ord(s[i]) xor i xor fkey);
      result := result + char(fkey);
    end;
  1:    
    begin
      fkey :=  ord(s[length(s)]);
      for i:=1 to length(s) - 1 do

"字符串加密解密"由第二电脑网原创提供,转载请注明:http://www.002pc.com/master/College/Language/Delphi/12944.html


关键字:

关于《字符串加密解密》文章的评论

站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td