EldoS | Feel safer!

Software components for data protection, secure storage and transfer

PHP <> Delphi = Invalid symmetric cipher padding

Also by EldoS: MsgConnect
Cross-platform protocol-independent communication framework for building peer-to-peer and client-server applications and middleware components.
#30248
Posted: 08/09/2014 17:33:09
by strelok-2007 (Basic support level)
Joined: 08/09/2014
Posts: 1

Hello
I use Delphi XE6 and free pack CryptoBlackBox as free part SecureBlackBox

Try estabiled encryption from php server to delphi applications.

PHP
Code
<?php
/*
* PHP mcrypt - Basic encryption and decryption of a string
*/
$string = "hello php to delphi encryption";

echo "Length : " . strlen($string) . "<br />\n";


$secret_key = "51f732e39e5d800569802df7c37631f4";

// Create the initialization vector for added security.
//$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);

//$iv = "d2328087c75596d1b0569d99cf3c2487";
$iv = "0123456789abcdef";

$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_256, 'cbc');
echo "Block Size : " . $size . "<br />\n";

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

// Decrypt $string
$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";
?>


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

implementation

{$R *.dfm}


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('Secret Length: ' + length(KeyMaterial.Key).ToString);
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
PHP
Code
Length : 30
Block Size : 32
Original string : hello php to delphi encryption
Encrypted string : F�̟����;�\�&�����ӭ���(
Decrypted string : hello php to delphi encryption
----
IV 0123456789abcdef
IV MDEyMzQ1Njc4OWFiY2RlZg==
Encrypted string : RtLMn+6qf4b/O7BcDOcmE6Ia+RnF6bvTre7U9gwLKAM=


Delphi:
Code
First chance exception at $77511D4D. Exception class EElSymmetricCryptoError with message 'Invalid symmetric cipher padding'. Process CryptoBlackBoxTest.exe (5776)


What to do?

Thanks!
#30249
Posted: 08/10/2014 02:53:33
by Eugene Mayevski (EldoS Corp.)

According to the license russians may not use our products. Stop using CryptoBlackbox immediately and delete it.


Sincerely yours
Eugene Mayevski

Reply

Statistics

Topic viewed 3444 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!