Skip to content

Instantly share code, notes, and snippets.

@tlikai
tlikai / aes-128-cbc.php
Last active November 11, 2021 15:29 — forked from yuriy-yarvinen/encrypt_decrypt.php
php aes-128-cbc
<?php
define('ENCRYPTION_KEY', 'EncryptDecryptPHP201908Y');
// Encrypt
function encrypt($plaintext) {
$ivlen = openssl_cipher_iv_length($cipher = "AES-128-CBC");
$iv = openssl_random_pseudo_bytes($ivlen);
$ciphertext_raw = openssl_encrypt($plaintext, $cipher, ENCRYPTION_KEY, $options = OPENSSL_RAW_DATA, $iv);
$hmac = hash_hmac('sha256', $ciphertext_raw, ENCRYPTION_KEY, $as_binary = true);