Y
Y
Yakov Kravtsov2014-08-10 00:55:54
PHP
Yakov Kravtsov, 2014-08-10 00:55:54

How to properly organize DelphiPHP encryption?

Greetings!
I decided to set up client-server encryption. As a client programming language - Delphi XE6, as a server - PHP 5.5. For PHP, you don’t have to choose much - MCrypt, but for Delphi - I decided to look, because what DCPCrypto knew earlier is probably outdated morally and physically. Almost immediately I came across SecureBlackBox - a free set of components and even support for D20 (that is, Delphi XE6) is.
Installed. I started trying to do something, I tried, I tried - it didn’t work out. Maybe someone has come across and can help?
Code in PHP

<?php
/*
 * PHP mcrypt - Basic encryption and decryption of a string
 */
$string = "Some text to be encrypted";
$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";

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

As a result:
Original string : Some text to be encrypted
Encrypted string : Odc_Պ!0HM˱?R
Decrypted string : Some text to be encrypted
----
IV 0123456789abcdef
IV MDEyMzQ1Njc4OWFiY2RlZg==
Encrypted string : r+zYEk/vwa9kjJ62Y4e0X9WK2uUhMEjPTeeLy7E/UgU=

Code in Delphi
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 a result:
Ошибка - First chance exception at $77511D4D. Exception class EElSymmetricCryptoError with message 'Invalid symmetric cipher padding'.

What could be the matter, and what is the best thing to do in this situation? Here he swears at padding, but as I understand it, this is alignment - but key 32, vector 16, data 32 - everything is aligned anyway, then where?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question