3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * http://stackoverflow.com/questions/9262109/php-simplest-two-way-encryption/30189841#30189841 * * This is not safe to use */ class UnsafeCrypto { const METHOD = 'aes-256-ctr'; /** * Encrypts (but does not authenticate) a message * * @param string $message - plaintext message * @param string $key - encryption key (raw binary expected) * @param boolean $encode - set to TRUE to return a base64-encoded * @return string (raw binary) */ public static function encrypt($message, $key, $encode = false) { $nonceSize = openssl_cipher_iv_length(self::METHOD); $nonce = openssl_random_pseudo_bytes($nonceSize); $ciphertext = openssl_encrypt( $message, self::METHOD, $key, OPENSSL_RAW_DATA, $nonce ); // Now let's pack the IV and the ciphertext together // Naively, we can just concatenate if ($encode) { return base64_encode($nonce.$ciphertext); } return $nonce.$ciphertext; } /** * Decrypts (but does not verify) a message * * @param string $message - ciphertext message * @param string $key - encryption key (raw binary expected) * @param boolean $encoded - are we expecting an encoded string? * @return string */ public static function decrypt($message, $key, $encoded = false) { if ($encoded) { $message = base64_decode($message, true); if ($message === false) { throw new Exception('Encryption failure'); } } $nonceSize = openssl_cipher_iv_length(self::METHOD); $nonce = mb_substr($message, 0, $nonceSize, '8bit'); $ciphertext = mb_substr($message, $nonceSize, null, '8bit'); $plaintext = openssl_decrypt( $ciphertext, self::METHOD, $key, OPENSSL_RAW_DATA, $nonce ); return $plaintext; } } $id = md5(uniqid()); var_dump($id); $key = hex2bin($id); var_dump($hex); $message = 'Ready your ammunition; we attack at dawn.'; $encrypted = UnsafeCrypto::encrypt($message, $key); $decrypted = UnsafeCrypto::decrypt($encrypted, $key); var_dump($encrypted, $decrypted);
Output for 7.2.6
string(32) "98f6811d42459ef347e624794ff31811" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL Fatal error: Uncaught Error: Call to undefined function openssl_cipher_iv_length() in /in/vsqNe:21 Stack trace: #0 /in/vsqNe(81): UnsafeCrypto::encrypt('Ready your ammu...', '\x98\xF6\x81\x1DBE\x9E\xF3G\xE6$yO\xF3\x18...') #1 {main} thrown in /in/vsqNe on line 21
Process exited with code 255.
Output for 7.2.0
string(32) "e65a0e3721f76db5adb04f24e264f8b7" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "�.X��t����fpβ�y' �� ��i���f*%����M�xm�0N������$" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.1.10
string(32) "181e61ae991ffa84909408ddba0e318a" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "�kվ�����Ob������o됖Ͼ#d1v#2W  )�[���_y����ԡ4ۣ�" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.1.7
string(32) "d527b7fd0fa2cd2c8770e3bb1e9b8216" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) ";���+�B�O�^P�vk��SO����b���@�=��#s���A��x�(�����Y" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.1.6
string(32) "8a4fdcdc6a6e419602dc981397f81330" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "��� tӵdm��.铤�@Æ�� �<֭H"GObٵ�\�^�LlV@M�m_����!Z" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.1.5
string(32) "3710d8b58c36ff3135051bd68bc4b7d7" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) " �cX�D�@v���]�l=�;����� 0�%�j� ������N-X�j�?b" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.1.4
string(32) "059986a867b2010c394985cdf51bd892" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) " j�nV�g_=�ꕮ8vi*��g'��ff%$� �b/q�H<�2�,�} " string(41) "Ready your ammunition; we attack at dawn."
Output for 7.1.3
string(32) "8548e1a44ed7b4ab83e98bc5f812235a" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "{uD���v�ʊ�+X��m1�^O Eܖ����1��+8�z�g���E�tV" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.1.2
string(32) "11bbf956c0a3cddad802f672a170e79c" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "�,�y���;ؚi�LCz��@��6s,�߮�괬%�=stO�{?�e?�S�&�t(�:" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.1.1
string(32) "28be9f673764063d82bc94cdd543bb30" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "ީ�H�x3H5����������Y~�A1��/�JD;�,�J�1�S�x �:�/A��" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.1.0
string(32) "6d66aac730dca8d01241b220ff83a821" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "ΕV�2�H0NҖ��f��b�]J%������Σ��� d� ��������ྂ ���o�" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.20
string(32) "7ae072afeae2a7fcf41232a5b85fc102" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "�!��t�-uRPc���`ڱ��!'�TS���$�覠<?����w�4�7D��9�r�" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.19
string(32) "dbd7d60705870ce4883ce4019bc5b90b" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "�f��F1|��_��RF ,O\��Xe��+7�SQs����6��m�8A}�<(��As;" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.18
string(32) "338a0136b2be8ac50086ea33b5639ee7" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "z� �w̆#�T7t�R���<,0W�0�|�m~��N.���ٽa;`s��B�Ѯ�'���" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.17
string(32) "974b04b792d6749550deb228491214e5" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "�Ͷ�%�������St�`��y{}����(f�a����<G�R���8f�kW � �*�D��" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.16
string(32) "84543aaa71919373f25e344b7367eda7" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "~A%���P� b ;.ө�i.�@3�P�#�זH�yL���6\��N5�l�" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.15
string(32) "8252e79378fd71b9a37aa7397e213a20" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "�ܰ�_�S W��6�,���� h���>z���4'@��'�t��2t��y&s��" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.14
string(32) "28dd821e0185c89ea4c5499da872dd3a" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) " ��k���k��8sG���''�>2Y��T|�m�A�./��VO�X_�ɈY�Ա�" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.13
string(32) "a98fd47a6bda600d2fff7da4e4b14153" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "�!����CB��%�蒹���ϩq;�*:��k}���$8[� TA�f�3���/���+�" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.12
string(32) "6feffea38b447784725c5b1e59160a1a" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "Ʒ1��� S_3��g0/�'6���{�_!�3���� |�}򀀁��**��t�" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.11
string(32) "5d931caf556213a70cc86fcfdec4f5c6" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "nbSD�h8�"Ts����"c� Q�Ri��GmH�b�=U߹Cv{!L��p���'���kI�" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.10
string(32) "6edf1a7af72da800e702c8324560df2a" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) ",|[[��[3�=&��Th�d�d�O�� ��i��� ϧ��ݾA�H�{��a��" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.9
string(32) "59fb1f40b19c35e346d516da2e383164" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "�tm|/"��^�9b"fS���r<CN���,v����>o�|v\2�A�M�)����A" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.8
string(32) "2fd77e532b81357bef130199bb49b88c" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "�me�vf��vW_�=3Ͻ���H23f���Sa����.���/ @��d�~��gF'" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.7
string(32) "06ffc04994ba428df8ce1a55e3dc9e8d" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "� QG�M����>�׍0��m9�r�K!��r5gS X]Ʉ�j�.j��X�� " string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.6
string(32) "f0d6e117f2d402566db193c397353b17" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "�ڕ2_k�۩\��2S��� 9��E�tp�f�l��Dʫ� ���Y�͝>�P��ّ�mk" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.5
string(32) "f9856fecd2ff93793142b889bdd62646" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "�� �auv^�o�Qr����ڄ@��m(���ٝ�h�K� <�m{ &� �S���" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.4
string(32) "b483415f3bc9caf306a5ff971a3d5465" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "q�c?�¥���8ߏ��S��'�A�Ž~�����[����k4�Gt��6�AV����W��" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.3
string(32) "d5ac0b4c949780661abe6f3104f07054" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "w����lE�x��ؑ�!<z��5@��� ���9�<[L�C. 3iN^b#�L�'ƥ<�" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.2
string(32) "323984fb7fca7b5134eccc9b0e93e64d" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "`[�N���;�u�W���</�Y/r!{��8�` �r ��� ~7�^�h�yՖ��&fV" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.1
string(32) "c5fe4de55999a72ff1626083b4f53722" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "�]��`j5�d�2�I��H�;�d<����: G;�I�ԩ��� � {o�/'^Ŋ�*" string(41) "Ready your ammunition; we attack at dawn."
Output for 7.0.0
string(32) "e5488b32033ebde750b3d1626eff6162" Notice: Undefined variable: hex in /in/vsqNe on line 77 NULL string(57) "ߛ�$X�ח� Lr����'t�j�q��[�,��L��8 7�p���[�Ek2��l_�" string(41) "Ready your ammunition; we attack at dawn."

preferences:
52.22 ms | 401 KiB | 37 Q