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
Antwort Antwort
Benutzerbild von haentschman
haentschman

Registriert seit: 24. Okt 2006
Ort: Seifhennersdorf / Sachsen
5.460 Beiträge
 
Delphi 12 Athens
 
#1

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

  Alt 26. Jan 2024, 06:56
Moin...

...hat etwas länger gedauert.

1. PassBytes := THash_SHA256( getauscht
2. schöne Zugriffsverletzung wie vorher ...wie jetzt?
3. Erinnerung an "...nur in einer konkreten Klasse vernünftig funktionieren kann."
4. Klasse erzeugt (Bild)
5. und siehe da...es funktioniert. (Bild)

...und keine Leaks.

PS: Warnung beseitigt: TextFormat: TDECFormatClass = TFormat_Base64; statt TextFormat: TDECFormatClass = TFormat_MIME64;


Danke nochmal...
Angehängte Grafiken
Dateityp: jpg Encrypt.jpg (73,5 KB, 25x aufgerufen)

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

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

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

  Alt 26. Jan 2024, 14:17
Ich frage mal ganz naiv: muß wirklich HashClass:=TDECHashClass(THash_SHA256.create); und deren Freigabe eingefügt werden.

ich habe in der class function ToolCrypt.Encode und ...Decode lediglich
PassBytes := THash_SHA256.KDFx(aKey[1],...geändert, ohne
HashClass:=TDECHashClass(THash.SHA256.create); und deren Freigabe aufzurufen.
Das hat ohne Fehler und Warnungen compiliert.

Muß ich wirklich das create aufrufen?
TDECFormatClass rufe ich ja auch nicht mit create auf

Ich habe beide Varianten ausprobiert und das gleiche Ergebnis: keine Fehler, keine Warnungen
Norbert
  Mit Zitat antworten Zitat
Benutzerbild von haentschman
haentschman

Registriert seit: 24. Okt 2006
Ort: Seifhennersdorf / Sachsen
5.460 Beiträge
 
Delphi 12 Athens
 
#3

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

  Alt 26. Jan 2024, 14:44
Hi...

im Original ist:
Delphi-Quellcode:
PassBytes := TDECHashAuthentication(ValidHash(HashClass)).KDFx(aKey[1],
...
...da fehlt was bei dir
Delphi-Quellcode:
THash_SHA256.KDFx(aKey[1]
...
-> ValidHash(HashClass)
...mit
Delphi-Quellcode:
PassBytes := THash_SHA256(ValidHash(HashClass)).KDFx(aKey[1],
...
...ergibt es eine Zugriffsverletzung.

THash_SHA256.KDFx(aKey[1] ...probiere ich mal aus. (geht)

PS: Wofür ist diese Prüfung da? (ValidHash)

Angehängte Grafiken
Dateityp: png Error.PNG (26,4 KB, 9x aufgerufen)

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

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

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
Benutzerbild von haentschman
haentschman

Registriert seit: 24. Okt 2006
Ort: Seifhennersdorf / Sachsen
5.460 Beiträge
 
Delphi 12 Athens
 
#5

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

  Alt 26. Jan 2024, 15:10
Zitat:
Diese unit funktioniert bei mir mit oder ohne den gesonderten Aufruf von HashClass:=.....
ohne Fehler und Warnungen
D10.2 <> D12 ... macht vieleicht was aus.
  Mit Zitat antworten Zitat
EdAdvokat

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

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

  Alt 26. Jan 2024, 15:28
ich nutze aktuell die CE 11.3 auch für dieses Projekt mit Windows 11
TurboMagic nutzt wohl Delphi 12 und der hat den Fehler erkannt und die Änderung beschrieben. Also sollte es nicht an der Delphiversion liegen können.
Norbert

Geändert von EdAdvokat (26. Jan 2024 um 16:20 Uhr)
  Mit Zitat antworten Zitat
Kas Ob.

Registriert seit: 3. Sep 2023
458 Beiträge
 
#7

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

  Alt 26. Jan 2024, 17:29
I looked at the library,
ValidFormat and ValidHash supposed to be working as factory design, you feed them a class type and will get a class, this implementation to remove the need to hardcode a switch between algorithms.

https://en.wikipedia.org/wiki/Factory_method_pattern
but with simple function instead of a class.
Kas
  Mit Zitat antworten Zitat
TurboMagic

Registriert seit: 28. Feb 2016
Ort: Nordost Baden-Württemberg
3.096 Beiträge
 
Delphi 12 Athens
 
#8

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

  Alt 26. Jan 2024, 17:52
ich nutze aktuell die CE 11.3 auch für dieses Projekt mit Windows 11
TurboMagic nutzt wohl Delphi 12 und der hat den Fehler erkannt und die Änderung beschrieben. Also sollte es nicht an der Delphiversion liegen können.
Nee, liegt nicht an der Delphi version. Ich habe bis vor kurzem noch hauptsächlich die 11.3 genutzt.
Ob ich die 12.0 nutze sag' ich nicht. Ätsch!
  Mit Zitat antworten Zitat
Kas Ob.

Registriert seit: 3. Sep 2023
458 Beiträge
 
#9

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

  Alt 29. Jan 2024, 11:43
Please try this:
Code:
unit Tools.Crypt;

interface

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

const
  conKey = 'aYr14iaz8u)xO7Ok';

var
  //CipherAlgo: TCipher_Rijndael;
  CipherMode: TCipherMode = cmCBCx;
  HashClass : TDECHashClass = THash_SHA256;
  TextFormat: TDECFormatClass = TFormat_Base64;
  KDFIndex : Integer = 1; // Iterations

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;
  //Cipher: TDECCipher;
  Salt, Data, Check, Pass, Mac: TBytes;
  MacLength, SaltLen, DataLen: Integer;
begin
  if aKey = '' then
  begin
    aKey := conKey;
  end;

  //Cipher:=ValidCipher(TCipher_Rijndael).Create;      // the way it should be used ! but it fail now with DecodeBytes
  Cipher := TCipher_Rijndael.Create;
  try
    MacLength := Cipher.Context.BlockSize;
    SaltLen := Cipher.InitVectorSize;

    Salt := ValidFormat(TextFormat).Decode(BytesOf(aHash));
    DataLen := Length(Salt) - MacLength - Cipher.Context.BufferSize;

    Data := Copy(Salt, MacLength, DataLen);
    Check := Copy(Salt, DataLen + SaltLen, Cipher.Context.BufferSize);

    SetLength(Salt, SaltLen);
    Pass := ValidAuthenticationHash(HashClass).KDFx(aKey[Low(aKey)], Length(aKey) * SizeOf(Char), Salt[Low(Salt)], Length(Salt), Cipher.Context.KeySize, KDFIndex);

    Cipher.Mode := CipherMode;
    Cipher.Init(Pass, nil);
    SetLength(Result, DataLen div SizeOf(Char));

    Cipher.Decode(Data[Low(Data)], Result[Low(Result)], DataLen);
    //Result := StringOf(Cipher.DecodeBytes(Data,TFormat_Copy));
    Mac := BytesOf(Cipher.CalcMAC);
    if MacLength < Length(Check) then
      MacLength := Length(Check);

    if (MacLength <> Cipher.Context.BlockSize) or (not CompareMem(Check, Mac, MacLength)) then
    begin
      Result := '';
    end;

  finally
    Cipher.Free;
    ProtectBytes(Salt);
    ProtectBytes(Check);
    ProtectBytes(Data);
    ProtectBytes(Pass);
    ProtectBytes(Mac);
  end;
end;

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

  Cipher := TCipher_Rijndael.Create;
  try
    Salt := RandomBytes(Cipher.InitVectorSize);
    Pass := ValidAuthenticationHash(HashClass).KDFx(aKey[Low(aKey)], Length(aKey) * SizeOf(Char), Salt[Low(Salt)], Length(Salt), Cipher.Context.KeySize, KDFIndex);
    Cipher.Mode := CipherMode;

    Cipher.Mode := cmCBCx;
    Cipher.Init(Pass, nil);

    SetLength(Data, Length(aText) * SizeOf(Char));
    Cipher.Encode(aText[Low(aText)], Data[Low(Data)], Length(Data));

    Mac := BytesOf(Cipher.CalcMAC);
    Result := StringOf(ValidFormat(TextFormat).Encode(Salt + Data + Mac));
  finally
    Cipher.Free;
    ProtectBytes(Salt);
    ProtectBytes(Pass);
    ProtectBytes(Data);
    ProtectBytes(Mac);
  end;
end;

end.
Kas
  Mit Zitat antworten Zitat
Benutzerbild von haentschman
haentschman

Registriert seit: 24. Okt 2006
Ort: Seifhennersdorf / Sachsen
5.460 Beiträge
 
Delphi 12 Athens
 
#10

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

  Alt 30. Jan 2024, 08:31
Hallo...

Zum Verständnis:
Der Fehler kommt NICHT sofort, sondern nach hunderten Aufrufen des GLEICHEN SQL aus der Ressource. (Bild + SQL)
Das SQL wird JEDE Minute im Execute des Threads ausgeführt. Im Fehlerfalle (Fehlermeldung von einem anderen SQL) ist das SQL leer = beschädigt.
"[FireDAC][Phys][MSSQL]-306. Anweisungstext [] darf nicht leer sein."
Damit kann er auch den Parameter nicht finden...
Delphi-Quellcode:
procedure TDatabaseState.OfflineTime(Workstation: string; InstanzID: Integer; TimeStamp: TDateTime);
var
  Qry: TFDQuery;
begin
  Qry := CreateQuery;
  try
    Qry.SQL.Text := GetSQLByName('COM_OFFLINE_TIME');
    Qry.ParamByName('HAN').AsInteger := InstanzID; //<-
    Qry.ParamByName('TIM').AsDateTime := TimeStamp;
    Qry.ParamByName('WOR').AsString := Workstation;
    Qry.ExecSQL;
  finally
    Qry.Free;
  end;
end;
...
function TDatabaseBase.GetSQLByName(SQLName: string): string;
var
  SQLStream: TResourceStream;
  SQLStrings: TStringList;
  SQLStringsDecrypt: TStringList;
begin
  Result := '';
  SQLStrings := TStringList.Create;
  try
    SQLStringsDecrypt := TStringList.Create;
    try
      SQLStream := TResourceStream.Create(HInstance, SQLName, PWideChar(conDatabaseResourceGroupString));
      try
        try
          SQLStrings.LoadFromStream(SQLStream);
          SQLStringsDecrypt.Text := TToolsCrypt.Decrypt(SQLStrings.Text, conKey); //<-
          SQLStringsDecrypt.Delete(0); // Kommentar entfernen
          Result := SQLStringsDecrypt.Text;
        except
          Result := '';
        end;
      finally
        SQLStream.Free;
      end;
    finally
      SQLStringsDecrypt.Free;
    end;
  finally
    SQLStrings.Free;
  end;
end;
Zitat:
i asked for specific test vector to guarantee the migration of your code, and you didn't answer with one specific value
siehe hier: https://www.delphipraxis.net/1531964-post9.html

PS: Nach Umstellung wieder auf V5.2, ist bei den Usern wieder Ruhe...
Angehängte Grafiken
Dateityp: png Fehler.PNG (36,3 KB, 14x aufgerufen)
Angehängte Dateien
Dateityp: zip SQL_Time.zip (1.023 Bytes, 3x aufgerufen)

Geändert von haentschman (30. Jan 2024 um 08:43 Uhr)
  Mit Zitat antworten Zitat
Antwort Antwort


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 23:49 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