3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /** Aes encryption */ class AES { const M_CBC = 'cbc'; const M_CFB = 'cfb'; const M_ECB = 'ecb'; const M_NOFB = 'nofb'; const M_OFB = 'ofb'; const M_STREAM = 'stream'; protected $key; protected $cipher; protected $data; protected $mode; protected $IV; /** * * @param type $data * @param type $key * @param type $blockSize * @param type $mode */ function __construct($data = null, $key = null, $blockSize = null, $mode = null) { $this->setData($data); $this->setKey($key); $this->setBlockSize($blockSize); $this->setMode($mode); $this->setIV(""); } /** * * @param type $data */ public function setData($data) { $this->data = $data; } /** * * @param type $key */ public function setKey($key) { $this->key = $key; } /** * * @param type $blockSize */ public function setBlockSize($blockSize) { switch ($blockSize) { case 128: $this->cipher = MCRYPT_RIJNDAEL_128; break; case 192: $this->cipher = MCRYPT_RIJNDAEL_192; break; case 256: $this->cipher = MCRYPT_RIJNDAEL_256; break; } } /** * * @param type $mode */ public function setMode($mode) { switch ($mode) { case AES::M_CBC: $this->mode = MCRYPT_MODE_CBC; break; case AES::M_CFB: $this->mode = MCRYPT_MODE_CFB; break; case AES::M_ECB: $this->mode = MCRYPT_MODE_ECB; break; case AES::M_NOFB: $this->mode = MCRYPT_MODE_NOFB; break; case AES::M_OFB: $this->mode = MCRYPT_MODE_OFB; break; case AES::M_STREAM: $this->mode = MCRYPT_MODE_STREAM; break; default: $this->mode = MCRYPT_MODE_ECB; break; } } /** * * @return boolean */ public function validateParams() { if ($this->data != null && $this->key != null && $this->cipher != null) { return true; } else { return FALSE; } } public function setIV($IV) { $this->IV = $IV; } protected function getIV() { if ($this->IV == "") { $this->IV = mcrypt_create_iv(mcrypt_get_iv_size($this->cipher, $this->mode), MCRYPT_RAND); } return $this->IV; } /** * @return type * @throws Exception */ public function encrypt() { if ($this->validateParams()) { return trim(base64_encode( mcrypt_encrypt( $this->cipher, $this->key, $this->data, $this->mode, $this->getIV()))); } else { throw new Exception('Invlid params!'); } } /** * * @return type * @throws Exception */ public function decrypt() { if ($this->validateParams()) { return trim(mcrypt_decrypt( $this->cipher, $this->key, base64_decode($this->data), $this->mode, $this->getIV())); } else { throw new Exception('Invlid params!'); } } } function encode($text) { return bin2hex($text); } function decode($base16){ return pack("H*", $base16); } function encrypt($key, $link){ $cp = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', 'cbc', ''); @mcrypt_generic_init($cp, $key, $key); $enc = mcrypt_generic($cp, $link); mcrypt_generic_deinit($cp); mcrypt_module_close($cp); return $enc; } $key="31323334353637383930393837363534"; $link="62QTUQPXD5PTTmyJPr6wh/b7fdQR94C+MEwg1XEvXI2nV4Lg38yp6AKLW6/ZWW5Z8c+CjfCWZkJoBkpl/p2SS7rp4GAuUR07OKu9rDp/2yoX14y/zx13w62yt6DIRvqWpk/Z4B1EFgSaky6UIHHefubrpnJtAmdq4hhtZXbrNvZvxn6DG74ZJC4B2Qwp3PM1EYz7PiK1MQv2p6dcLdi5J1seJUp9oICdKKYF/hDnNXC6UQSOKz1QDD8oWd4glsp1WGOiR7XXat4llB3g+rZV2suupoR6jpFDAv5DK3GUjEZGUUAgnhNMbHvRVFfce80LOQn7Lh+TUvrXqfE45fzgJcf6qswWHdFCtF8mOUQvjsr67wkkBTn4shNG1bWYF5oXbBOArt+R9dk5IluD2mDGiOj3FymwPQFd+nrHFYIsnSMHs3VC7jpa8nVHo2zYajeXc43dgMiH4mZwfYica1myoKqAfnCpokhaSaOtbE0SKOdhJw2Hw90IChEwvlWFoZmeW1MlA3W9CYIGdR4Xwxl7TXNgw2w9EXwXc0N8WQV1bK7AUB3F722HJMgiLCqjs9UzSTk6yhBs4LNdKnW+F44iQEV7vFibvGVbwanjypxX+E4aydaPPFA3TCpZY+pEo2IY3mfmnH8oyswlp55SvIvLBU4IHNNdfv8ONZzUiasaQSZxQ0K6aVm8nbjUBZEfCX3h5N1h047Vt0sVSI19Bsv9mDzIgXc1jnDREvDdhQM97lzzctm+fyjAPnXmScuqPnhn"; $blockSize = 128; $aes = new AES($link, encode($key), $blockSize); //$aes->setData($enc); $dec = $aes->decrypt(); echo "Decryption: ".$dec."\r\n"; echo base64_encode(encrypt(encode($key), $link));
based on gnrVK
Output for 7.2.0 - 7.3.0rc3
Warning: Use of undefined constant MCRYPT_RIJNDAEL_128 - assumed 'MCRYPT_RIJNDAEL_128' (this will throw an Error in a future version of PHP) in /in/VrNe3 on line 57 Warning: Use of undefined constant MCRYPT_MODE_ECB - assumed 'MCRYPT_MODE_ECB' (this will throw an Error in a future version of PHP) in /in/VrNe3 on line 95 Fatal error: Uncaught Error: Call to undefined function mcrypt_decrypt() in /in/VrNe3:145 Stack trace: #0 /in/VrNe3(179): AES->decrypt() #1 {main} thrown in /in/VrNe3 on line 145
Process exited with code 255.
Output for 7.0.0 - 7.1.20
Notice: Use of undefined constant MCRYPT_RIJNDAEL_128 - assumed 'MCRYPT_RIJNDAEL_128' in /in/VrNe3 on line 57 Notice: Use of undefined constant MCRYPT_MODE_ECB - assumed 'MCRYPT_MODE_ECB' in /in/VrNe3 on line 95 Fatal error: Uncaught Error: Call to undefined function mcrypt_decrypt() in /in/VrNe3:145 Stack trace: #0 /in/VrNe3(179): AES->decrypt() #1 {main} thrown in /in/VrNe3 on line 145
Process exited with code 255.
Output for hhvm-3.15.4
Warning: Size of key is too large for this algorithm in /in/VrNe3 on line 146 Decryption: 耵Lm��b.��I���:�AQ����� ������]U�"��,m���eZT���I��0^�l��\ɼ���@��:�r��^O���/����J��`G�ڠ+֑�r�8�Ly#ћ��b��� �x-}e���m"䄔t��HDx~�n�Tk��R��Q���4�9Ҡ��r���8��6S�I��=*j �b �h��ۋ�Oqg=k=��t��e���(��|^f��:PX�S��n�y�6�OEN����M �J-N�\/f ���W1�ރ�ac0����"��H�>9T�� ��s4�v��Z, 3Q�9 ׮���!�� s�*c�'w�^Ҹ�!��"��ԋ���˄u�-@�U���Dv�L<z�%�wt��.H����ᴴ�%���|�S�9�� F�<1ƅ�����M�kdayk�f��G��F��?yE���͔XT��|���V�t��yi�9�P��p����4��@�� k��!��Bl��{�� 1��~�a�u]Q�6%c��hI�yGfS������R�p�0�h� �b3���۔0a aZU8gm5kbmceoagvQHSM5nlX58B95CYOMUl5PQWJwjY+9DbSxw2v2EKixx6KdSFOgCb7POl/CdWzsJa2s9K3Uou2x616V0KUARpPVOMO1kPNeDMARWF1TfzPSREMFq1fb3HnsDENWC6M1yvUcY/GGP9Wf4pZA9ItAJ3oBlA7XIPtjLFZcEcqzqHePIP44SB1+KdDLAWCTjXqk1/J9rnFZZbMsLcMyuM4ynTLBGMvIC1kStuU3w6ypnTAE60Z6IvfvLHJXncCP7QOrWzoGEzc+x5/Kp53RI1E87t3KS/1YHPbFhsZewh+VNX70+47CLV61bfbkPo/vem5jbUdceT0hdP2uz5ecJ9D5/4CIFm4Tp4warTpDKcYYtxyY2x/eSbJXtr84fvUPcc7HITFUWOnORHS4ESUoC4AsVnj3s8N+UsXIXGgmia//MIQomS9anoTye0XggKBKFp6RGLBPLLR+YvAtZVA/XhLvuyVc9zFZ6rA9scaoLM/2cUAID+Hw94bTJy8hgfkg0rShckn9S1RGl9/ui8PEbmj6hNEyunx4j+PZilKI6Adldp5qK1bQYhWhvY64PlpLK1vOQ+2ZlZjtYwtv7XNcY2A9pyhanatkNE47dZuVnXPNaooapXiPCcWX4j4g1nuUs+kD1Vj7nb9XPjLv7Qv9sRU3ksdg9kwAzjZYWP60EUoXQfeoQ1yOR3BFfKaZ2B3tBSk+LU9Bo0hZsJYdjXfbthVfXBPUkeVEYoe54WAe+FFX4MQxaRque0LwWi28saMxYa/duVruNwTaX1IcC2OHZxYMkym5aXOiIQhq3PBfhimNTBpdRNcAjjw3rO5YCrD+i8dxrw4o5+TTd9LQu2uBa/W9Ny0Kph7tUK+6Z7YOnNMfiXMQiYytHbUTAdHLkvjwJGTkt2x6J9QgZjPchSPz/EYmS33WONGDnloTrruO2F+y2h3pn3y0jMgg1cfgjukiA4YkE0fo2DpYprInB9g0gWZEeoKp+6OJQwjKDe9IqZb1YYb7Q41RgV+
Output for 5.6.0 - 5.6.30
Notice: Use of undefined constant MCRYPT_RIJNDAEL_128 - assumed 'MCRYPT_RIJNDAEL_128' in /in/VrNe3 on line 57 Notice: Use of undefined constant MCRYPT_MODE_ECB - assumed 'MCRYPT_MODE_ECB' in /in/VrNe3 on line 95 Fatal error: Call to undefined function mcrypt_decrypt() in /in/VrNe3 on line 145
Process exited with code 255.