Skip to content

Instantly share code, notes, and snippets.

View waltzie's full-sized avatar

Walter Summonte waltzie

View GitHub Profile
@waltzie
waltzie / gist:295fb2473f3773965d050b86482e0449
Created February 5, 2019 18:52 — forked from srsbiz/gist:8373451ed3450c0548c3
php AES-128-CBC mcrypt & openssl
<?php
function encrypt_mcrypt($msg, $key, $iv = null) {
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
if (!$iv) {
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
}
$pad = $iv_size - (strlen($msg) % $iv_size);
$msg .= str_repeat(chr($pad), $pad);
$encryptedMessage = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $msg, MCRYPT_MODE_CBC, $iv);
return base64_encode($iv . $encryptedMessage);