此文章是vip文章,如何查看?  

1,点击链接获取密钥 http://nicethemes.cn/product/view29882.html

2,在下方输入文章查看密钥即可立即查看当前vip文章


delphi7的加密算法与 delphi XE+加解密算法

  • 时间:
  • 浏览:
  • 来源:互联网

delphi7的加密算法与 delphi XE+加解密算法

delphi7加解密算法:

Function JiaMi(Src:String; Key:String):String;
var
  KeyLen :Integer;
  KeyPos :Integer;
  offset :Integer;
  dest :String;
  SrcPos :Integer;
  SrcAsc :Integer;
  Range :Integer;
begin
  KeyLen:=Length(Key);
  KeyPos:=0;
  Range:=1;
  Randomize;
  offset:=Random(Range);
  dest:=format('%1.2x',[offset]);
  for SrcPos := 1 to Length(Src) do
  begin
    SrcAsc:=(Ord(Src[SrcPos]) + offset) MOD 255;
    if KeyPos < KeyLen then KeyPos:= KeyPos + 1 else KeyPos:=1;
    SrcAsc:= SrcAsc xor Ord(Key[KeyPos]);
    dest:=dest + format('%1.2x',[SrcAsc]);
    offset:=SrcAsc;
  end;
  Result:=Dest;
end;     
//解密函数
Function JieMi (Src:String; Key:String):String;
var
  KeyLen :Integer;
  KeyPos :Integer;
  offset :Integer;
  dest :String;
  SrcPos :Integer;
  SrcAsc :Integer;
  TmpSrcAsc :Integer;
  Range :Integer;
begin
  KeyLen:=Length(Key);
  if KeyLen = 0 then key:='starlin';
  KeyPos:=0;
  SrcPos:=0;
  SrcAsc:=0;
  Range:=1;
  offset:=StrToInt('$'+ copy(src,1,2));
  SrcPos:=3;
  repeat
    try
      SrcAsc:=StrToInt('$'+ copy(src,SrcPos,2));
    except
      SrcAsc:=StrToInt('$00');
    end;
    if KeyPos < KeyLen Then KeyPos := KeyPos + 1 else KeyPos := 1;
    TmpSrcAsc := SrcAsc xor Ord(Key[KeyPos]);
    if TmpSrcAsc <= offset then
      TmpSrcAsc := 255 + TmpSrcAsc - offset
    else
      TmpSrcAsc := TmpSrcAsc - offset;
    dest := dest + chr(TmpSrcAsc);
    offset:=srcAsc;
    SrcPos:=SrcPos + 2;
  until SrcPos >= Length(Src);
  Result:=Dest;
  Result:=Copy(Result,1,Length(Result)-1);
end;     
end.

Delphi XE+版本:

Function JiaMi(Src:String; Key:String):String;
// Src 支持汉字,字母,数字,符号
// Key 不支持汉字;适合字母与数字
var
  KeyLen :Integer;
  KeyPos :Integer;
  offset :Integer;
  dest :String;
  SrcPos :Integer;
  SrcAsc :Integer;
  Range :Integer;
  IntTemp:integer;
  SrcAnsi:PAnsiChar;

begin
  KeyLen:=Length(Key);
  KeyPos:=0;
  Range:=1;
  Randomize;
  offset:=Random(Range);
  dest:=format('%1.2x',[offset]);
  SrcAnsi:=PAnsiChar(AnsiString(Src));
  IntTemp:= Length(SrcAnsi);
  for SrcPos := 1 to IntTemp do

  begin
    SrcAsc:=(Ord(SrcAnsi[SrcPos-1]) + offset) MOD 255;
    if KeyPos < KeyLen then KeyPos:= KeyPos + 1 else KeyPos:=1;
    SrcAsc:= SrcAsc xor Ord(Key[KeyPos]);
    dest:=dest + format('%1.2x',[SrcAsc]);
    offset:=SrcAsc;
  end;

  Result:=Dest;
end;

//解密函数
Function JieMi (Src:String; Key:String):String;
// Src 支持汉字,字母,数字,符号
// Key 不支持汉字;适合字母与数字
var
  KeyLen :Integer;
  KeyPos :Integer;
  offset :Integer;
  dest :String;
  SrcPos :Integer;
  SrcAsc :Integer;
  TmpSrcAsc :Integer;
  Range :Integer;
  SrcAnsi:PAnsiChar;
  IntTemp,i:Integer;
  ByteArray: array of Byte;
begin
  KeyLen:=Length(Key);
  if KeyLen = 0 then key:='starlin';
  KeyPos:=0;
  SrcPos:=0;
  SrcAsc:=0;
  Range:=1;
  offset:=StrToInt('$'+ copy(src,1,2));
  SrcPos:=3;
  IntTemp:=Length(Src) div 2;
  setlength(ByteArray,IntTemp);
  i:=0;
  repeat
    try
      SrcAsc:=StrToInt('$'+ copy(src,SrcPos,2));
    except
      SrcAsc:=StrToInt('$00');
    end;    
    if KeyPos < KeyLen Then KeyPos := KeyPos + 1 else KeyPos := 1;    
    TmpSrcAsc := SrcAsc xor Ord(Key[KeyPos]);    
    if TmpSrcAsc <= offset then
      TmpSrcAsc := 255 + TmpSrcAsc - offset
    else
      TmpSrcAsc := TmpSrcAsc - offset;
    ByteArray[i]:=TmpSrcAsc;
    i:=i+1;           
    offset:=srcAsc;
    SrcPos:=SrcPos + 2;
  until SrcPos >= Length(Src);
  GetMem(SrcAnsi,IntTemp+1); 
  CopyMemory(@SrcAnsi[0],@ByteArray[0],IntTemp);
  dest:=string(strpas(SrcAnsi));     
  Result:=Dest; 
end;

测试:
str1 :=jiemi(jiami(‘学习ABC123’,‘uabd123’),‘uabd123’);
//返回值:学习ABC123

本文链接http://element-ui.cn/news/show-577257.aspx