Android-PHP-Encrypt-Decrypt icon indicating copy to clipboard operation
Android-PHP-Encrypt-Decrypt copied to clipboard

Error when use php 7

Open isarjanfaza opened this issue 6 years ago • 7 comments

hi when use php version 7 not work and get error {main} thrown in MCrypt.php on line 11

please check this bug

thanks

isarjanfaza avatar Nov 25 '18 11:11 isarjanfaza

In PHP 7, MCrypt is deprecated... A workaround is to use openssl_* instead :

function encrypt2($str) {
 
			$encrypted = openssl_encrypt($str, 'aes-128-cbc', KEY_SERVICE, OPENSSL_RAW_DATA, IV_SERVICE);
  
			return bin2hex($encrypted);
		  }
  
		  function decrypt2($code) {
			$code = $this->hex2bin($code);
  
			$decrypted = openssl_decrypt($code, 'aes-128-cbc', KEY_SERVICE, OPENSSL_RAW_DATA |  OPENSSL_ZERO_PADDING, IV_SERVICE);
  
			return trim($decrypted);
		  }

Goufalite avatar Feb 05 '19 23:02 Goufalite

Thanks @Goufalite for the suggestion.

Would you like to create a pull request to contribute with the code?

serpro avatar Feb 06 '19 09:02 serpro

I have to check something with the ZERO_PADDING option which messes with the result, then I'll make the pull request

Goufalite avatar Feb 06 '19 22:02 Goufalite

hi @Goufalite how to set java class for this code?

razmjooDev avatar Nov 24 '20 02:11 razmjooDev

Look at the code tab on top, there's a Java MCrypt class.

I still have some leading NULL characters at the beginning so some trim might be in order to process the strings.

Goufalite avatar Nov 24 '20 09:11 Goufalite

i used that Java class but the results were not the same as your code results

razmjooDev avatar Nov 24 '20 20:11 razmjooDev

What were the results?

Goufalite avatar Nov 24 '20 21:11 Goufalite