⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xencrypt.pas

📁 我自己用的Delphi函数单元 具体说明见打包文件的HELP目录下面
💻 PAS
字号:
unit xEncrypt;

//BORLAND加密单元,可产生2的96次方数的范围,以使解密者难于枚举;
//StartKey避免小于256,如果用于互联网,应该下列形式,即键值由用户提供:
//function Encrypt(const InString:string; StartKey,MultKey,AddKey:Integer): string;
//function Decrypt(const InString:string; StartKey,MultKey,AddKey:Integer): string;

interface

uses
    SysUtils,Classes;

const
  StartKey    = 956;  	{Start default key}
  MultKey	  = 58645;	{Mult default key}
  AddKey	  = 28564;	{Add default key}

function UUEncrypt(const InString:string): string;
function UUDecrypt(const InString:string): string;

function Encrypt(const InString:string; StartKey,MultKey,AddKey:Integer): string;
function Decrypt(const InString:string; StartKey,MultKey,AddKey:Integer): string;


implementation

const
   Offset = 32;

function _UUEncode(InString: string):String;
const
   CharsPerLine = 60;
   BytesPerHunk = 3;
   SixBitMask = $3F;
var
   OutString : String;
   Line: array[0..CharsPerLine-1] of Char;
   Hunk: array[0..2] of Byte;
   Chars: array[0..3] of Byte;
   x,k,m,n: Integer;  //x:LineLength; m : BytesNum; n : BytesInLine

  procedure FlushLine;
  var
     I: Integer;
  begin
      OutString := OutString +char(n + Offset);
      for I := 0 to x - 1 do OutString := OutString +Line[I];
      OutString := OutString +#13#10;
      x := 0;
      n := 0;
  end;

  procedure FlushHunk;
  var
     I: Integer;
  begin
      if x = CharsPerLine  then FlushLine;
      Chars[0] := Byte(Hunk[0] shr 2);
      Chars[1] := Byte((Hunk[0] shl 4) + (Hunk[1] shr 4));
      Chars[2] := Byte((Hunk[1] shl 2) + (Hunk[2] shr 6));
      Chars[3] := Byte(Hunk[2] and SixBitMask);
      for I := 0 to 3 do
      begin
         Line[x] := Char((Chars[I] and SixBitMask) + Offset);
         Inc(x);
     end;
      Inc(n, m);
      m := 0
  end;

begin
    OutString := '';
    n := 0;
    x := 0;
    m := 0;
    k:=1;
    while k <= Length(InString) do
    begin
        if m = BytesPerHunk then FlushHunk;
        Hunk[m] := ord(InString[k]);
        Inc(k);
        Inc(m);
    end;
    if m > 0   then FlushHunk;
    FlushLine;
    if x > 0    then FlushLine;

    Result := OutString;
end;

function _UUDecode(InString: string):String;
var
    oList : TStringList;
    Line: string;
    x,y,m,n,i,j: Integer;
    Chars: array[0..3] of Byte;
    Hunk: array[0..2] of Byte;

    function NextCh: Char;
    begin
        Inc(x);
        if x > Length(Line) then raise Exception.Create('行索引溢出。');
        if not (Line[x] in [' '..'`']) then raise Exception.Create('行中包含非法字符。');
        if Line[x] = '`' then Result := ' '
        else Result := Line[x]
    end;

begin
    Result := '';
    oList := TStringList.Create;
    oList.Text :=InString;

    try
        try
            for y:= 0 to oList.Count-1 do
            begin
                Line := oList[y];
                if (Length(Line) = 0) or (Line[1] in [' ', '`']) then
                    raise Exception.Create('编码数据中有空行。');
                x := 0;
                m := 3;
                n := Byte(NextCh) - Offset;
                for i := 1 to n do
                begin
                    if m = 3 then
                    begin
                      for j := 0 to 3  do Chars[j] := Byte(NextCh) - Offset;
                      Hunk[0] := Byte((Chars[0] shl 2) + (Chars[1] shr 4));
                      Hunk[1] := Byte((Chars[1] shl 4) + (Chars[2] shr 2));
                      Hunk[2] := Byte((Chars[2] shl 6) + Chars[3]);
                      m := 0
                    end;
                    Result := Result + Char(Hunk[m]);
                    Inc(m);
                end;
            end;
        except
            Result := '';
        end;
    finally
        oList.Free;
    end;
end;

{$R-}
{$Q-}
{*******************************************************
 * Standard Encryption algorithm - Copied from Borland *
 *******************************************************}
function Encrypt(const InString:string; StartKey,MultKey,AddKey:Integer): string;
var
  I : Longword;
begin
  Result := '';
  for I := 1 to Length(InString) do
  begin
    Result := Result + CHAR(Byte(InString[I]) xor (StartKey shr 8));
    StartKey := (Byte(Result[I]) + StartKey) * MultKey + AddKey;
  end;
end;
{*******************************************************
 * Standard Decryption algorithm - Copied from Borland *
 *******************************************************}
function Decrypt(const InString:string; StartKey,MultKey,AddKey:Integer): string;
var
  I : Longword;
begin
  Result := '';
  for I := 1 to Length(InString) do
  begin
    Result := Result + CHAR(Byte(InString[I]) xor (StartKey shr 8));
    StartKey := (Byte(InString[I]) + StartKey) * MultKey + AddKey;
  end;
end;
{$R+}
{$Q+}

function UUEncrypt(const InString:string): string;
begin
    Result := _UUEncode(Encrypt(InString,StartKey,MultKey,AddKey));
end;

function UUDecrypt(const InString:string): string;
begin
    Result := Decrypt(_UUDecode(InString),StartKey,MultKey,AddKey);
end;

end.


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -