As we talked in the comments, I am passing how I use the library Dcpcrypt 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 3DES, but this library has a wide range of algorithmic options, you can utilize whatever you prefer.
In the example I used a txt file to facilitate the example, but this code works for any type of file.
This example was created using the official dcpcrypt demos and documentation.
What you need is to put the file extension in the
windows
and encrypt the contents of the file so that another program or other personnel cannot identify.– Roberto de Campos
Question of putting the extension in the windows registry I was already studying however how to encrypt the contents of the file without corrupting it and still making only my software open it
– Z04h
To be clearer would create a Winrar of life where it is an Executable in which generates another executable with files within that
– Z04h
Your description is a little confusing. It seems to me that you want your program to encrypt the backup files, and only it can decompress them. Is this it? If yes, there is a wide range of encryption libraries that you can use for this development, e.g.: Dcpcrypt
– Confundir
That so only my program could open the file
– Z04h