cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Go to solution

Error re: openssl encrypt and decrypt in cpanel

function encrypt($string){

$cipher = "aes-128-gcm";
$key = "2cf7c0f30195c737ca9e570f4e1d2654716a34f80cd9d2d4c61d1ae55d59d4bd81d9790b08e92bf4836eb06d5cb1673a8fff4e9fc239babe77f88dc846ad6ce4";
$iv_len = openssl_cipher_iv_length($cipher);
$tag_length = 16;
$iv = openssl_random_pseudo_bytes($iv_len);
$tag ="";

$output = openssl_encrypt($string, $cipher, $key, OPENSSL_RAW_DATA, $iv, $tag, "", $tag_length);
$output = base64_encode($iv.$tag.$output);

return $output;

}


function decrypt($string){

$encrypted_string = base64_decode($string);
$key = "2cf7c0f30195c737ca9e570f4e1d2654716a34f80cd9d2d4c61d1ae55d59d4bd81d9790b08e92bf4836eb06d5cb1673a8fff4e9fc239babe77f88dc846ad6ce4";
$cipher = "AES-128-GCM";
$iv_len = openssl_cipher_iv_length($cipher);
$tag_length = 16;
$iv = substr($encrypted_string, 0, $iv_len);
$tag = substr($encrypted_string, $iv_len, $tag_length);
$encrypt_text = substr($encrypted_string,$iv_len + $tag_length);

$output = openssl_decrypt($encrypt_text, $cipher, $key, OPENSSL_RAW_DATA, $iv, $tag);

return $output;

 

}

 

 

These are the fucntion for encryption and decryption of text in cpanel hosting but it gives error 

openssl_cipher_iv_length(): Unknown cipher algorithm
openssl_encrypt(): Unknown cipher algorithm
openssl_decrypt(): Unknown cipher algorithm

 What is the issue please help me in this regard. It working fine in localhost server(xampp)

1 ACCEPTED SOLUTION
Super User IV

@Satyajeet17 

I setup a test page on my VPS and the only thing I changed with your code was  

 

$password = '3sc3RLrpd17';
$key = substr(hash('sha256', $password, true), 0, 32);

 

And the password can be anything random.

 

I didn't get any errors when doing that - are you getting any errors in the error log or displayed on the site?

I am a GoDaddy End User - Just Like You
Check out my site! | I currently manage over 300 WordPress Websites
* Please note that I offer free advice on this forum. Thank You Info If you would like personalized help, please contact me. Otherwise, please ask your question in the proper forum so the answer can assist EVERYONE in the community and not just you. Thanks! *

Once your issue is resolved,
please be sure to come back and click accept for the solution

Get Better Support on the Community Boards!
Etiquette When Asking for Help from the Community

View solution in original post

1 REPLY 1
Super User IV

@Satyajeet17 

I setup a test page on my VPS and the only thing I changed with your code was  

 

$password = '3sc3RLrpd17';
$key = substr(hash('sha256', $password, true), 0, 32);

 

And the password can be anything random.

 

I didn't get any errors when doing that - are you getting any errors in the error log or displayed on the site?

I am a GoDaddy End User - Just Like You
Check out my site! | I currently manage over 300 WordPress Websites
* Please note that I offer free advice on this forum. Thank You Info If you would like personalized help, please contact me. Otherwise, please ask your question in the proper forum so the answer can assist EVERYONE in the community and not just you. Thanks! *

Once your issue is resolved,
please be sure to come back and click accept for the solution

Get Better Support on the Community Boards!
Etiquette When Asking for Help from the Community

View solution in original post