I have the following code using for encryption
$key="company";
$value['service_id']="edit_details";
$encrypted_string=trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $value['service_id'], MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
and i used the below code for decryption
$key="company";
$serviceId=$encrypted_string;
$decrypted_string=trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($serviceId), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
I am not getting the original string edit_details.
I don't what is wrong in this.
So if you are getting the value by $_GET
use like this
$encrypted_string = trim(urlencode(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $value['service_id'], MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)))));
when base64_encoded
data is transferred , it may get corrupted if the data contains characters such as ?+/
. so urlencode()
is needed when a data is transferred.
Since you're using ECB, you don't need an initialization vector (IV) at all. Just remove it:
$key="company";
$value['service_id']="edit_details";
$encrypted_string=trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $value['service_id'], MCRYPT_MODE_ECB)));
$decrypted_string=trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($serviceId), MCRYPT_MODE_ECB));
In other modes, such as CBC, when an IV is needed, you're supposed to use the same IV for encryption and decryption. Pass it along with encrypted data, as shown on the manual page.
This wikipedia article explains how different block modes work.