Conforme conversamos nos comentários, estou passando como utilizo a biblioteca DCPCrypt para criptografar/descriptografar arquivos. Esta pode ser utilizada como componente no delphi, é gratuito e open source.
Criptografar/descriptografar arquivo:
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;
Exemplo encriptação:
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;
Exemplo desencriptação:
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;
No exemplo utilizei o algoritmo de hash sha-512 e cifra 3DES, mas esta biblioteca tem uma vasta gama de opções de algoritmos, você pode utilizar o que você preferir.
No exemplo utilizei um arquivo txt pra facilitar o exemplo, mas este código funciona para qualquer tipo de arquivo.
Este exemplo foi criado utilizando os demos e documentação oficias do dcpcrypt.
windows
e criptografar o conteúdo do arquivo para que outro programa ou outra pessoal não consiga identificar.