Created
September 28, 2014 18:57
-
-
Save itarato/e994379dac2074f219d2 to your computer and use it in GitHub Desktop.
PHP AES CBC encryption then MAX example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$method = MCRYPT_RIJNDAEL_128; | |
$block_cypher_mode = MCRYPT_MODE_CBC; | |
$clean = "Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed molestie augue sit amet leo consequat posuere. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Proin vel ante a orci tempus eleifend ut et magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus luctus urna sed urna ultricies ac tempor dui sagittis. In condimentum facilisis porta. Sed nec diam eu diam mattis viverra. Nulla fringilla, orci ac euismod semper, magna diam porttitor mauris, quis sollicitudin sapien justo in libero. Vestibulum mollis mauris enim. Morbi euismod magna ac lorem rutrum elementum. Donec viverra auctor lobortis. Pellentesque eu est a nulla placerat dignissim. Morbi a enim in magna semper bibendum. Etiam scelerisque, nunc ac egestas consequat, odio nibh euismod nulla."; | |
$keyOriginal = "abcdefghijklmop"; | |
$encrypted = encrypt($clean, $keyOriginal, 16, $method, $block_cypher_mode); | |
$decrypted = decrypt($encrypted, $keyOriginal, 16, $method, $block_cypher_mode); | |
function encrypt($clean, $key, $key_size, $method, $block_cypher_mode) { | |
// Hashing key. | |
$key = hash('sha256', $key, TRUE); | |
$key = substr($key, 0, $key_size); | |
// Creating IV. | |
$iv_size = mcrypt_get_iv_size($method, $block_cypher_mode); | |
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); | |
// Padding clean text. | |
$block = mcrypt_get_block_size($method, $block_cypher_mode); | |
$pad = $block - (strlen($clean) % $block); | |
$clean .= str_repeat(chr($pad), $pad); | |
// Encrypt. | |
$cipherText = mcrypt_encrypt($method, $key, $clean, $block_cypher_mode, $iv); | |
// MAC. | |
$mac = hash_hmac('sha256', $cipherText, $key); | |
return $mac . $iv . $cipherText; | |
} | |
function decrypt($cipher, $key, $key_size, $method, $block_cypher_mode) { | |
// Extract MAC. | |
$mac = substr($cipher, 0, 64); | |
// Extracting IV. | |
$ivSize = mcrypt_get_iv_size($method, $block_cypher_mode); | |
$iv = substr($cipher, 64, $ivSize); | |
// Extracting text. | |
$cipherText = substr($cipher, 64 + $ivSize); | |
// Hashing key. | |
$key = hash('sha256', $key, TRUE); | |
$key = substr($key, 0, $key_size); | |
// Check MAC. | |
$macControl = hash_hmac('sha256', $cipherText, $key); | |
if ($macControl !== $mac) { | |
return FALSE; | |
} | |
// Decrypt. | |
return mcrypt_decrypt($method, $key, $cipherText, $block_cypher_mode, $iv); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment