1+ <?php
2+
3+ /*
4+ MIT License
5+
6+ Copyright (c) 2022 Nikos Siatras
7+
8+ Permission is hereby granted, free of charge, to any person obtaining a copy
9+ of this software and associated documentation files (the "Software"), to deal
10+ in the Software without restriction, including without limitation the rights
11+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+ copies of the Software, and to permit persons to whom the Software is
13+ furnished to do so, subject to the following conditions:
14+
15+ The above copyright notice and this permission notice shall be included in all
16+ copies or substantial portions of the Software.
17+
18+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+ SOFTWARE.
25+ */
26+
27+ class AES128Encryption
28+ {
29+
30+ private static string $ OPENSSL_CIPHER_NAME = "aes-128-cbc " ; //Name of OpenSSL Cipher
31+ private static int $ CIPHER_KEY_LEN = 16 ; // 16 bytes (128 bits)
32+
33+ static function getRandomIV ()
34+ {
35+ $ characters = '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*()-=_+ABCDEFGHIJKLMNOPQRSTUVWXYZ ' ;
36+ $ charactersLength = strlen ($ characters );
37+ $ randomString = '' ;
38+ for ($ i = 0 ; $ i < AES128Encryption::$ CIPHER_KEY_LEN ; $ i ++)
39+ {
40+ $ randomString .= $ characters [rand (0 , $ charactersLength - 1 )];
41+ }
42+
43+ return $ randomString ;
44+ }
45+
46+ /**
47+
48+ * Encrypt data using AES Cipher (CBC) with 128 bit key
49+ * @param type $key - key to use should be 16 bytes long (128 bits)
50+ * @param type $data - data to encrypt
51+ * @return encrypted data in base64 encoding with iv attached at end after a :
52+ */
53+ static function encrypt (string $ key , string $ data )
54+ {
55+ $ iv = AES128Encryption::getRandomIV ();
56+
57+ if (strlen ($ key ) < AES128Encryption::$ CIPHER_KEY_LEN )
58+ {
59+ $ key = str_pad ($ key , AES128Encryption::$ CIPHER_KEY_LEN , "0 " ); //0 pad to len 16
60+ }
61+ else if (strlen ($ key ) > AES128Encryption::$ CIPHER_KEY_LEN )
62+ {
63+ $ key = substr ($ str , 0 , AES128Encryption::$ CIPHER_KEY_LEN ); //truncate to 16 bytes
64+ }
65+
66+ $ encodedEncryptedData = base64_encode (openssl_encrypt ($ data , AES128Encryption::$ OPENSSL_CIPHER_NAME , $ key , OPENSSL_RAW_DATA , $ iv ));
67+ $ encodedIV = base64_encode ($ iv );
68+ $ encryptedPayload = $ encodedEncryptedData . ": " . $ encodedIV ;
69+ return $ encryptedPayload ;
70+ }
71+
72+ /**
73+ * Decrypt data using AES Cipher (CBC) with 128 bit key
74+ * @param type $key - key to use should be 16 bytes long (128 bits)
75+ * @param type $data - data to be decrypted in base64 encoding with iv attached at the end after a :
76+ * @return decrypted data
77+ */
78+ static function decrypt (string $ key , string $ data )
79+ {
80+ if (strlen ($ key ) < AES128Encryption::$ CIPHER_KEY_LEN )
81+ {
82+ $ key = str_pad ("$ key " , AES128Encryption::$ CIPHER_KEY_LEN , "0 " ); //0 pad to len 16
83+ }
84+ else if (strlen ($ key ) > AES128Encryption::$ CIPHER_KEY_LEN )
85+ {
86+ $ key = substr ($ str , 0 , AES128Encryption::$ CIPHER_KEY_LEN ); //truncate to 16 bytes
87+ }
88+
89+ $ parts = explode (': ' , $ data ); //Separate Encrypted data from iv.
90+ $ decryptedData = openssl_decrypt (base64_decode ($ parts [0 ]), AES128Encryption::$ OPENSSL_CIPHER_NAME , $ key , OPENSSL_RAW_DATA , base64_decode ($ parts [1 ]));
91+ return $ decryptedData ;
92+ }
93+ }
94+
95+ ?>
0 commit comments