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

📄 encrypteddatafile.pas

📁 wbs43open-src.zip 数字隐藏工具
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit EncryptedDataFile;

// version 4.0 //

interface

uses DataFile, Classes, SysUtils, MLKBBSGenerator,
     DCPcrypt2, DCPblowfish, DCPtwofish, DCPcast128, DCPrijndael, DCPsha1
{$IFDEF CLX}
     ,QForms, QDialogs
{$ELSE}
     , Forms, Dialogs
{$ENDIF}
     ;

type
  TEncryptedDataFile = class(TDataFile)
  protected
    ControlByte:     Byte;
    Pwd:             String;
    PwdPar:          Boolean;
    Algorithm:       Byte;
    Key:             Array[0..19] of Byte;

    procedure MixPassword;
    procedure ExtractPassword;

    procedure Mix;
    procedure Unmix;

    procedure Crypt;

    procedure Header4(var hdr: Array of byte);

    procedure GenerateKey;

    procedure EncryptBlockCipher;
    procedure DecryptBlockCipher;

  public
    procedure CreateFromDataFile(DF: TDataFile; ReadControl: Boolean);
    procedure ExportToDataFile(DF: TDataFile; WriteControl: Boolean);

    procedure SetPassword(pwd: String);
    function GetPassword: String;

    procedure SetCrypt(C: Boolean);
    function GetCrypt: Boolean;

    procedure SetMix(M: Boolean);
    function GetMix: Boolean;

    procedure SetTransmit(T: Boolean);
    function GetTransmit: Boolean;

    procedure SetAlgorithm(algo: Byte);
    function GetAlgorithm: Byte;

    procedure Encrypt;
    procedure Decrypt;

  end;

implementation

   procedure TEncryptedDataFile.CreateFromDataFile(DF: TDataFile; ReadControl: Boolean);
   var
     newData:    TMemoryStream;
     CData:      Array[0..2] of Byte;
   begin
     Data.Free;
     Data:=TMemoryStream.Create;
     DF.SaveToStream(Data);
     Algorithm:=0;
     if ReadControl then begin
       Data.Seek(0,soFromBeginning);
       Data.Read(CData,1);
       ControlByte:=CData[0];
       newData:=TMemoryStream.Create;
       newData.CopyFrom(Data,Data.Size-1);
       Data.Free;
       Data:=newData;
       if not(GetMix) and not(GetCrypt) then begin
         // read header4
         Data.Seek(0,soFromBeginning);
         Data.Read(CData,3);
         if CData[0]=255 then begin
            // get algorithm
            Algorithm:=CData[2];
            // delete header
            newData:=TMemoryStream.Create;
            newData.CopyFrom(Data,Data.Size-3);
            Data.Free;
            Data:=newData;
         end;
       end;
     end
     else begin
       // create random control byte
      ControlByte:=0;
      Randomize;
      ControlByte:=ControlByte and (Random(2) shl 5);
      ControlByte:=ControlByte and (Random(2) shl 4);
      ControlByte:=ControlByte and (Random(2) shl 3);
      ControlByte:=ControlByte and (Random(2) shl 2);
      ControlByte:=ControlByte and (Random(2) shl 1);
     end;
   end;

   procedure TEncryptedDataFile.ExportToDataFile(DF: TDataFile; WriteControl: Boolean);
   var
     CData:         Array[0..2] of Byte;
     newData:       TMemoryStream;
     i:             Byte;
   begin
     if WriteControl then begin
       newData:=TMemoryStream.Create;
       CData[0]:=ControlByte;
       newData.Write(CData,1);
       if Algorithm>0 then begin
         Header4(CData);
         newData.Write(CData,3);
       end;
       Data.Seek(0,soFromBeginning);
       newData.CopyFrom(Data,Data.Size);
       Data.Free;
       Data:=newData;
     end;
     DF.LoadFromStream(Data);
   end;

   procedure TEncryptedDataFile.SetPassword(pwd: String);
   var
     i:      Integer;
   begin
     self.Pwd:=pwd;
     PwdPar:=False;
     for i:=1 to Length(self.Pwd) do begin
       if (Ord(self.Pwd[i]) and $01)=$01 then PwdPar:=not(PwdPar);
     end;
   end;

   function TEncryptedDataFile.GetPassword: String;
   begin
     GetPassword:=Pwd;
   end;

   procedure TEncryptedDataFile.SetCrypt(C: Boolean);
   begin
     if C then Algorithm:=0;
     if C then ControlByte:=ControlByte or $80
     else ControlByte:=ControlByte and $7F;
   end;

   function TEncryptedDataFile.GetCrypt: Boolean;
   begin
     if (ControlByte and $80)<>0 then GetCrypt:=True else GetCrypt:=False;
   end;

   procedure TEncryptedDataFile.SetMix(M: Boolean);
   begin
     if M then Algorithm:=0;
     if M then ControlByte:=ControlByte or $40
     else ControlByte:=ControlByte and $BF;
   end;

   function TEncryptedDataFile.GetMix: Boolean;
   begin
     if (ControlByte and $40)<>0 then GetMix:=True else GetMix:=False;
   end;

   procedure TEncryptedDataFile.SetTransmit(T: Boolean);
   var
     par:    boolean;
   begin
     par:=PwdPar;
     if not(T) then par:=not(par);
     if par then ControlByte:=ControlByte or $01
     else ControlByte:=ControlByte and $FE;
   end;

   function TEncryptedDataFile.GetTransmit: Boolean;
   var
     par:   boolean;
   begin
     if (ControlByte and $01)<>0 then par:=True else par:=False;
     if (par=PwdPar) then GetTransmit:=True else GetTransmit:=False;
   end;

   procedure TEncryptedDataFile.SetAlgorithm(algo: Byte);
   begin
     Algorithm:=algo;
   end;

   function TEncryptedDataFile.GetAlgorithm: Byte;
   begin
     GetAlgorithm:=Algorithm;
   end;

   procedure TEncryptedDataFile.Encrypt;
   begin
     if Algorithm=0 then begin
       if GetTransmit then MixPassword;
       if GetMix then Mix;
       if GetCrypt then Crypt;
     end
     else begin
       GenerateKey;
       EncryptBlockCipher;
     end;
   end;

   procedure TEncryptedDataFile.Decrypt;
   begin
     if Algorithm=0 then begin
       if GetCrypt then Crypt;
       if GetMix then Unmix;
       if GetTransmit then ExtractPassword;
     end
     else begin
       GenerateKey;
       DecryptBlockCipher;
     end;
   end;

   ////////////////////////////////////////////////////////////////////////

   procedure TEncryptedDataFile.MixPassword;
   var
     newData:       TMemoryStream;
     DBuffer:       Array[0..1023] of Byte;
     PwBuffer:      Array[0..255] of Byte;
     DAsked:        Integer;
     DRead:         Integer;
     i:             Integer;
     offset:        Integer;
     pwdptr:        Integer;
   begin
     newData:=TMemoryStream.Create;
     Data.Seek(0,soFromBeginning);
     pwdptr:=1;
     offset:=Ord(Pwd[pwdptr]) and $3F;
     DAsked:=offset;
     DRead:=DAsked;
     while DRead=DAsked do begin
       DAsked:=offset;
       DRead:=Data.Read(DBuffer,DAsked);
       newData.Write(DBuffer,DRead);
       if offset<64 then begin
         PwBuffer[0]:=Ord(Pwd[pwdptr]);
         newData.Write(PwBuffer,1);
         if pwdptr<Length(Pwd) then begin
           Inc(pwdPtr);
           offset:=Ord(Pwd[pwdptr]) and $3F;
         end
         else offset:=1024;
       end;
     end;
     if pwdptr<Length(Pwd) then begin
       for i:=0 to (Length(Pwd)-pwdptr-1) do begin
         PwBuffer[i]:=Ord(Pwd[i+pwdptr+1]);
       end;
       newData.Write(PwBuffer,Length(Pwd)-pwdptr);
     end;
     Data.Free;
     Data:=newData;
   end;

   procedure TEncryptedDataFile.ExtractPassword;
   var
     newData:       TMemoryStream;
     DBuffer:       Array[0..1023] of Byte;
     PwBuffer:      Array[0..1] of Byte;
     DAsked:        Integer;
     DRead:         Integer;
     i:             Integer;
     offset:        Integer;
     pwdptr:        Integer;
     newPwd:        String;
     pwdleft:       Integer;
   begin
     newPwd:='';

⌨️ 快捷键说明

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