Home > other >  Getting extra garbage with AES 256 decrypt (php, phpseclilb, openssl)
Getting extra garbage with AES 256 decrypt (php, phpseclilb, openssl)

Time:02-10

I have the following PHP code:

require('/var/www/third_party_plugins/phpseclib/vendor/autoload.php');
use phpseclib3\Crypt\PublicKeyLoader;
use phpseclib3\Crypt\RSA;
use phpseclib3\Math\BigInteger;
use phpseclib3\Crypt\AES;
use phpseclib3\Crypt\Random;
$message = hex2bin('f5f905e8b2d8f0a72e179a169a59bc373021a75865e55c6797627bc43ddc6af0d9bd673bf94f5e8defc5af81019fd87c7d504a6aa758ba1e2f1f9858d0293b0b');
$key = hex2bin('d2ce45fd5f80c15db0a4ab26a7e27f42b507ed9469f0d63c1dbe4f89ed84c0c2');
$iv = hex2bin('db9d7e844b00282327221bb563639f96');
$cipher = new AES('cbc');
$cipher->setIV($iv);
$cipher->setKey($key);
//$cipher->disablePadding();
$Decrypted = bin2hex($cipher->decrypt($message));
print("\n" . $Decrypted . "\n");

When I run this code, I get the following result:

240dcbefc0f82fadc00ef8494488aaa81400000c2def01e79fec6c4d9a822358dd8a910cac606e8afcb607793cb442093a56b7b40b

Inside of this result, I can see the message I WANT, which is:

1400000c2def01e79fec6c4d9a822358

However, there are 16 bytes of data in the front of the message which make no sense to me, it seems like some kind of padding, but I dont want it in my result nor do I understand why it is there, because from what I understand the padding should be removed by phpseclib or openssl functions

240dcbef c0f82fad c00ef849 4488aaa8

I understand that there is a 20 byte MAC at the end, however, I notice ONE extra byte in front of the mac:

dd8a910cac606e8afcb607793cb442093a56b7b40b  // THIS IS 21 BYTES, NOT 20...why?

I'm also having trouble re-encrypting the data and getting a valid response from the server, as I'm re-encrypting it with PHP, then sending the SAME data to my C# server, and it is unable to decrypt the message.

CodePudding user response:

  •  Tags:  
  • Related