virus_forever
virus_forever

Reputation: 23

Delphi CryptoBlackBox and PHP

Hello

<?php
$string = "Some text to be encrypted";
$secret_key = "51f732e39e5d800569802df7c37631f4";

$iv = "0123456789abcdef";

$encrypted_string = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $secret_key, $string, MCRYPT_MODE_CBC, $iv);

$decrypted_string = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $secret_key, $encrypted_string, MCRYPT_MODE_CBC, $iv);

echo "Original string : " . $string . "<br />\n";
echo "Encrypted string : " . $encrypted_string . "<br />\n";
echo "Decrypted string : " . $decrypted_string . "<br />\n";
echo "----<br />\n";
echo "IV " . $iv . "<br />\n";
echo "IV " . base64_encode($iv) . "<br />\n";
echo "Encrypted string : " . base64_encode($encrypted_string) . "<br />\n";
?>

Result: ... Encrypted string : r+zYEk/vwa9kjJ62Y4e0X9WK2uUhMEjPTeeLy7E/UgU=

In delphi try use free library CryptoBlackBox

const
 iviv:   RawByteString = '0123456789abcdef';
 keykey: RawByteString = '51f732e39e5d800569802df7c37631f4';

procedure TForm1.sButton1Click(Sender: TObject);
var
 Crypto : TElSymmetricCrypto;
 KeyMaterial : TElSymmetricKeyMaterial;
 Factory : TElSymmetricCryptoFactory;

 Secret: ByteArray;
 IV: ByteArray;

 Data: RawByteString;
 Input, Output: ByteArray;

 Result: AnsiString;

 OutSize: Integer;
begin
 Factory := TElSymmetricCryptoFactory.Create;

 SetLength(Secret, Length(keykey));
 Move(keykey[1], Secret[0], Length(keykey));

 SetLength(IV, Length(iviv));
 Move(iviv[1], IV[0], Length(iviv));

 KeyMaterial:= TElSymmetricKeyMaterial.Create;
 KeyMaterial.Key:= Secret;
 KeyMaterial.IV:= IV;

 ShowMessage('IV Length: ' + length(KeyMaterial.IV).ToString);

 Crypto := Factory.CreateInstance(SB_ALGORITHM_CNT_AES256, cmCBC);

 Crypto.KeyMaterial:= KeyMaterial;

 Data:= DecodeBase64(sEdit2.Text);

 SetLength(Input, Length(Data));
 Move(Data[1], Input[0], Length(Data));

 ShowMessage('Input Length: ' + length(Input).ToString);

 try
  OutSize := 0;
  Crypto.Decrypt(@Input[0], Length(Input), nil, OutSize);

  ShowMessage('Length: ' + IntToStr(OutSize));

  SetLength(Output, OutSize);
  Crypto.Decrypt(@Input[0], Length(Input), @Output[0], OutSize);
  SetLength(Output, OutSize);

  SetLength(Result, OutSize);
  Move(Output[0], Result[1], OutSize);

  ShowMessage(Result);
 except
  on E: Exception do
   ShowMessage(E.Message);
 end;
end; 

As Result - Exception:

First chance exception at $77511D4D. Exception class EElSymmetricCryptoError with message 'Invalid symmetric cipher padding'.

May be encountered someone with this? Like all the same - the key and initialization vector, the data needed to accurately decrypt.

Upvotes: 2

Views: 490

Answers (1)

Maarten Bodewes
Maarten Bodewes

Reputation: 93988

MCRYPT_RIJNDAEL_256 is not AES, it is the Rijndael cipher with a block size of 256 bits, instead of AES with a key size of 256. Use MCRYPT_RIJNDAEL_128 with a correctly sized key instead.

Also make sure that your character encoding and padding modes match. I could not find the padding mechanism for your Delphi code, PHP uses zero-byte padding up to the block boundary (0..15 bytes).

Upvotes: 2

Related Questions