Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justingreerbbi/575d230615e479d1513c6f193d4347de to your computer and use it in GitHub Desktop.
Save justingreerbbi/575d230615e479d1513c6f193d4347de to your computer and use it in GitHub Desktop.
Symmetric Encryption in PHP
<?php
class SymmetricEncryption {
private $cipher;
public function __construct($cipher = 'aes-256-cbc') {
$this->cipher = $cipher;
}
private function getKeySize() {
if (preg_match("/([0-9]+)/i", $this->cipher, $matches)) {
return $matches[1] >> 3;
}
return 0;
}
private function derived($password, $salt) {
$AESKeyLength = $this->getKeySize();
$AESIVLength = openssl_cipher_iv_length($this->cipher);
$pbkdf2 = hash_pbkdf2("SHA1", $password, mb_convert_encoding($salt, 'UTF-16LE'), 1000, $AESKeyLength + $AESIVLength, TRUE);
$key = substr($pbkdf2, 0, $AESKeyLength);
$iv = substr($pbkdf2, $AESKeyLength, $AESIVLength);
$derived = new stdClass();
$derived->key = $key;
$derived->iv = $iv;
return $derived;
}
function encrypt($message, $password, $salt) {
$derived = $this->derived($password, $salt);
$enc = openssl_encrypt(mb_convert_encoding($message, 'UTF-16', 'UTF-8'), $this->cipher, $derived->key, NULL, $derived->iv);
return $enc;
}
function decrypt($message, $password, $salt) {
$derived = $this->derived($password, $salt);
$dec = openssl_decrypt($message, $this->cipher, $derived->key, NULL, $derived->iv);
return mb_convert_encoding($dec, 'UTF-8', 'UTF-16');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment