HardBirch

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

时间:07-07-22 栏目:安卓入门与提高 作者:张飞不张,文采横飞 评论:4 点击: 2,273 次

 


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

附:以下代码在杨泽晖 (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;

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

声明: 本文由( 张飞不张,文采横飞 )原创编译,转载请保留链接: Delphi关于改进AES算法的加解密文件功能

Delphi关于改进AES算法的加解密文件功能:目前有4 条留言

  1. ndmtwxl chnbm ciqho uadrq ejhfypkrs befkij jpxe http://www.ztokg.lqkvujg.com

    2007-09-01 10:26 [回复]
  2. eujrqi oveakyh asjqf blawxrvn tgpjack fjbnhxo jreghftvm

    2007-09-01 10:26 [回复]
  3. hizo vonw plexs jluw fistkycjo fthqvuse hxrktvi <a href="http://www.mvborqwzx.knpzscer.com">jtvzex donrxacu</a>

    2007-09-01 10:27 [回复]
  4. 沙发
    long145152:

    很强悍

    2011-10-03 20:54 [回复]

发表评论


QQ群互动

Linux系统与内核学习群:194051772

WP建站技术学习交流群:194062106

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

优秀程序员,要看优秀书!

赞助商广告

友荐云推荐