Einzelnen Beitrag anzeigen

pko

Registriert seit: 4. Sep 2008
Ort: Niederrhein
5 Beiträge
 
Delphi XE2 Enterprise
 
#3

AW: DECUtil alt auf DECUtil 5.2

  Alt 5. Jan 2012, 19:19
schau mal

var
ACipherClass: TDECCipherClass = TCipher_Blowfish; //TCipher_1DES;//TCipher_Rijndael;
ACipherMode: TCipherMode = cmCTSx;
AHashClass: TDECHashClass = THash_Whirlpool;
(** oder *******
THash_MD2
THash_MD4
THash_MD5
THash_RipeMD128
THash_RipeMD160
THash_RipeMD256
THash_RipeMD320
THash_SHA
THash_SHA1
THash_SHA256
THash_SHA384
THash_SHA512
THash_Haval128
THash_Haval160
THash_Haval192
THash_Haval224
THash_Haval256
THash_Tiger
THash_Panama
THash_Whirlpool
THash_Whirlpool1
THash_Square
THash_Snefru128
THash_Snefru256
THash_Sapphire
*********)
ATextFormat: TDECFormatClass = TFormat_HEX; //TFormat_Mime64;
AKDFIndex: LongWord = 1;



function TRegisterClass.EncodeCipher( Input, Key: String ): String;
var
ASalt: Binary;
AData: Binary;
APass: Binary;
begin
with ValidCipher(ACipherClass).Create, Context do
try
ASalt := RandomBinary(16);
APass := ValidHash(AHashClass).KDFx(Key[1], Length(Key) * SizeOf(Key[1]), ASalt[1], Length(ASalt), KeySize, TFormat_Copy, AKDFIndex);
Mode := ACipherMode;
Init(APass);
SetLength(AData, Length(Input) * SizeOf(Input[1]));
Encode(Input[1], AData[1], Length(AData));
Result := ValidFormat(ATextFormat).Encode(ASalt + AData + CalcMAC);
finally
Free;
ProtectBinary(ASalt);
ProtectBinary(AData);
ProtectBinary(APass);
end;
ShowMessage('EncodeCipher: '+result);
end;

function TRegisterClass.DecodeCipher( Input, Key: String ): String;
var
ASalt: Binary;
AData: Binary;
ACheck: Binary;
APass: Binary;
ALen: Integer;
begin
with ValidCipher(ACipherClass).Create, Context do
try
ASalt := ValidFormat(ATextFormat).Decode(Input);
ALen := Length(ASalt) - 16 - BufferSize;
AData := System.Copy(ASalt, 17, ALen);
ACheck := System.Copy(ASalt, ALen + 17, BufferSize);
SetLength(ASalt, 16);
APass := ValidHash(AHashClass).KDFx(Key[1], Length(Key) * SizeOf(Key[1]), ASalt[1], Length(ASalt), KeySize, TFormat_Copy, AKDFIndex);
Mode := ACipherMode;
Init(APass);
SetLength(Result, ALen div SizeOf(Input[1]));
Decode(AData[1], Result[1], ALen);
if ACheck <> CalcMAC then
raise Exception.Create('Invalid data');
finally
Free;
ProtectBinary(ASalt);
ProtectBinary(AData);
ProtectBinary(ACheck);
ProtectBinary(APass);
end;
ShowMessage('DecodeCipher: '+result);
end;
Klaus
  Mit Zitat antworten Zitat