Created
September 15, 2021 20:45
-
-
Save mudssrali/1f1aa6582ba2fd2b56a0b1eb53e4954a to your computer and use it in GitHub Desktop.
Php implementation of encryption and decryption using openssl_encrypt and openssl_decrypt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$ENCRYPTION_KEY = ''; | |
$ENCRYPTION_ALGORITHM = 'AES-128-ECB'; | |
function encrypt($plainText) { | |
global $ENCRYPTION_KEY; | |
global $ENCRYPTION_ALGORITHM; | |
$EncryptionKey = makeHash($ENCRYPTION_KEY, 16); | |
$encryptedText = openssl_encrypt($plainText, $ENCRYPTION_ALGORITHM, $EncryptionKey); | |
return $encryptedText; | |
} | |
function decrypt($ciphertext) { | |
global $ENCRYPTION_KEY; | |
global $ENCRYPTION_ALGORITHM; | |
$EncryptionKey = makeHash($ENCRYPTION_KEY, 16); | |
$decrypt = openssl_decrypt($ciphertext, $ENCRYPTION_ALGORITHM, $EncryptionKey); | |
return $decrypt; | |
} | |
function makeHash($text, $length) { | |
$hash_key = hash("sha512", $text, false); | |
return substr($hash_key,0,$length); | |
} | |
// $ct = encrypt("hello"); | |
// echo $ct."\n"; | |
// echo decrypt($ct)."\n"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment