#1 2014-02-02 16:44:45

Swanty
Member
Registered: 2014-02-02
Posts: 3

TAESCBC compatibility

So basically I'm trying to use SynCrypto TAESCBC in my Delphi application and PHP.

The code that I have in php:

<?php
function addpadding($string, $blocksize = 16)
{
    $len = strlen($string);
    $pad = $blocksize - ($len % $blocksize);
    $string .= str_repeat(chr($pad), $pad);
    return $string;
}

function encrypt($data) 
{ 
    $iv = "AAAAAAAAAAAAAAAA";
    $key = 'AAAAAAAAAAAAAAAA';

    return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, addpadding($data), MCRYPT_MODE_CBC, $iv));
}

echo encrypt('test');
?>


Code that I have in C#:

        private string AES_Key = @"AAAAAAAAAAAAAAAA";
        private string AES_IV = @"AAAAAAAAAAAAAAAA";

        private String AES_encrypt(String Input)
        {
            var aes = new RijndaelManaged();
            aes.KeySize = 128;
            aes.BlockSize = 128;
            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.PKCS7;
            aes.Key = Encoding.UTF8.GetBytes(AES_Key);
            aes.IV = Encoding.UTF8.GetBytes(AES_IV);

            var encrypt = aes.CreateEncryptor(aes.Key, aes.IV);
            byte[] xBuff = null;
            using (var ms = new MemoryStream())
            {
                using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write))
                {
                    byte[] xXml = Encoding.UTF8.GetBytes(Input);
                    cs.Write(xXml, 0, xXml.Length);
                }
                xBuff = ms.ToArray();
            }

            String Output = Convert.ToBase64String(xBuff);
            return Output;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(AES_encrypt("test"));
        }


Code in Delphi:

Uses ...SynCommons, SynCrypto...

procedure TForm1.Button1Click(Sender: TObject);
var
  aes: TAESCBC;
  iv: TAESBlock;
begin
  FillChar(iv, 16, 65); // 65 = A
  aes := TAESCBC.Create('AAAAAAAAAAAAAAAA', 128, iv);
  Edit1.Text := BinToBase64(aes.EncryptPKCS7('test'));
  aes.Free;
end;

Results:
PHP    = 1E6FuveraIZPdP85AP4j7Q==
C#     = 1E6FuveraIZPdP85AP4j7Q==
Delphi = OfDDh/gm/SZtrVCo2l6R6A==

What am I doing wrong?

Offline

#2 2014-02-03 01:18:28

Swanty
Member
Registered: 2014-02-02
Posts: 3

Re: TAESCBC compatibility

Found the problem in my code. Thanks!

New delphi code:

Uses ...SynCommons, SynCrypto...

procedure TForm1.Button1Click(Sender: TObject);
var
  aes: TAESCBC;
  iv: TAESBlock;
begin
  FillChar(iv, 16, 65); // 65 = A
  aes := TAESCBC.Create(AnsiString('AAAAAAAAAAAAAAAA'), 128, iv);
  Edit1.Text := BinToBase64(aes.EncryptPKCS7('test'));
  aes.Free;
end;

Offline

#3 2014-02-03 02:07:26

Swanty
Member
Registered: 2014-02-02
Posts: 3

Re: TAESCBC compatibility

Ok, switched to AES 256 and moved away from PHP mcrypt to phpseclib due to '\0' padding in data.

Here's the code in case anyone needs.

PHP:

// http://phpseclib.sourceforge.net/
set_include_path('include/phpseclib0.3.5');
include('Crypt/AES.php');

$my_key = 'testkey';
$my_iv = '1234567890ABCDEF';

$cipher = new Crypt_AES(); // could use CRYPT_AES_MODE_CBC
// keys are null-padded to the closest valid size
// longer than the longest key and it's truncated
//$cipher->setKeyLength(256);
$cipher->setKey(hash('SHA256', $my_key, true));
$cipher->setIV($my_iv); // defaults to all-NULLs if not explicitely defined
//$cipher->disablePadding();

echo base64_encode(@$cipher->encrypt('testdata'));

Delphi:

Uses SynCommons, SynCrypto;

procedure TForm1.Button1Click(Sender: TObject);
const
  my_key = 'testkey';
  my_iv  = '1234567890ABCDEF';
var
  key : TSHA256Digest;
  aes : TAESCBC;
  iv  : TAESBlock;
begin
  HexToBin(Pointer(SHA256(my_key)), @key, 32);
  Move(TEncoding.ANSI.GetBytes(my_iv)[0], iv[0], 16);
  aes := TAESCBC.Create(key, 256, iv);
  try
    Edit1.Text := BinToBase64(aes.EncryptPKCS7('testdata'));
  finally
    aes.Free;
  end;
end;

P.S. Thank you so much Synopse!!!

Last edited by Swanty (2014-02-03 13:20:31)

Offline

#4 2014-02-03 12:11:57

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: TAESCBC compatibility

Nice.

You are welcome!

Offline

#5 2021-08-24 02:22:52

Cahaya
Member
Registered: 2015-06-21
Posts: 36

Re: TAESCBC compatibility

Mormot AES encrypt and PHP decrypt

I used this code :
  SynCommons.HexToBin(Pointer(SHA256(my_key)),@key,32);
  aes := TAESCBC.Create(Key,256);
  try
    s := StringToUTF8(EdToEncrypt.Text);
    s := BinToBase64(aes.EncryptPKCS7(s,true));
    EdEncrypt.Text := UTF8ToString(s);
  finally
    aes.Free;
  end;

And for php :

$encrypt_method = 'AES-256-CBC';

    // hash
    $key_hash = hex2bin(hash('sha256', $key));

    // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
    $iv = substr(hex2bin(hash('sha256', $key)), 0, 16);

    $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key_hash, OPENSSL_RAW_DATA, $iv);

    return $output;

Why php can not decrypt properly ?

Text : Hello world
key : testkey
encrypt : UfSnUFH0p1BR9KdQUfSnUOrKd0falUmp/hI8tik+UjU=

Php decrypt : i~UIJ8nkHello world

Please help.....

Offline

#6 2021-08-24 08:47:01

Cahaya
Member
Registered: 2015-06-21
Posts: 36

Re: TAESCBC compatibility

I use manual IV based on PHP script and success.
Here is code for manual IV :

procedure TForm1.Button5Click(Sender: TObject);
var
  aes: TAESCBC;
  Key: TSHA256Digest;
  s  : RawBytestring;
  iv : array[0..15] of AnsiChar;
begin
  SynCommons.HexToBin(Pointer(SHA256(my_key)),@key,32);
  SynCommons.HexToBin(Pointer(SHA256(my_key)),@iv,16);
  aes := TAESCBC.Create(Key,256);
  aes.IV := TAESBlock(iv);
  try
    s := StringToUTF8(EdEncrypt.Text);
    s := aes.DecryptPKCS7(Base64ToBin(s));
    EdToEncrypt.Text := UTF8ToString(s);
  finally
    aes.Free;
  end;
end;

Last edited by Cahaya (2021-08-24 08:48:15)

Offline

Board footer

Powered by FluxBB