As we talked in the comments, I'm going to use the DCPCrypt library for encrypt / decrypt files. This can be used as a component in delphi, it is free and open source.
Encrypt / decrypt file:
uses
DCPcrypt2, System.Math, DCPsha512, DCPdes;
procedure EncriptaArquivo(aArquivo: string; aArquivoEncriptado: string; aHash: TDCP_hash; aCipher: TDCP_cipher; aSenha: String);
var
CipherIV: array of byte;
HashDigest: array of byte;
Salt: array[0..7] of byte;
strmInput, strmOutput: TFileStream;
i: integer;
begin
strmInput := nil;
strmOutput := nil;
try
strmInput := TFileStream.Create(aArquivo,fmOpenRead);
strmOutput := TFileStream.Create(aArquivoEncriptado,fmCreate);
SetLength(HashDigest,aHash.HashSize div 8);
for i := 0 to 7 do
Salt[i] := Random(256);
strmOutput.WriteBuffer(Salt,Sizeof(Salt));
aHash.Init;
aHash.Update(Salt[0],Sizeof(Salt));
aHash.UpdateStr(aSenha);
aHash.Final(HashDigest[0]);
if (aCipher is TDCP_blockcipher) then
begin
SetLength(CipherIV,TDCP_blockcipher(aCipher).BlockSize div 8);
for i := 0 to (Length(CipherIV) - 1) do
CipherIV[i] := Random(256);
strmOutput.WriteBuffer(CipherIV[0],Length(CipherIV));
aCipher.Init(HashDigest[0],System.Math.Min(aCipher.MaxKeySize,aHash.HashSize),CipherIV);
TDCP_blockcipher(aCipher).CipherMode := cmCBC;
end
else
aCipher.Init(HashDigest[0],Min(aCipher.MaxKeySize,aHash.HashSize),nil);
aCipher.EncryptStream(strmInput,strmOutput,strmInput.Size);
aCipher.Burn;
strmInput.Free;
strmOutput.Free;
except
strmInput.Free;
strmOutput.Free;
MessageDlg('Um erro aconteceu no processo de encriptação',mtError,[mbOK],0);
end;
end;
procedure DesencriptaArquivo(aArquivoEncriptado: string; aArquivoDesencriptado: string; aHash: TDCP_hash; aCipher: TDCP_cipher; aSenha: String);
var
CipherIV: array of byte;
HashDigest: array of byte;
Salt: array[0..7] of byte;
strmInput, strmOutput: TFileStream;
begin
strmInput := nil;
strmOutput := nil;
try
strmInput := TFileStream.Create(aArquivoEncriptado,fmOpenRead);
strmOutput := TFileStream.Create(aArquivoDesencriptado,fmCreate);
SetLength(HashDigest,aHash.HashSize div 8);
strmInput.ReadBuffer(Salt[0],Sizeof(Salt));
aHash.Init;
aHash.Update(Salt[0],Sizeof(Salt));
aHash.UpdateStr(aSenha);
aHash.Final(HashDigest[0]);
if (aCipher is TDCP_blockcipher) then
begin
SetLength(CipherIV,TDCP_blockcipher(aCipher).BlockSize div 8);
strmInput.ReadBuffer(CipherIV[0],Length(CipherIV));
aCipher.Init(HashDigest[0],Min(aCipher.MaxKeySize,aHash.HashSize),CipherIV);
TDCP_blockcipher(aCipher).CipherMode := cmCBC;
end
else
aCipher.Init(HashDigest[0],Min(aCipher.MaxKeySize,aHash.HashSize),nil);
aCipher.DecryptStream(strmInput,strmOutput,strmInput.Size - strmInput.Position);
aCipher.Burn;
strmInput.Free;
strmOutput.Free;
except
strmInput.Free;
strmOutput.Free;
MessageDlg('Um erro aconteceu no processo de desencriptação',mtError,[mbOK],0);
end;
end;
Encryption example:
var
vHash: TDCP_hash;
vCipher: TDCP_3des;
vSenha: String;
begin
vSenha := 'abobrinha123';
vHash := TDCP_sha512.Create(nil);
vCipher := TDCP_3des.Create(nil);
try
EncriptaArquivo('d:\ArquivoOriginal.txt', 'd:\ArquivoEncriptado.txt', vHash, vCipher, vSenha);
finally
FreeAndNil(vHash);
FreeAndNil(vCipher);
end;
end;
Decryption example:
var
vHash: TDCP_hash;
vCipher: TDCP_3des;
vSenha: String;
begin
vSenha := 'abobrinha123';
vHash := TDCP_sha512.Create(nil);
vCipher := TDCP_3des.Create(nil);
try
DesencriptaArquivo('d:\ArquivoEncriptado.txt', 'd:\ArquivoDesencriptado.txt', vHash, vCipher, vSenha);
finally
FreeAndNil(vHash);
FreeAndNil(vCipher);
end;
end;
In the example I used the hash algorithm sha-512 and cipher #
In the example I used a txt file to make the example easier, but this code works for any type of file.
This example was created using dcpcrypt official demos and documentation.