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

📄 unitfymd5.pas

📁 MD5加密与解密的说明文件与,烦琐的加密不能解密
💻 PAS
📖 第 1 页 / 共 5 页
字号:
    $1948c25c, $02fb8a8c, $01c36ae4, $d6ebe1f9, 
    $90d4f869, $a65cdea0, $3f09252d, $c208e69f,
    $b74e6132, $ce77e25b, $578fdfe3, $3ac372e6));

var
  Padding: TByteArray64 =
    ($80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

function _F(x, y, z: longword): longword;
begin
  Result := (((x) and (y)) or ((not x) and (z)));
end;

function _G(x, y, z: longword): longword;
begin
  Result := (((x) and (z)) or ((y) and (not z)));
end;

function _H(x, y, z: longword): longword;
begin
  Result := ((x) xor (y) xor (z));
end;

function _I(x, y, z: longword): longword;
begin
  Result := ((y) xor ((x) or (not z)));
end;

function RotL(x, n: longword): longword;
begin
  Result := (((x) shl (n)) or ((x) shr (32 - (n))));
end;

procedure FF(var a: longword; b, c, d, x, s, ac: longword);
begin
  a := a + _F(b, c, d) + x + ac;
  a := RotL(a, s);
  a := a + b;
end;

procedure GG(var a: longword; b, c, d, x, s, ac: longword);
begin
  a := a + _G(b, c, d) + x + ac;
  a := RotL(a, s);
  a := a + b;
end;

procedure HH(var a: longword; b, c, d, x, s, ac: longword);
begin
  a := a + _H(b, c, d) + x + ac;
  a := RotL(a, s);
  a := a + b;
end;

procedure II(var a: longword; b, c, d, x, s, ac: longword);
begin
  a := a + _I(b, c, d) + x + ac;
  a := RotL(a, s);
  a := a + b;
end;

procedure MD5Encode(Output: PByteArray; Input: PLongWordArray; Len: longword);
var
  i, j: longword;
begin
  j := 0;
  i := 0;
  while j < Len do
  begin
    output[j]     := byte(input[i] and $ff);
    output[j + 1] := byte((input[i] shr 8) and $ff);
    output[j + 2] := byte((input[i] shr 16) and $ff);
    output[j + 3] := byte((input[i] shr 24) and $ff);
    Inc(j, 4);
    Inc(i);
  end;
end;

procedure MD5Decode(Output: PLongWordArray; Input: PByteArray; Len: longword);
var
  i, j: longword;
begin
  j := 0;
  i := 0;
  while j < Len do 
  begin
    Output[i] := longword(Input[j]) or (longword(Input[j + 1]) shl 8) or
      (longword(Input[j + 2]) shl 16) or (longword(Input[j + 3]) shl 24);
    Inc(j, 4);
    Inc(i);
  end;
end;

procedure MD5Transform(State: PLongWordArray4; Buffer: PByteArray64);
var
  a, b, c, d: longword;
  x:          array[0..15] of longword;
begin
  a := State[0];
  b := State[1];
  c := State[2];
  d := State[3];
  MD5Decode(PLongWordArray(@x), PByteArray(Buffer), 64);

  FF(a, b, c, d, x[0], S11, $d76aa478);
  FF(d, a, b, c, x[1], S12, $e8c7b756);
  FF(c, d, a, b, x[2], S13, $242070db);
  FF(b, c, d, a, x[3], S14, $c1bdceee);
  FF(a, b, c, d, x[4], S11, $f57c0faf);
  FF(d, a, b, c, x[5], S12, $4787c62a);
  FF(c, d, a, b, x[6], S13, $a8304613);
  FF(b, c, d, a, x[7], S14, $fd469501);
  FF(a, b, c, d, x[8], S11, $698098d8);
  FF(d, a, b, c, x[9], S12, $8b44f7af);
  FF(c, d, a, b, x[10], S13, $ffff5bb1);
  FF(b, c, d, a, x[11], S14, $895cd7be);
  FF(a, b, c, d, x[12], S11, $6b901122);
  FF(d, a, b, c, x[13], S12, $fd987193);
  FF(c, d, a, b, x[14], S13, $a679438e);
  FF(b, c, d, a, x[15], S14, $49b40821);

  GG(a, b, c, d, x[1], S21, $f61e2562);
  GG(d, a, b, c, x[6], S22, $c040b340);
  GG(c, d, a, b, x[11], S23, $265e5a51);
  GG(b, c, d, a, x[0], S24, $e9b6c7aa);
  GG(a, b, c, d, x[5], S21, $d62f105d);
  GG(d, a, b, c, x[10], S22, $2441453);
  GG(c, d, a, b, x[15], S23, $d8a1e681);
  GG(b, c, d, a, x[4], S24, $e7d3fbc8);
  GG(a, b, c, d, x[9], S21, $21e1cde6);
  GG(d, a, b, c, x[14], S22, $c33707d6);
  GG(c, d, a, b, x[3], S23, $f4d50d87);

  GG(b, c, d, a, x[8], S24, $455a14ed);
  GG(a, b, c, d, x[13], S21, $a9e3e905);
  GG(d, a, b, c, x[2], S22, $fcefa3f8);
  GG(c, d, a, b, x[7], S23, $676f02d9);
  GG(b, c, d, a, x[12], S24, $8d2a4c8a);

  HH(a, b, c, d, x[5], S31, $fffa3942);
  HH(d, a, b, c, x[8], S32, $8771f681);
  HH(c, d, a, b, x[11], S33, $6d9d6122);
  HH(b, c, d, a, x[14], S34, $fde5380c);
  HH(a, b, c, d, x[1], S31, $a4beea44);
  HH(d, a, b, c, x[4], S32, $4bdecfa9);
  HH(c, d, a, b, x[7], S33, $f6bb4b60);
  HH(b, c, d, a, x[10], S34, $bebfbc70);
  HH(a, b, c, d, x[13], S31, $289b7ec6);
  HH(d, a, b, c, x[0], S32, $eaa127fa);
  HH(c, d, a, b, x[3], S33, $d4ef3085);
  HH(b, c, d, a, x[6], S34, $4881d05);
  HH(a, b, c, d, x[9], S31, $d9d4d039);
  HH(d, a, b, c, x[12], S32, $e6db99e5);
  HH(c, d, a, b, x[15], S33, $1fa27cf8);
  HH(b, c, d, a, x[2], S34, $c4ac5665);

  II(a, b, c, d, x[0], S41, $f4292244);
  II(d, a, b, c, x[7], S42, $432aff97);
  II(c, d, a, b, x[14], S43, $ab9423a7);
  II(b, c, d, a, x[5], S44, $fc93a039);
  II(a, b, c, d, x[12], S41, $655b59c3);
  II(d, a, b, c, x[3], S42, $8f0ccc92);
  II(c, d, a, b, x[10], S43, $ffeff47d);
  II(b, c, d, a, x[1], S44, $85845dd1);
  II(a, b, c, d, x[8], S41, $6fa87e4f);
  II(d, a, b, c, x[15], S42, $fe2ce6e0);
  II(c, d, a, b, x[6], S43, $a3014314);
  II(b, c, d, a, x[13], S44, $4e0811a1);
  II(a, b, c, d, x[4], S41, $f7537e82);
  II(d, a, b, c, x[11], S42, $bd3af235);
  II(c, d, a, b, x[2], S43, $2ad7d2bb);
  II(b, c, d, a, x[9], S44, $eb86d391);

  Inc(State[0], a);
  Inc(State[1], b);
  Inc(State[2], c);
  Inc(State[3], d);

  FillChar(PByteArray(@x)^, SizeOf(x), #0);
end;

procedure MD5Init(var Context: TMD5Context);
begin
  FillChar(Context, SizeOf(Context), 0);
  Context.State[0] := $67452301;
  Context.State[1] := $efcdab89;
  Context.State[2] := $98badcfe;
  Context.State[3] := $10325476;
end;

procedure MD5Update(var Context: TMD5Context; Input: PByteArray; InputLen: longword);
var
  i, Index, PartLen: longword;
begin
  Index := longword((Context.Count[0] shr 3) and $3F);
  Inc(Context.Count[0], longword(InputLen) shl 3);
  if Context.Count[0] < longword(InputLen) shl 3 then
    Inc(Context.Count[1]);
  Inc(Context.Count[1], longword(InputLen) shr 29);
  PartLen := 64 - Index;
  if InputLen >= PartLen then 
  begin
    Move(Input^, PByteArray(@Context.Buffer[index])^, PartLen);
    MD5Transform(@Context.State, @Context.Buffer);
    i := PartLen;
    while i + 63 < InputLen do 
    begin
      MD5Transform(@Context.State, PByteArray64(@Input[i]));
      Inc(i, 64);
    end;
    Index := 0;
  end 
  else
    i := 0;
  Move(PByteArray(@Input[i])^, PByteArray(@Context.Buffer[Index])^, InputLen - i);
end;

procedure MD5Final(var Digest: TMD5Digest; var Context: TMD5Context);
var
  Bits:          array [0..7] of byte;
  Index, PadLen: longword;
begin
  MD5Encode(PByteArray(@Bits), PLongWordArray(@Context.Count), 8);
  Index := longword((Context.Count[0] shr 3) and $3F);
  if Index < 56 then
    PadLen := 56 - Index
  else
    PadLen := 120 - Index;
  MD5Update(Context, PByteArray(@PADDING), PadLen);
  MD5Update(Context, PByteArray(@Bits), 8);
  MD5Encode(PByteArray(@Digest), PLongWordArray(@Context.State), 16);
  FillChar(PByteArray(@Context)^, SizeOf(Context), #0);
end;

function MD5ToString(const ADigest: TMD5Digest): string;
//var
//  i: integer;
begin
  SetLength(Result, 32);
  BinToHex(@ADigest.Cs, @Result[1], 16);
//  Result := '';
//  for i := 0 to 15 do
//    Result := Result + IntToHex(ADigest.Bs[i], 2);
end;

function MD5(const AString: string): TMD5Digest;
begin
  Result := MD5(AString[1], Length(AString));
end;

function GetFileHash(const AFileName: string): TMD5Digest;
var
  f: TFileStream;
begin
  f := TFileStream.Create(AFileName, fmOpenRead);
  try
    Result := MD5(f);
  finally
    f.Free;
  end;
end;

function MD5(const AStream: TStream): TMD5Digest;
var
  Context:    TMD5Context;
  Buffer:     array[0..4095] of byte;
  Size:       integer;
  ReadBytes:  integer;
  TotalBytes: integer;
  SavePos:    integer;
begin
  MD5Init(Context);
  Size       := AStream.Size;
  SavePos    := AStream.Position;
  TotalBytes := 0;
  try
    AStream.Seek(0, soFromBeginning);
    repeat
      ReadBytes := AStream.Read(Buffer, SizeOf(Buffer));
      Inc(TotalBytes, ReadBytes);
      MD5Update(Context, @Buffer, ReadBytes);
    until (ReadBytes = 0) or (TotalBytes = Size);
  finally
    AStream.Seek(SavePos, soFromBeginning);
  end;
  MD5Final(Result, Context);
end;

function MD5(const ABuffer; ASize: integer): TMD5Digest;
var
  Context: TMD5Context;
begin
  MD5Init(Context);
  MD5Update(Context, PByteArray(@ABuffer), ASize);
  MD5Final(Result, Context);
end;

function CompareMD5(const ADigest1, ADigest2: TMD5Digest): boolean;
begin
  Result := (ADigest1.D1 = ADigest2.D1) and (ADigest1.D2 = ADigest2.D2);
end;

procedure XorBlock(Input1, Input2, Output: PByteArray; Length: integer);
var
  i: integer;
begin
  for i := 0 to Length - 1 do
    Output[i] := Input1[i] xor Input2[i];
end;

procedure IncBlock(P: PByteArray; Len: integer);
begin
  Inc(P[Len - 1]);
  if (P[Len - 1] = 0) and (Len > 1) then
    IncBlock(P, Len - 1);
end;

procedure BFEncryptECB(const Data: TBlowfishData; Input, Output: Pointer);
var
  L, R: longword;
begin
  Move(Input^, L, 4);
  Move(Pointer(integer(Input) + 4)^, R, 4);
  L := (L shr 24) or ((L shr 8) and $FF00) or ((L shl 8) and $FF0000) or (L shl 24);
  R := (R shr 24) or ((R shr 8) and $FF00) or ((R shl 8) and $FF0000) or (R shl 24);
  L := L xor Data.PBoxM[0];
  R := R xor (((Data.SBoxM[0, (L shr 24) and $FF] + Data.SBoxM[1, (L shr 16) and $FF])
    xor Data.SBoxM[2, (L shr 8) and $FF]) + Data.SBoxM[3, L and $FF]) xor Data.PBoxM[1];
  L := L xor (((Data.SBoxM[0, (R shr 24) and $FF] + Data.SBoxM[1, (R shr 16) and $FF])
    xor Data.SBoxM[2, (R shr 8) and $FF]) + Data.SBoxM[3, R and $FF]) xor Data.PBoxM[2];
  R := R xor (((Data.SBoxM[0, (L shr 24) and $FF] + Data.SBoxM[1, (L shr 16) and $FF])
    xor Data.SBoxM[2, (L shr 8) and $FF]) + Data.SBoxM[3, L and $FF]) xor Data.PBoxM[3];
  L := L xor (((Data.SBoxM[0, (R shr 24) and $FF] + Data.SBoxM[1, (R shr 16) and $FF])
    xor Data.SBoxM[2, (R shr 8) and $FF]) + Data.SBoxM[3, R and $FF]) xor Data.PBoxM[4];
  R := R xor (((Data.SBoxM[0, (L shr 24) and $FF] + Data.SBoxM[1, (L shr 16) and $FF])
    xor Data.SBoxM[2, (L shr 8) and $FF]) + Data.SBoxM[3, L and $FF]) xor Data.PBoxM[5];
  L := L xor (((Data.SBoxM[0, (R shr 24) and $FF] + Data.SBoxM[1, (R shr 16) and $FF])
    xor Data.SBoxM[2, (R shr 8) and $FF]) + Data.SBoxM[3, R and $FF]) xor Data.PBoxM[6];
  R := R xor (((Data.SBoxM[0, (L shr 24) and $FF] + Data.SBoxM[1, (L shr 16) and $FF])
    xor Data.SBoxM[2, (L shr 8) and $FF]) + Data.SBoxM[3, L and $FF]) xor Data.PBoxM[7];
  L := L xor (((Data.SBoxM[0, (R shr 24) and $FF] + Data.SBoxM[1, (R shr 16) and $FF])
    xor Data.SBoxM[2, (R shr 8) and $FF]) + Data.SBoxM[3, R and $FF]) xor Data.PBoxM[8];
  R := R xor (((Data.SBoxM[0, (L shr 24) and $FF] + Data.SBoxM[1, (L shr 16) and $FF])
    xor Data.SBoxM[2, (L shr 8) and $FF]) + Data.SBoxM[3, L and $FF]) xor Data.PBoxM[9];
  L := L xor (((Data.SBoxM[0, (R shr 24) and $FF] + Data.SBoxM[1, (R shr 16) and $FF])
    xor Data.SBoxM[2, (R shr 8) and $FF]) + Data.SBoxM[3, R and $FF]) xor Data.PBoxM[10];
  R := R xor (((Data.SBoxM[0, (L shr 24) and $FF] + Data.SBoxM[1, (L shr 16) and $FF])
    xor Data.SBoxM[2, (L shr 8) and $FF]) + Data.SBoxM[3, L and $FF]) xor Data.PBoxM[11];
  L := L xor (((Data.SBoxM[0, (R shr 24) and $FF] + Data.SBoxM[1, (R shr 16) and $FF])
    xor Data.SBoxM[2, (R shr 8) and $FF]) + Data.SBoxM[3, R and $FF]) xor Data.PBoxM[12];
  R := R xor (((Data.SBoxM[0, (L shr 24) and $FF] + Data.SBoxM[1, (L shr 16) and $FF])
    xor Data.SBoxM[2, (L shr 8) and $FF]) + Data.SBoxM[3, L and $FF]) xor Data.PBoxM[13];
  L := L xor (((Data.SBoxM[0, (R shr 24) and $FF] + Data.SBoxM[1, (R shr 16) and $FF])
    xor Data.SBoxM[2, (R shr 8) and $FF]) + Data.SBoxM[3, R and $FF]) xor Data.PBoxM[14];
  R := R xor (((Data.SBoxM[0, (L shr 24) and $FF] + Data.SBoxM[1, (L shr 16) and $FF])
    xor Data.SBoxM[2, (L shr 8) and $FF]) + Data.SBoxM[3, L and $FF]) xor Data.PBoxM[15];
  L := L xor (((Data.SBoxM[0, (R shr 24) and $FF] + Data.SBoxM[1, (R shr 16) and $FF])
    xor Data.SBoxM[2, (R shr 8) and $FF]) + Data.SBoxM[3, R and $FF]) xor Data.PBoxM[16];
  R := R xor Data.PBoxM[17];
  L := (L shr 24) or ((L shr 8) and $FF00) or ((L shl 8) and $FF0000) or (L shl 24);
  R := (R shr 24) or ((R shr 8) and $FF00) or ((R shl 8) and $FF0000) or (R shl 24);
  Move(R, Output^, 4);
  Move(L, Pointer(integer(Output) + 4)^, 4);
end;

⌨️ 快捷键说明

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