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

📄 gost.pas

📁 MiniHex 1.1 源程序说明 “MiniHex11SrcSource”目录中的所有文件是MiniHex 1.1的主程序; “MiniHex11SrcControls”目录中的是该软件
💻 PAS
📖 第 1 页 / 共 2 页
字号:
begin
  if (Len <> 32) then
    raise Exception.Create('Gost: Invalid key length');
  UserKey:= Key;
  with FGostData do
  begin
    if IV= nil then
    begin
      FillChar(InitBlock,8,0);
      FillChar(LastBlock,8,0);
    end
    else
    begin
      Move(IV^,InitBlock,8);
      Move(IV^,LastBlock,8);
    end;
    for i:= 0 to 7 do
      XKey[i]:= (UserKey[4*i+3] shl 24) or (UserKey[4*i+2] shl 16) or
        (UserKey[4*i+1] shl 8) or (UserKey[4*i+0]);
  end;
end;

procedure TGost.GostBurn(var FGostData: TGostData);
begin
  FillChar(FGostData,Sizeof(FGostData),0);
end;

function f(const x: DWord): DWord;
begin
  Result:= sTable[3,x shr 24] xor sTable[2,(x shr 16) and $FF] xor sTable[1,(x shr 8) and $FF] xor sTable[0,x and $FF];
end;

procedure TGost.GostEncryptECB(InData, OutData: pointer);
var
  n1, n2: DWord;
  i: integer;
begin
  Move(InData^,n1,4);
  Move(pointer(integer(InData)+4)^,n2,4);
  for i:= 0 to 2 do
  begin
    n2:= n2 xor f(n1+FGostData.XKey[0]);
    n1:= n1 xor f(n2+FGostData.XKey[1]);
    n2:= n2 xor f(n1+FGostData.XKey[2]);
    n1:= n1 xor f(n2+FGostData.XKey[3]);
    n2:= n2 xor f(n1+FGostData.XKey[4]);
    n1:= n1 xor f(n2+FGostData.XKey[5]);
    n2:= n2 xor f(n1+FGostData.XKey[6]);
    n1:= n1 xor f(n2+FGostData.XKey[7]);
  end;
  n2:= n2 xor f(n1+FGostData.XKey[7]);
  n1:= n1 xor f(n2+FGostData.XKey[6]);
  n2:= n2 xor f(n1+FGostData.XKey[5]);
  n1:= n1 xor f(n2+FGostData.XKey[4]);
  n2:= n2 xor f(n1+FGostData.XKey[3]);
  n1:= n1 xor f(n2+FGostData.XKey[2]);
  n2:= n2 xor f(n1+FGostData.XKey[1]);
  n1:= n1 xor f(n2+FGostData.XKey[0]);
  Move(n2,OutData^,4);
  Move(n1,pointer(integer(OutData)+4)^,4);
end;

procedure TGost.GostDecryptECB(InData, OutData: pointer);
var
  n1, n2: DWord;
  i: integer;
begin
  Move(InData^,n1,4);
  Move(pointer(integer(InData)+4)^,n2,4);
  n2:= n2 xor f(n1+FGostData.XKey[0]);
  n1:= n1 xor f(n2+FGostData.XKey[1]);
  n2:= n2 xor f(n1+FGostData.XKey[2]);
  n1:= n1 xor f(n2+FGostData.XKey[3]);
  n2:= n2 xor f(n1+FGostData.XKey[4]);
  n1:= n1 xor f(n2+FGostData.XKey[5]);
  n2:= n2 xor f(n1+FGostData.XKey[6]);
  n1:= n1 xor f(n2+FGostData.XKey[7]);
  for i:= 0 to 2 do
  begin
    n2:= n2 xor f(n1+FGostData.XKey[7]);
    n1:= n1 xor f(n2+FGostData.XKey[6]);
    n2:= n2 xor f(n1+FGostData.XKey[5]);
    n1:= n1 xor f(n2+FGostData.XKey[4]);
    n2:= n2 xor f(n1+FGostData.XKey[3]);
    n1:= n1 xor f(n2+FGostData.XKey[2]);
    n2:= n2 xor f(n1+FGostData.XKey[1]);
    n1:= n1 xor f(n2+FGostData.XKey[0]);
  end;
  Move(n2,OutData^,4);
  Move(n1,pointer(integer(OutData)+4)^,4);
end;

procedure TGost.GostEncryptCBC(InData, OutData: pointer);
begin
  XorBlock(InData,@FGostData.LastBlock,OutData,8);
  GostEncryptECB(OutData,OutData);
  Move(OutData^,FGostData.LastBlock,8);
end;

procedure TGost.GostDecryptCBC(InData, OutData: pointer);
var
  TempBlock: array[0..7] of byte;
begin
  Move(InData^,TempBlock,8);
  GostDecryptECB(InData,OutData);
  XorBlock(OutData,@FGostData.LastBlock,OutData,8);
  Move(TempBlock,FGostData.LastBlock,8);
end;

procedure TGost.GostEncryptCFB(InData, OutData: pointer; Len: integer);
var
  i: integer;
  TempBlock: array[0..7] of byte;
begin
  for i:= 0 to Len-1 do
  begin
    GostEncryptECB(@FGostData.LastBlock,@TempBlock);
    PByteArray(OutData)[i]:= PByteArray(InData)[i] xor TempBlock[0];
    Move(FGostData.LastBlock[1],FGostData.LastBlock[0],7);
    FGostData.LastBlock[7]:= PByteArray(OutData)[i];
  end;
end;

procedure TGost.GostDecryptCFB(InData, OutData: pointer; Len: integer);
var
  i: integer;
  TempBlock: array[0..7] of byte;
  b: byte;
begin
  for i:= 0 to Len-1 do
  begin
    b:= PByteArray(InData)[i];
    GostEncryptECB(@FGostData.LastBlock,@TempBlock);
    PByteArray(OutData)[i]:= PByteArray(InData)[i] xor TempBlock[0];
    Move(FGostData.LastBlock[1],FGostData.LastBlock[0],7);
    FGostData.LastBlock[7]:= b;
  end;
end;

procedure TGost.GostEncryptOFB(InData, OutData: pointer);
begin
  GostEncryptECB(@FGostData.LastBlock,@FGostData.LastBlock);
  XorBlock(@FGostData.LastBlock,InData,OutData,8);
end;

procedure TGost.GostDecryptOFB(InData, OutData: pointer);
begin
  GostEncryptECB(@FGostData.LastBlock,@FGostData.LastBlock);
  XorBlock(@FGostData.LastBlock,InData,OutData,8);
end;

procedure TGost.GostEncryptOFBC(InData, OutData: pointer; Len: integer);
var
  i: integer;
  TempBlock: array[0..7] of byte;
begin
  for i:= 0 to Len-1 do
  begin
    GostEncryptECB(@FGostData.LastBlock,@TempBlock);
    PByteArray(OutData)[i]:= PByteArray(InData)[i] xor TempBlock[0];
    IncBlock(@FGostData.LastBlock,8);
  end;
end;

procedure TGost.GostDecryptOFBC(InData, OutData: pointer; Len: integer);
var
  i: integer;
  TempBlock: array[0..7] of byte;
begin
  for i:= 0 to Len-1 do
  begin
    GostEncryptECB(@FGostData.LastBlock,@TempBlock);
    PByteArray(OutData)[i]:= PByteArray(InData)[i] xor TempBlock[0];
    IncBlock(@FGostData.LastBlock,8);
  end;
end;

procedure TGost.Reset;
begin
  Move(FGostData.InitBlock,FGostData.LastBlock,8);
end;

end.

⌨️ 快捷键说明

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