Skip to content

Instantly share code, notes, and snippets.

View kelixlabs's full-sized avatar

Wahyu Kecambah Linux kelixlabs

View GitHub Profile
@kelixlabs
kelixlabs / gist:4ba5cc41e424d28072703458f6c36974
Created December 4, 2021 11:51 — 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);