Einzelnen Beitrag anzeigen

Benutzerbild von haentschman
haentschman

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

Verschlüsselung: Unterschied von const und var ???

  Alt 5. Okt 2017, 06:33
Hallöle...

Ich hätte dann mal ein Problem... Ich habe eine Verschlüsselungsmethode, geborgt aus dem Internet, die gut funktioniert. Diese wollte ich erweitern...nun kommt als Result nichts raus.

Delphi-Quellcode:
// extra Unit
const
  conKey = 'ZdrjE[dyhf';

var
  aCipherMode: TCipherMode = cmCBCx;
  aHashClass: TDECHashClass = THash_SHA256;
  aTextFormat: TDECFormatClass = TFormat_MIME64;
  aKDFIndex: LongWord = 1;
Original:
Delphi-Quellcode:
class function TdTools.Decrypt(aHash: string): string;
var
  Cipher: TCipher_Rijndael;
  aSalt: Binary;
  aData: Binary;
  aCheck: Binary;
  aPass: Binary;
  aLen: Integer;
begin
  Cipher := TCipher_Rijndael.Create;
  try
    aSalt := ValidFormat(aTextFormat).Decode(RawByteString(aHash));
    aLen := Length(aSalt) - 16 - Cipher.Context.BufferSize;
    aData := Copy(aSalt, 17, aLen);
    aCheck := Copy(aSalt, aLen + 17, Cipher.Context.BufferSize);
    SetLength(aSalt, 16);
    aPass := ValidHash(aHashClass).KDFx(conKey[1], Length(conKey) * SizeOf(conKey[1]), aSalt[1], Length(aSalt), Cipher.Context.KeySize, TFormat_Copy, aKDFIndex); // Konstante
    Cipher.Mode := aCipherMode;
    Cipher.Init(aPass);
    SetLength(Result, aLen div SizeOf(aHash[1]));
    Cipher.Decode(aData[1], Result[1], aLen);
    if aCheck <> Cipher.CalcMAC then
      Result := '';
  finally
    Cipher.Free;
    ProtectBinary(aSalt);
    ProtectBinary(aData);
    ProtectBinary(aCheck);
    ProtectBinary(aPass);
  end;
end;
Erweiterungen:
Delphi-Quellcode:
class function TdTools.Encrypt(aText: string; const aKey: string): string;
var
  Cipher: TCipher_Rijndael;
  aSalt: Binary;
  aData: Binary;
  aPass: Binary;
  Key: string;
begin
  if aKey = 'then
  begin
    Key := conKey;
  end
  else
  begin
    Key := aKey;
  end;
  Cipher := TCipher_Rijndael.Create;
  try
    aSalt := RandomBinary(16);
    aPass := ValidHash(aHashClass).KDFx(Key, Length(Key) * SizeOf(Key[1]), aSalt[1], Length(aSalt), Cipher.Context.KeySize, TFormat_Copy, aKDFIndex); // Variable
    Cipher.Mode := aCipherMode;
    Cipher.Init(aPass);
    SetLength(aData, Length(aText) * SizeOf(aText[1]));
    Cipher.Encode(aText[1], aData[1], Length(aData));
    Result := string(ValidFormat(aTextFormat).Encode(aSalt + aData + Cipher.CalcMAC));
  finally
    Cipher.Free;
    ProtectBinary(aSalt);
    ProtectBinary(aData);
    ProtectBinary(aPass);
  end;
end;
Seit gestern ist es zu früh für das Problem... Da kann man eigentlich nichts falsch machen. Ich tippe auf den Unterschied von const und var...aber warum?

Danke für Infos...

Geändert von haentschman ( 5. Okt 2017 um 07:22 Uhr)
  Mit Zitat antworten Zitat