Einzelnen Beitrag anzeigen

Hedge

Registriert seit: 30. Jun 2007
278 Beiträge
 
Delphi 2009 Professional
 
#1

dcpcrypt + Delphi 2009 = falsches en/decryption

  Alt 4. Dez 2009, 20:44
Ich habe heute die aktuelle Version von dcpcrypt die für Delphi 2009 geeignet sein soll installiert.

Auf der Seite von dcpcrypt gibt es ein Beispiel zur Kommunikation mit PHP-Scripts.

Die bereits kompilierte Exe funktioniert tadellos (nach encrypten einer Zeichenkette wieder in die Ausgangszeichenfolge decrypten).

Wenn ich das Beispiel selbst kompiliere kommt ein ganz anderer Ciphertext beim encrypten raus und entsprechend auch ein falsches Ergebnis beim decrypten.

Der Code ent- und verschlüsseln ist folgender:

Delphi-Quellcode:
// Encrypt a string and return the Base64 encoded result
procedure TfrmMain.btnEncryptClick(Sender: TObject);
var
  Cipher : TDCP_rijndael;
  Data, Key, IV : string;
begin
  // Pad Key, IV and Data with zeros as appropriate
  Key := PadWithZeros(boxKey.Text,KeySize);
  IV := PadWithZeros(boxIV.Text,BlockSize);
  Data := PadWithZeros(boxPlainTextIn.Text,BlockSize);
  // Create the cipher and initialise according to the key length
  Cipher := TDCP_rijndael.Create(Self);
  if Length(boxKey.Text) <= 16 then
    Cipher.Init(Key[1],128,@IV[1])
  else if Length(boxKey.Text) <= 24 then
    Cipher.Init(Key[1],192,@IV[1])
  else
    Cipher.Init(Key[1],256,@IV[1]);
  // Encrypt the data
  Cipher.EncryptCBC(Data[1],Data[1],Length(Data));
  // Free the cipher and clear sensitive information
  Cipher.Free;
  FillChar(Key[1],Length(Key),0);
  // Display the Base64 encoded result
  boxCipherTextOut.Text := Base64EncodeStr(AnsiString(Data));
end;

procedure TfrmMain.btnDecryptClick(Sender: TObject);
var
  Cipher : TDCP_rijndael;
  Data, Key, IV : string;
begin
  // Pad Key and IV with zeros as appropriate
  Key := PadWithZeros(boxKey.Text,KeySize);
  IV := PadWithZeros(boxIV.Text,BlockSize);
  // Decode the Base64 encoded string
  Data := Base64DecodeStr(AnsiString(boxCipherTextIn.Text));
  // Create the cipher and initialise according to the key length
  Cipher := TDCP_rijndael.Create(Self);
  if Length(boxKey.Text) <= 16 then
    Cipher.Init(Key[1],128,@IV[1])
  else if Length(boxKey.Text) <= 24 then
    Cipher.Init(Key[1],192,@IV[1])
  else
    Cipher.Init(Key[1],256,@IV[1]);
  // Decrypt the data
  Cipher.DecryptCBC(Data[1],Data[1],Length(Data));
  // Free the cipher and clear sensitive information
  Cipher.Free;
  FillChar(Key[1],Length(Key),0);
  // Display the result
  boxPlainTextOut.Text := Data;
end;
٩๏̯͡๏)۶
  Mit Zitat antworten Zitat