Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rijndael 256-bit Encryption Function (CBC)
<?php
// Define a 32-byte (64 character) hexadecimal encryption key
// Note: The same encryption key used to encrypt the data must be used to decrypt the data
define('ENCRYPTION_KEY', 'd0a7e7997b6d5fcd55f4b5c32611b87cd923e88837b63bf2941ef819dc8ca282');
// Encrypt Function
function mc_encrypt($encrypt, $key){
$encrypt = serialize($encrypt);
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_DEV_URANDOM);
$key = pack('H*', $key);
$mac = hash_hmac('sha256', $encrypt, substr(bin2hex($key), -32));
$passcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $encrypt.$mac, MCRYPT_MODE_CBC, $iv);
$encoded = base64_encode($passcrypt).'|'.base64_encode($iv);
return $encoded;
}
// Decrypt Function
function mc_decrypt($decrypt, $key){
$decrypt = explode('|', $decrypt.'|');
$decoded = base64_decode($decrypt[0]);
$iv = base64_decode($decrypt[1]);
if(strlen($iv)!==mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC)){ return false; }
$key = pack('H*', $key);
$decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $decoded, MCRYPT_MODE_CBC, $iv));
$mac = substr($decrypted, -64);
$decrypted = substr($decrypted, 0, -64);
$calcmac = hash_hmac('sha256', $decrypted, substr(bin2hex($key), -32));
if($calcmac!==$mac){ return false; }
$decrypted = unserialize($decrypted);
return $decrypted;
}
echo '<h1>Rijndael 256-bit CBC Encryption Function</h1>';
$data = 'Super secret confidential string data.';
$encrypted_data = mc_encrypt($data, ENCRYPTION_KEY);
echo '<h2>Example #1: String Data</h2>';
echo 'Data to be Encrypted: ' . $data . '<br/>';
echo 'Encrypted Data: ' . $encrypted_data . '<br/>';
echo 'Decrypted Data: ' . mc_decrypt($encrypted_data, ENCRYPTION_KEY) . '</br>';
$data = array(1, 5, 8, 9, 22, 10, 61);
$encrypted_data = mc_encrypt($data, ENCRYPTION_KEY);
echo '<h2>Example #2: Non-String Data</h2>';
echo 'Data to be Encrypted: <pre>';
print_r($data);
echo '</pre><br/>';
echo 'Encrypted Data: ' . $encrypted_data . '<br/>';
echo 'Decrypted Data: <pre>';
print_r(mc_decrypt($encrypted_data, ENCRYPTION_KEY));
echo '</pre>';
?>
@lucbonnin

This comment has been minimized.

Copy link

@lucbonnin lucbonnin commented Jan 27, 2014

Hi,
For you information, i've reused your class to create a yiiframework extension.
on yii extension page : http://www.yiiframework.com/extension/aes256/
on github : https://github.com/lucbonnin/aes256_yii_extension

i've added you as an author like that :
" * @author Inspired by joshhartman version https://gist.github.com/joshhartman/5383582#file-mcrypt-cbc-php"

have a nice day and thank you for this class, really helpful to create my yii extension.

@WajahatAnwar

This comment has been minimized.

Copy link

@WajahatAnwar WajahatAnwar commented May 5, 2016

This is the AES Encryption or not

@anasbenmansour

This comment has been minimized.

Copy link

@anasbenmansour anasbenmansour commented Feb 21, 2017

Hi , is there a code equivalent in JAVA ?
Help pls

@wxiaoguang

This comment has been minimized.

Copy link

@wxiaoguang wxiaoguang commented Apr 24, 2018

Rijndael-256 is NOT AES-256. DO NOT USE Rijndael-256 OR YOU MAY FAIL TO DECRYPT.

@panbb

This comment has been minimized.

Copy link

@panbb panbb commented Apr 28, 2018

mcrypt_decrypt
(PHP 4 >= 4.0.2, PHP 5, PHP 7 < 7.2.0, PECL mcrypt >= 1.0.0)

mcrypt_decrypt — Расшифровывает данные с заданными параметрами

Внимание
Эта функция объявлена УСТАРЕВШЕЙ, начиная с PHP 7.1.0 и была УДАЛЕНА в версии PHP 7.2.0. Использовать эту функцию крайне не рекомендуется.

@japser36

This comment has been minimized.

Copy link

@japser36 japser36 commented Jul 9, 2019

Is there a JS version of these functions?

@joshhartman

This comment has been minimized.

Copy link
Owner Author

@joshhartman joshhartman commented Jul 10, 2019

No, sorry, and these functions are now deprecated. Use the Libsodium library if available in your language.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment