Delphi关于改进AES算法的加解密文件功能

以下代码出自http://blog.csdn.net/hellogv/,引用请注明出处!


代码有点乱,想要具体实现方法的朋友,可以给我留言

附:以下代码在杨泽晖 (Jorlen Young)所开发的AES算法接口上改进!

function StrToHex(Value: string): string;

var

  I: Integer;

begin

  Result := '';

  for I := 1 to Length(Value) do

    Result := Result + IntToHex(Ord(Value[I]), 2);

end;



 



function EncryptString(Value: string; Key: string): string;

var

  SS, DS: TStringStream;

  Size: Int64;

  AESKey256: TAESKey256;

begin

  Result := '';

  SS := TStringStream.Create(Value);

  DS := TStringStream.Create('');

  try

    Size := SS.Size;

    DS.WriteBuffer(Size, SizeOf(Size));

    FillChar(AESKey256, SizeOf(AESKey256), 0 );

    Move(PChar(Key)^, AESKey256, Min(SizeOf(AESKey256), Length(Key)));

    EncryptAESStreamECB(SS, 0, AESKey256, DS);

    Result := StrToHex(DS.DataString);

  finally

    SS.Free;

    DS.Free;

  end;

end;



function HexToStr(Value: string): string;

var

  I: Integer;

begin

  Result := '';

  for I := 1 to Length(Value) do

  begin

    if ((I mod 2) = 1) then

      Result := Result + Chr(StrToInt('0x'+ Copy(Value, I, 2)));

  end;

end;



function DecryptString(Value: string; Key: string): string;

var

  SS, DS: TStringStream;

  Size: Int64;

  AESKey256: TAESKey256;

begin

  Result := '';

  SS := TStringStream.Create(HexToStr(Value));

  DS := TStringStream.Create('');

  try

    Size := SS.Size;

    SS.ReadBuffer(Size, SizeOf(Size));

    FillChar(AESKey256, SizeOf(AESKey256), 0 );

    Move(PChar(Key)^, AESKey256, Min(SizeOf(AESKey256), Length(Key)));

    DecryptAESStreamECB(SS, SS.Size - SS.Position, AESKey256, DS);

    Result := DS.DataString;

  finally

    SS.Free;

    DS.Free;

  end;

end;



// ---------- 文件加密函数 按照 256 位密匙加密

procedure TSysClass.EncryptFile(SourceFile, DestFile: string;Key: string);

var

  SFS, DFS: TFileStream;

  Size: Int64;

  AESKey256: TAESKey256;

  info:string;

begin



  SFS := TFileStream.Create(SourceFile, fmOpenRead);

  try

    DFS := TFileStream.Create(DestFile, fmCreate);

    try

    //-----------------------------文件解密部分---------------------------------

      Size := SFS.Size;

      DFS.WriteBuffer(Size, SizeOf(Size));

      FillChar(AESKey256, SizeOf(AESKey256), 0 );

      Move(PChar(Key)^, AESKey256, Min(SizeOf(AESKey256), Length(Key)));

      EncryptAESStreamECB(SFS, 0, AESKey256, DFS);

    //-----------------------------在文件尾部加入判断解密是否成功的信息----------

      info:=EncryptString('123456789',Key);

      DFS.Seek(0,soFromEnd);

      DFS.Write(Info[1],60);//往后面写入60个字符,其中有一些保留没有使用

    finally

      DFS.Free;

    end;

  finally

    SFS.Free;

  end;

end;



// ---------- 文件解密函数 按照 256 位密匙解密

Function TSysClass.DecryptFile(SourceFile, DestFile: string;Key: string):boolean;

var

  Test, DFS: TFileStream;

  SFS:TMemoryStream;

  Size: Int64;

  AESKey256: TAESKey256;

  Info:string;

begin

  SetLength(Info,60);

 //----------------------------------------先检测文件解密是否正确-------------------

  Test := TFileStream.Create(SourceFile, fmOpenRead);

  try

    Test.Seek(-60,soFromEnd);

    Test.Read(Info[1],60);

    //-----------------------------判断密码是否正确,如果错误

    if AnsiContainsStr(Trim(info),EncryptString('123456789',Key)) = false then begin

       result:=false;//解密失败

       Test.Free;//释放流

       exit;

    end;

    //-----------------------------如果密码正确,取得实际文件的内容

    Test.Seek(0,soFromBeginning);

    SFS:=TMemoryStream.Create;

    SFS.CopyFrom(Test,Test.Size-60);

    Test.Free;

  except //-----------------------如果出现异常不能打开文件

    SFS.Free;

    Test.Free;

    result:=false;

    exit;

  end;

  //-------------------------------正式开始解密

  SFS.Position:=0;

  SFS.ReadBuffer(Size, SizeOf(Size));

  DFS := TFileStream.Create(DestFile, fmCreate);

  FillChar(AESKey256, SizeOf(AESKey256), 0 );

  Move(PChar(Key)^, AESKey256, Min(SizeOf(AESKey256), Length(Key)));

  DecryptAESStreamECB(SFS, SFS.Size - SFS.Position, AESKey256, DFS);

  DFS.Size := Size;

  DFS.Free;

  SFS.Free;

  result:=true;

end;

//-----------------------【加密解密基础函数】---------------------------------------------------- 

你可能感兴趣的:(算法,.net,Blog,Delphi)