Skip to content

Instantly share code, notes, and snippets.

@dougvann
Created October 14, 2020 05:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dougvann/ae94340b6bae5e72f328ff170706525f to your computer and use it in GitHub Desktop.
Save dougvann/ae94340b6bae5e72f328ff170706525f to your computer and use it in GitHub Desktop.
function encryptData($data) {
$plainText = json_encode($data);
$bytesToBeEncrypted = $plainText;
$passwordBytes = utf8_encode("p@SSword");
$passwordBytes = hash('sha256', $passwordBytes, true);
$saltBytes = array(1,2,3,4,5,6,7,8);
$saltBytesstring = "";
for($i=0;$i<count($saltBytes);$i++){
$saltBytesstring = $saltBytesstring.chr($saltBytes[$i]);
}
$hash = hash_pbkdf2("sha1", $passwordBytes, $saltBytesstring, 1000, 48, true);
$keyHash = substr($hash, 0, 32);
$ivHash = substr($hash, 32, 16);
$method = 'AES-256-CBC';
$encrypted = replaceCharacters(base64_encode(openssl_encrypt($bytesToBeEncrypted, $method, $keyHash, OPENSSL_RAW_DATA, $ivHash)));
return $encrypted;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment