![]() |
AES encryption with Header
I use the following the Code to encrypt my file using EAS algo :
I use this Interface to the AES functions written by Brian Gladman : ![]()
Delphi-Quellcode:
the encryption Routine works well but the decryption is not working and no file is being saved .
const HeadSig = 'mee'; // Header Sig
type TMyFileHead = packed record Signature : array [0..2] of char; // header sig flVer : String [4];// File Version end; type { * encryption routine *} function Enc(inStream, outStream :TStream; Header: TMyFileHead; const PWD :String='') : boolean; begin inStream.Seek(0,soFromBeginning); outStream.Write(Header,SizeOf(TMyFileHead));// Write my Header Info to the Stream outStream.Seek(SizeOf(Header),soFromBeginning); // Start the AES encryption routine with TEncryption.Create(PWD,defCryptBufSize) do begin if EncryptStream(inStream,outStream) then result:=true else result:=false; Free; end; end; // example Of use procedure TForm1.Button3Click(Sender: TObject); var H : TMyFileHead; F : TMemoryStream; F1 : TMemoryStream; begin H.Signature:=HeadSig;// write my header Signature "mee" F := TMemoryStream.Create; F1 := TMemoryStream.Create; F.LoadFromFile('myfile.txt'); // encrypt myfile.txt if Enc(F,F1,H)then F1.SaveToFile(ChangeFileExt('myfile.txt','.enc')); F.Free; F1.Free; end; { * decryption routine *} function Dec(inStream, outStream :TStream; var Header: TMyFileHead; const PWD :String=''):boolean; begin inStream.Seek(0,soFromBeginning); inStream.Read(Header,SizeOf(TMyFileHead)); // file header<>'mee' if Header.Signature <> HeadSig then begin // invalid File Signature so Stop the Decryption Operation . Result := False; Exit; end; inStream.Seek(0,SizeOf(TMyFileHead)); outStream.Seek(0,soFromBeginning); with TEncryption.Create(PWD,defCryptBufSize) do begin if DecryptStream(inStream,outStream,instream.Size) then result:=true else result:=false; Free; end; // example of use procedure TForm1.Button4Click(Sender: TObject); var F : TMemoryStream; F1 : TMemoryStream; h : TMyFileHead; begin F := TMemoryStream.Create; F1 := TMemoryStream.Create; // load the encrypted file F.LoadFromFile('myfile.enc'); if Dec(F,F1,H) then // save the decrypted file f1.SaveToFile(ChangeFileExt('myfile.enc','.dec')); F.Free; F1.Free; end; end; What's incorrect with my Decryption Routine please ?? |
Re: AES encryption with Header
Hi there,
Delphi-Quellcode:
does dec give a true or false back?
if Dec(F,F1,H) then
begin f1.position:=0; // new // save the decrypted file f1.SaveToFile(ChangeFileExt('myfile.enc','.dec')); end; else showMessage('error ind dec') F.Free; F1.Free; Best regards Klaus |
Re: AES encryption with Header
thank you Klaus01 , now it works ( what a worse error i did in forgetting the f1 reposition )....
the dec result is based on the DecryptStream() result . Zitat:
|
Re: AES encryption with Header
i've noticed that in the decrypted file there's an extra string added at the end of file i think it's the Header Info !!!
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 02:52 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz