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

本文提供了一套基于AES算法的字符串及文件加解密方法,包括256位密钥的加密函数、解密函数,并详细展示了如何进行文件级别的加密与解密操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下代码出自http://blog.youkuaiyun.com/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; //-----------------------【加密解密基础函数】----------------------------------------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值