Einzelnen Beitrag anzeigen

BlackSeven

Registriert seit: 25. Sep 2004
79 Beiträge
 
Delphi XE7 Professional
 
#1

AES: Entschlüsselung JavaScript -> Delphi

  Alt 1. Feb 2013, 09:38
Hallo!

Ich möchte einen in JavaScript per AES verschlüsselten Text in Delphi entschlüsseln.
Leider bisher ohne Erfolg.

JavaScript AES:
code.google.com/p/crypto-js

Text: message
Passwort: test
Verschlüsselter Text: U2FsdGVkX19h+WKvkdYWILeBgb10Yg6pVrXS1qAQmmo=
IV:aa2d712f7ee98cba8fb3019a5e26f821
Ciphertext:b78181bd74620ea956b5d2d6a0109a6a
Salt:61f962af91d61620
Key:9b1c3e329b5e061a571afac99de6c4368390a5475fb60c 5faabb7

Delphi-Quellcode:
uses
  DECUtil, DECCipher, DECHash, DECFmt,
  X.System.Data;

{$R *.dfm}

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

function DoDecrypt(const AText: WideString; const APassword: WideString): WideString; overload;
var
  ASalt: Binary;
  AData: Binary;
  ACheck: Binary;
  APass: Binary;
  ALen: Integer;

begin

  with ValidCipher(ACipherClass).Create, Context do
  try

    ASalt := ValidFormat(ATextFormat).Decode(AText);
    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(APassword[1], Length(APassword) * SizeOf(APassword[1]), ASalt[1], Length(ASalt), KeySize, TFormat_Copy, AKDFIndex);
    Mode := ACipherMode;

    Init(APass);

    SetLength(Result, ALen div SizeOf(AText[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;

end;

procedure TForm1.DecryptClick(Sender: TObject);
var
  Key: WideString;
  Text: WideString;

begin

  Key := 'test';
  Text := 'U2FsdGVkX19h+WKvkdYWILeBgb10Yg6pVrXS1qAQmmo=';

  Output.Text := DoDecrypt(Text, Key);

end;
Für Hilfe bin ich sehr dankbar!
  Mit Zitat antworten Zitat