AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Programmieren allgemein Delphi Hilfe bei Umstellung Unit DEC5.1 zu DEC6.0
Thema durchsuchen
Ansicht
Themen-Optionen

Hilfe bei Umstellung Unit DEC5.1 zu DEC6.0

Ein Thema von haentschman · begonnen am 11. Jan 2024 · letzter Beitrag vom 31. Jan 2024
 
EdAdvokat

Registriert seit: 1. Mai 2016
Ort: Berlin
419 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#19

AW: Hilfe bei Umstellung Unit DEC5.1 zu DEC6.0

  Alt 26. Jan 2024, 15:04
Damit wir nicht nur über Code-Schnipsel sprechen hier die gesamte unit, die nun funktioniert,
wahlweise ergänzt mit HashClass:= TDECHashClass(THash.SHA256.create); :
Delphi-Quellcode:
unit Tools.Crypt;

interface

uses
  System.SysUtils,
  DECUtil, DECHash,
  DECHashBase, DECCipherBase, DECCiphers, DECFormatBase, DECFormat,
  DECHashAuthentication, DECRandom;

const
  conKey = 'aYr14iaz8u)xO7Ok';

var
  CipherMode: TCipherMode = cmCBCx;
  HashClass: TDECHashClass = THash_SHA256;
  TextFormat: TDECFormatClass = TFormat_Base64;
  KDFIndex: LongWord = 1;

type
  TToolsCrypt = class
  public
    class function Decrypt(aHash: string; aKey: string = ''): string;
    class function Encrypt(aText: string; aKey: string = ''): string;
  end;

implementation

{ TToolsCrypt }

class function TToolsCrypt.Decrypt(aHash, aKey: string): string;
var
  Cipher: TCipher_Rijndael;
  Salt: RawByteString; //Binary;
  Data: RawByteString;
  Check: RawByteString;
  Pass: RawByteString;
  PassBytes: TBytes;
  Len: Integer;
begin
  if aKey = 'then
  begin
    aKey := conKey;
  end;

  Cipher := TCipher_Rijndael.Create;
  try
    Salt := ValidFormat(TextFormat).Decode(RawByteString(aHash));
    Len := Length(Salt) - 16 - Cipher.Context.BufferSize;
    Data := Copy(Salt, 17, Len);
    Check := Copy(Salt, Len + 17, Cipher.Context.BufferSize);
    SetLength(Salt, 16);
    PassBytes := THash_SHA256.KDFx(aKey[1],
                                  Length(aKey) * 2,
                                  Salt[1],
                                  Length(Salt),
                                  Cipher.Context.KeySize,
                                  KDFIndex);

    SetLength(Pass, Length(PassBytes));
    Move(PassBytes[0], Pass[low(Pass)], Length(PassBytes));

    Cipher.Mode := CipherMode;
    Cipher.Init(Pass);
    SetLength(Result, Len div 2);
    Cipher.Decode(Data[1], Result[1], Len);
    if Check <> Cipher.CalcMAC then
    begin
      Result := '';
    end;
  finally
    Cipher.Free;
    //ProtectBinary(Salt);
    ProtectString(Salt);
    ProtectString(Data);
    ProtectString(Check);
    ProtectString(Pass);
  end;
end;

class function TToolsCrypt.Encrypt(aText, aKey: string): string;
var
  Cipher: TCipher_Rijndael;
  SaltBytes : TBytes;
  Salt: RawByteString; //Binary;
  Data: RawByteString; //Binary;
  Pass: RawByteString; //Binary;
  PassBytes: TBytes;
begin
  if aKey = 'then
  begin
    aKey := conKey;
  end;

  Cipher := TCipher_Rijndael.Create;
  try
    SaltBytes := RandomBytes(16); //RandomBinary(16);
    SetLength(Salt, Length(SaltBytes));
    Move(SaltBytes[0], Salt[low(Salt)], Length(SaltBytes));

    PassBytes := THash_SHA256.KDFx(aKey[1],
                                  Length(aKey) * 2,
                                  Salt[1],
                                  Length(Salt),
                                  Cipher.Context.KeySize,
                                  KDFIndex);

    SetLength(Pass, Length(PassBytes));
    Move(PassBytes[0], Pass[low(Pass)], Length(PassBytes));

    Cipher.Mode := CipherMode;
    Cipher.Init(Pass);
    SetLength(Data, Length(aText) * 2);
    Cipher.Encode(aText[1], Data[1], Length(Data));
    Result := string(ValidFormat(TextFormat).Encode(Salt + Data + Cipher.CalcMAC));
  finally
    Cipher.Free;
    ProtectString(Salt);
    ProtectString(Data);
    ProtectString(Pass);
  end;
end;

end.
Hier wird
Delphi-Quellcode:
PassBytes:= THash_SHA256.KDFx(aKey[1],
                                  Length(aKey) * 2,
                                  Salt[1],
                                  Length(Salt),
                                  Cipher.Context.KeySize,
                                  KDFIndex);
anders als zuvor aufgerufen.
Diese unit funktioniert bei mir mit oder ohne den gesonderten Aufruf von HashClass:=.....
ohne Fehler und Warnungen
Schau noch mal in #12 nach
Norbert

Geändert von EdAdvokat (26. Jan 2024 um 15:08 Uhr)
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 21:34 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz