Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Library: Algorithmen (https://www.delphipraxis.net/28-library-algorithmen/)
-   -   Prism Speicherblock mit Rjindael verschlüsseln/entschlüsseln (https://www.delphipraxis.net/68778-speicherblock-mit-rjindael-verschluesseln-entschluesseln.html)

xaromz 21. Apr 2006 09:44


Speicherblock mit Rjindael verschlüsseln/entschlüsseln
 
Hallo,

unter .Net verwendet man die Klassen aus dem Namespace System.Security.Cryptography.
Hier mal zwei Beispiele zum Ver-/Entschlüsseln eines Speicherblocks mit Rijndael:

Delphi-Quellcode:
type
  Bytes = array of Byte;

function Encrypt(Buffer, Key, IV: Bytes): Bytes;
var
  Encryptor: ICryptoTransform;
  ST: MemoryStream;
  Encrypt: CryptoStream;
  FAlgorythm: RijndaelManaged;
begin
  // Vernünftige Argumente?
  if (IV = '') or (Key = '') then
  begin
    Result := Buffer;
    Exit;
  end;

  // Vorbereiten
  FAlgorythm := RijndaelManaged.Create;
  FAlgorythm.IV := IV;
  FAlgorythm.Key := Key;
  FAlgorythm.Mode := CipherMode.CBC;
  FAlgorythm.Padding := PaddingMode.Zeros;
  Encryptor := FAlgorythm.CreateEncryptor;

  // Stream erstellen
  ST := MemoryStream.Create;
  Encrypt := CryptoStream.Create(ST, Encryptor, CryptoStreamMode.Write);

  // Speicher in Stream schreiben
  Encrypt.Write(Buffer, 0, Length(Buffer));
  Encrypt.FlushFinalBlock;

  // Ergebnis abgreifen
  Result := ST.ToArray;
end;

function Decrypt(Buffer, Key, IV: Bytes): Bytes;
var
  Decryptor: ICryptoTransform;
  ST: MemoryStream;
  Decrypt: CryptoStream;
  FAlgorythm: RijndaelManaged;
begin
  if (IV = '') or (Key = '') then
  begin
    Result := Buffer;
    Exit;
  end;

  FAlgorythm := RijndaelManaged.Create;
  FAlgorythm.IV := IV;
  FAlgorythm.Key := Key;
  FAlgorythm.Mode := CipherMode.CBC;
  FAlgorythm.Padding := PaddingMode.Zeros;

  Decryptor := FAlgorythm.CreateDecryptor;

  ST := MemoryStream.Create(Buffer);
  Decrypt := CryptoStream.Create(ST, Decryptor, CryptoStreamMode.Read);

  SetLength(Result, ST.Length);

  Decrypt.Read(Result, 0, ST.Length);
end;
Gruß
xaromz

[edit=Matze]Beitrag etwas angepasst. Mfg, Matze[/edit]


Alle Zeitangaben in WEZ +1. Es ist jetzt 07:29 Uhr.

Powered by vBulletin® Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2020 by Daniel R. Wolf