How can I convert php code

Thanks for the code, I changed the encryption to ECB, but it could not decipher the encrypted text I gave as an example.

This password can be decrypted on the site I gave as an example. My aim is to decipher the encrypted text in PHP.

I ask you, if the code you wrote works without any problems, can you update and write the encrypted code I gave as an example so that I can decipher the text?

The site you provided is outdated and insecure. I will not help you create insecure code.

I gave you a perfectly good working example that is up to date. There is no reason you cannot use it. We are not a code on demand service. Perhaps you should try to actually learn about encryption instead of copy/pasting code.

The only thing the sample site has to do with the code I’m asking for help with is that it can decrypt the encryption system.

I would like help on how I can create an encrypted code like the one on this site in PHP. The code you gave may be nice, but it does not decode the sample encrypted code and many sample codes on the market do not.

That’s why I wrote it on the help site, my aim is not to copy/paste to learn, I guess you understand the php coding language as much as I do.

@bypalermo have you tried any other resources to encrypt data, apart from that website?
For example, have you tried to encrypt and then decrypt something using the class provided in this thread?
I understand your want to learn encryption, but I don’t quite understand your insistance of using that website to do it.

I tried many different sites but it didn’t solve it.
Because of this situation, I forwarded this site as an example.

I tried a sample PHP code and it didn’t solve it either. Can’t the decryption on this site be done in PHP?

The only way I can get the encoded string from the OPs first post is to pad out the original string with zeros to get it to 32 bytes, but the documentation says that the encrypt and decrypt functions will add and remove padding as required:

$orig_string = "Is it solved now? :)" . str_repeat(chr(0),12);

When I do that, and call a modified version of the code posted by @benanamen earlier, I get this encoded string:

A554754D3907093641E3F02235D661FE97999A2FA085F32EC0A775C0B2C30DC279DA1F30AB89C9390DD121E0858E4353

As you can see, it’s the same encoded string, but after the C30DC2 at the end of the OPs string, I have an additional 16 bytes. Of course, without those extra 16 bytes, the string won’t decode. But it does decode on their site.

As an example, I tried as follows but it didn’t solve it! I wonder which php code did you use to decode it?

class AES{
    public $key = "DC421A13034B91D1048A483EEB0254EB";
    public $iv = '1234567891234567';

    public  function decode($str){
        return openssl_decrypt($str,"AES-128-ECB",$this->key,$this->iv);
    }
}
$aes = new AES();

var_dump($aes->decode("A554754D3907093641E3F02235D661FE97999A2FA085F32EC0A775C0B2C30DC279DA1F30AB89C9390DD121E0858E4353"));

This is all the code I used, the class is taken directly from the post by @benanamen and modified a little:

// SOURCE:  https://programmer.group/ecb-and-cbc-encryption-and-decryption-of-php7-openssl_decrypt-aes.html
class AES
{
    public $key ;

    public function __construct()
    {
    	$this->key =  hex2bin("9F53074AEED900BAA8C6F4797CD0F7FC"); 	
    }

    public  function decode($str)
    {
        return openssl_decrypt($str,"AES-128-ECB",$this->key,OPENSSL_RAW_DATA);
    }

    public  function encode($str)
    {
        return (openssl_encrypt($str,"AES-128-ECB",$this->key,OPENSSL_RAW_DATA));
	}

}

$orig_string = "Is it solved now? :)" . str_repeat(chr(0),12);

$aes = new AES();

$s1 = $aes->encode($orig_string);
var_dump($s1);
echo strtoupper(bin2hex($s1));

$new_string = $aes->decode($s1);
var_dump($new_string);

The changes I made:

Change the key to your key.
Remove the initialisation vector as ECB doesn’t use it. Didn’t try with it left in.
Remove the base64 encode and decode.
Change the encryption type to ECB.
Add 12 chr(0) onto the end of your unencoded string - if I don’t do this, the encoded string is different after the first 16 bytes. Doc suggests I shouldn’t need to do this.

I couldn’t see a way to make the PHP functions return the same encoded sequence that the web site does. Maybe you could ask them why?

I was tempted to just ignore your comment goading me. Suffice it to say, I know substantially more about PHP than you probably ever will. I gave you expert advice and working code that is up to current standards that you refuse to learn from and instead insist on an answer to outdated insecure output from an outdated random website. Nevertheless, for the sake of everyone else and to put this to rest I will provide the outdated insecure answer.

!!THIS CODE SHOULD NOT BE USED!!
mcrypt extension is deprecated in PHP 7.1 and removed in PHP 7.2

<?php
$text = "Is it solved now? :)";
$key = hex2bin("9F53074AEED900BAA8C6F4797CD0F7FC");
$method = MCRYPT_RIJNDAEL_128; // AES-128 in ECB mode

// Ensure key length is correct
if (strlen($key) !== 16) {
    echo "Key length must be 16 bytes for AES-128 encryption.";
    exit();
}

// Encrypt the text using AES in ECB mode
$encrypted = mcrypt_encrypt($method, $key, $text, MCRYPT_MODE_ECB);

// Convert the encrypted binary data to hexadecimal
$hexResult = bin2hex($encrypted);
echo "Hex Result: $hexResult\n";

// Decrypt the encrypted text
$decrypted = mcrypt_decrypt($method, $key, $encrypted, MCRYPT_MODE_ECB);

// Remove padding
$decrypted = rtrim($decrypted, "\0");

echo "Decrypted Text: $decrypted\n";
1 Like

thank you, it worked for me

Sorry if I misunderstood, it is true that the coding may be outdated, but there is an encrypted code and I had to decrypt it with PHP. The code you wrote is exactly what I wanted, thank you very much.

If that code works for you, your PHP installation is out of date.

To quote:

meaning that function is GONE in PHP 7.2. PHP’s current version is 8.3.4.

I have already set the php 5.6 version for this code to work :smiley:

So that we’re clear… PHP 5.6 was released in 2014, left support over half a decade ago, and is considered unsecure.

1 Like

Yes I know, but I have to use it because I have a project to run.