2
« Poslední příspěvek od rmaster kdy Dnes v 15:59:29 »
Zdravím, potřebuji zdánlivě jednoduchou věc, nad kterou už bádám druhý den a jsem v koncích.
Chci na serveru v PHP zašifrovat textový soubor, který předem vygeneruji, pak ho přenést na Windows (to nyní neřeším) a ten soubor následně načíst mojí aplikací a rozšifrovat. Používám Delphi XE3.
Mělo by se jednat o offline registraci mého programu, když uživatel nemá přístup k internetu.
Vše mi funguje, ale obsah zdrojového soubor před zašifrováním a obsah cílového souboru po dešifrování je úplně jiný.
Nedokázal by někdo poradit? Moc Děkuji.
<?php
$inputFile = 'soubor.txt';
$outputFile = 'soubor_encrypted.txt';
$key = 'tajny_klic_12345';
$iv = openssl_random_pseudo_bytes(16); // Inicializační vektor
$data = file_get_contents($inputFile);
$ciphertext = openssl_encrypt($data, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);
$ivAndCiphertext = $iv . $ciphertext;
$base64EncodedData = base64_encode($ivAndCiphertext);
file_put_contents($outputFile, $base64EncodedData);
echo "Soubor byl úspěšně zašifrován a uložen ve formátu Base64.";
?>
uses
System.SysUtils, System.Classes, DCPrijndael, DCPcrypt2;
const
Base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
function DecodeBase64(const Input: string): TBytes;
var
i, j, Triplet: Integer;
OutputIndex: Integer;
PadCount: Integer;
begin
PadCount := 0;
for i := Length(Input) downto 1 do
begin
if Input[i] = '=' then
Inc(PadCount)
else
Break;
end;
SetLength(Result, (Length(Input) * 3) div 4 - PadCount);
OutputIndex := 0;
for i := 1 to Length(Input) div 4 do
begin
Triplet := (Pos(Input[(i - 1) * 4 + 1], Base64Chars) - 1) shl 18 or
(Pos(Input[(i - 1) * 4 + 2], Base64Chars) - 1) shl 12 or
(Pos(Input[(i - 1) * 4 + 3], Base64Chars) - 1) shl 6 or
(Pos(Input[(i - 1) * 4 + 4], Base64Chars) - 1);
Result[OutputIndex] := (Triplet shr 16) and $FF;
Result[OutputIndex + 1] := (Triplet shr 8) and $FF;
Result[OutputIndex + 2] := Triplet and $FF;
OutputIndex := OutputIndex + 3;
end;
if PadCount = 1 then
SetLength(Result, Length(Result) - 1);
if PadCount = 2 then
SetLength(Result, Length(Result) - 2);
end;
procedure DecryptFile(const EncryptedFile, DecryptedFile, Key: string);
var
InputStream, OutputStream: TFileStream;
Base64EncodedData: string;
IVAndCiphertext, IV, EncryptedData: TBytes;
Rijndael: TDCP_rijndael;
begin
InputStream := TFileStream.Create(EncryptedFile, fmOpenRead);
try
SetLength(Base64EncodedData, InputStream.Size);
InputStream.ReadBuffer(Base64EncodedData[1], InputStream.Size);
IVAndCiphertext := DecodeBase64(Base64EncodedData);
SetLength(IV, 16);
Move(IVAndCiphertext[0], IV[0], 16);
SetLength(EncryptedData, Length(IVAndCiphertext) - 16);
Move(IVAndCiphertext[16], EncryptedData[0], Length(EncryptedData));
Rijndael := TDCP_rijndael.Create(nil);
try
Rijndael.Init(Key[1], 256, IV); // AES-256, klíč o délce 256 bitů
Rijndael.Decrypt(EncryptedData[0], EncryptedData[0], Length(EncryptedData));
OutputStream := TFileStream.Create(DecryptedFile, fmCreate);
try
OutputStream.WriteBuffer(EncryptedData[0], Length(EncryptedData));
finally
OutputStream.Free;
end;
finally
Rijndael.Free;
end;
finally
InputStream.Free;
end;
end;
..........................
// Použití
begin
// Zavoláme funkci pro dešifrování
DecryptFile('soubor_encrypted.txt', 'soubor_decrypted.txt', 'tajny_klic_12345');
// Výsledek je uložen do souboru 'soubor_decrypted.txt'
ShowMessage('Soubor byl úspěšně dešifrován a uložen.');
end.