php two way encrypt decrypt

function encrypt_decrypt($string, $action = 'encrypt')
{
    $encrypt_method = "AES-256-CBC";
    $secret_key = 'AA74CDCC2BBRT935136HH7B63C27'; // user define private key
    $secret_iv = '5fgf5HJ5g27'; // user define secret key
    $key = hash('sha256', $secret_key);
    $iv = substr(hash('sha256', $secret_iv), 0, 16); // sha256 is hash_hmac_algo
    if ($action == 'encrypt') {
        $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
        $output = base64_encode($output);
    } else if ($action == 'decrypt') {
        $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
    }
    return $output;
}
 
echo "Your Encrypted password is = ". $pwd = encrypt_decrypt('spaceo', 'encrypt');
echo "Your Decrypted password is = ". encrypt_decrypt($pwd, 'decrypt');

3.86
7
Awgiedawgie 440215 points

                                    $decoded = base64_decode($encoded);

3.86 (7 Votes)
0
3.8
5
Krish 100200 points

                                    function encryptor($action, $string) {
	$output = FALSE;
	$encrypt_method = "AES-256-CBC";
	$secret_key = 'SecretKeyWord';
	$secret_iv  = 'SecretIV@123GKrQp';
	// hash
	$key = hash('sha256', $secret_key);
	// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
	$iv = substr(hash('sha256', $secret_iv), 0, 16);
	//do the encryption given text/string/number
	if ($action == 'encrypt') {
		$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
		$output = base64_encode($output);
	} elseif ($action == 'decrypt') {
		//decrypt the given text/string/number
		$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
	}
	return $output;
}

function encrypt($data) {
	return urlencode(self::encryptor('encrypt', self::sanitize($data)));
}

function decrypt($data) {
	return self::encryptor('decrypt', urldecode(self::sanitize($data)));
}
// Now you can just call encrypt($string) or decrypt($string)
?>

3.8 (5 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
decrypt and encypt in php php ecrypt and decrypt message encrypt and decrypt in php how to encrypt and decrypt password in php encryption and decryption in php php string encryption and decryption php crypt decrypt php decrypt string encrypted in codeinger 4 encryption and decryption in php example encrypt decrypt php php encrypt message encrypt() decrypt php encrypt and decrypt data php simple 10 encrypt and decrypt php simple encrypt and decrypt Encrypt and decrypt text messages in pure PHP code encrypt and decrypt number in php how to encrypt and decrypt a password in php ohow to encrypt and decrypt string in php how to encrypt and decrypt id in php encrypt decrypt in php php password encrypt and decrypt how to decrypt encrypted php code php crypt encrypt decrypt php decrypt several methods to encrypt and decrypt a value for php php simple encrypt decrypt encrypt decrypt file php encrypt decrypt text php simple php encrypt decrypt encrypt and decrypt in php encrypt and decrypt a string in php php encrypt decrypt php encrypt and decrypt Encrypt with JAVA and Decrypt with PHP php encrypt decrypt password php encrypt decrypt online encrypt in c# and decrypt in php php unsafe encryption maintain end to end encryption php how to incrypt decrypt in php '/' is encrypted 2 times in php encryption in php php encrypt simple encrypt and decrypt in php encrypt with key php php 2 way encryption with salt create encryption library for php reversible encryption php how to encrypt small php code 2way fixed encryption php php two way encrypt decrypt
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source