Magento Engineer
social
Journal

How do you Encrypt and Decrypt a PHP String?

/**Function to encrypt or decrypt the given value**/
/**Create a function for Encryption url**/

function encrypt_decrypt($string){   
    $string_length=strlen($string);
    $encrypted_string="";     
    for ($position = 0;$position<$string_length;$position++){         
        $key = (($string_length+$position)+1);
        $key = (255+$key) % 255;
        $get_char_to_be_encrypted = SUBSTR($string, $position, 1);
        $ascii_char = ORD($get_char_to_be_encrypted);
        $xored_char = $ascii_char ^ $key;  //xor operation
        $encrypted_char = CHR($xored_char);
        $encrypted_string .= $encrypted_char;
    }
    /***Return the encrypted/decrypted string***/
    return $encrypted_string;
 }

/*** While passing the unique value to a link- Do the following steps ***/
$pid=88;//Let's 88 is the actual id
/***For more security multiply some value-You can set the multiplication value in config file*/
$passstring=$pid*12345;
$encrypted_string=encrypt_decrypt($passstring);
$param=urlencode($encrypted_string);

/*** Add this url to your anchor link***/
$url='your_target_path.php?id='.$param;
 
/*** While fetching the params in the target file- Do the following steps ***/
$getid=$_GET['id'];
$passstring=urldecode(stripslashes($getid));
$decrypted_string= encrypt_decrypt($passstring);
/***Divide the decrypted value with the same value we used for the multiplication***/
$your_actual_id= $decrypted_string/12345;

/*** Now fire your MySql query by this ID***/

Ad comes here
Comments
Leave A Comment
PHP blog
Update Cookies Preferences