Skip to content

Instantly share code, notes, and snippets.

@mudssrali
Created September 15, 2021 20:45
Show Gist options
  • Save mudssrali/1f1aa6582ba2fd2b56a0b1eb53e4954a to your computer and use it in GitHub Desktop.
Save mudssrali/1f1aa6582ba2fd2b56a0b1eb53e4954a to your computer and use it in GitHub Desktop.
Php implementation of encryption and decryption using openssl_encrypt and openssl_decrypt
<?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