Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#12

AW: Passwort Entschlüsseln

  Alt 30. Okt 2014, 13:06
Wenn man einen Blick auf das DEC wirft
... habe mich ein bisschen mit dem Delphi Encryption Compendium 5 auseinander gesetzt.
dann sollte einem doch auch die Datei Example.txt ins Auge springen.

Und bis auf die Tatsache, dass dort mit with warum auch immer das Lesen und Verstehen des Codes erschwert wird, ist dort aber genau das enthalten, was hier benötigt wird.
Delphi-Quellcode:
var
  ACipherClass: TDECCipherClass = TCipher_Rijndael;
  ACipherMode: TCipherMode = cmCBCx;
  AHashClass: TDECHashClass = THash_Whirlpool;
  ATextFormat: TDECFormatClass = TFormat_Mime64;
  AKDFIndex: LongWord = 1;

function Encrypt( const AText: String; const APassword: String ): String; overload;
function Decrypt( const AText: String; const APassword: String ): String; overload;
Damit das Verstehen besser wird habe ich mal dieses with entfernt und die Parameter mit Kommentaren versehen.
Delphi-Quellcode:
program dp_182529;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils,
  DECUtil, DECCipher, DECHash, DECFmt;

var
  ACipherClass: TDECCipherClass = TCipher_Rijndael;
  ACipherMode: TCipherMode = cmCBCx;
  AHashClass: TDECHashClass = THash_Whirlpool;
  ATextFormat: TDECFormatClass = TFormat_Mime64;
  AKDFIndex: LongWord = 1;

function Encrypt( const AText: String; const APassword: String ): String;
var
  LSalt: Binary;
  LData: Binary;
  LPass: Binary;
  LCipher: TDECCipher;
begin
  LCipher := ValidCipher(
    { CipherClass } ACipherClass ).
  { > } Create;
  try
    LSalt := RandomBinary( 16 );
    LPass := ValidHash(
      { HashClass } AHashClass ).
    { > } KDFx(
      { Data } APassword[1],
      { DataSize } Length( APassword ) * SizeOf( APassword[1] ),
      { Seed } LSalt[1],
      { SeedSize } Length( LSalt ),
      { MaskSize } LCipher.Context.KeySize,
      { Format } TFormat_Copy,
      { Index } AKDFIndex );
    LCipher.Mode := ACipherMode;
    LCipher.Init( LPass );
    SetLength( LData, Length( AText ) * SizeOf( AText[1] ) );
    LCipher.Encode(
      { Source } AText[1],
      { Dest } LData[1],
      { DataSize } Length( LData ) );
    Result := ValidFormat(
      { FormatClass } ATextFormat ).
    { > } Encode(
      { Value } LSalt + LData + LCipher.CalcMAC );
  finally
    LCipher.Free;
    ProtectBinary( LSalt );
    ProtectBinary( LData );
    ProtectBinary( LPass );
  end;
end;

function Decrypt( const AText: String; const APassword: String ): String;
var
  LSalt: Binary;
  LData: Binary;
  LCheck: Binary;
  LPass: Binary;
  LLen: Integer;
  LCipher: TDECCipher;
begin
  LCipher := ValidCipher(
    { CipherClass } ACipherClass ).
  { > } Create;
  try
    LSalt := ValidFormat(
      { FormatClass } ATextFormat ).
    { > } Decode(
      { Value } AText );
    LLen := Length( LSalt ) - 16 - LCipher.Context.BufferSize;
    LData := System.Copy( LSalt, 17, LLen );
    LCheck := System.Copy( LSalt, LLen + 17, LCipher.Context.BufferSize );
    SetLength( LSalt, 16 );
    LPass := ValidHash(
      { HashClass } AHashClass ).
    { > } KDFx(
      { Data } APassword[1],
      { DataSize } Length( APassword ) * SizeOf( APassword[1] ),
      { Seed } LSalt[1],
      { SeedSize } Length( LSalt ),
      { MaskSize } LCipher.Context.KeySize,
      { Format } TFormat_Copy,
      { Index } AKDFIndex );
    LCipher.Mode := ACipherMode;
    LCipher.Init(
      { Key } LPass );
    SetLength( Result, LLen div SizeOf( AText[1] ) );
    LCipher.Decode(
      { Source } LData[1],
      { Dest } Result[1],
      { DataSize } LLen );
    if LCheck <> LCipher.CalcMAC
    then
      raise Exception.Create( 'Invalid data' );
  finally
    LCipher.Free;
    ProtectBinary( LSalt );
    ProtectBinary( LData );
    ProtectBinary( LCheck );
    ProtectBinary( LPass );
  end;
end;

procedure Main;
var
  LText: string;
  LPass: string;
begin
  LText := 'The quick brown fox jumps over the lazy dog';
  LPass := 'U5r5klO0zwu674593';

  Writeln( 'Encode Test: ', Encrypt( LText, LPass ) );
  Writeln( 'Decode Test: ', Decrypt( Encrypt( LText, LPass ), LPass ) );
end;

begin
  try
    Main;
  except
    on E: Exception do
      Writeln( E.ClassName, ': ', E.Message );
  end;
  ReadLn;

end.
Mit der Basis müsste man jetzt auch weiterkommen ...
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat