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 AES256Encryption
28+ {
29+
30+ private static string $ OPENSSL_CIPHER_NAME = "aes-256-cbc " ; //Name of OpenSSL Cipher
31+ private static int $ CIPHER_KEY_LEN = 32 ; // 32 bytes (256 bits)
32+
33+ static function getRandomIV ()
34+ {
35+ $ length = 16 ;
36+ $ characters = '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*()-=_+ABCDEFGHIJKLMNOPQRSTUVWXYZ ' ;
37+ $ charactersLength = strlen ($ characters );
38+ $ randomString = '' ;
39+ for ($ i = 0 ; $ i < $ length ; $ i ++)
40+ {
41+ $ randomString .= $ characters [rand (0 , $ charactersLength - 1 )];
42+ }
43+
44+ return $ randomString ;
45+ }
46+
47+ /**
48+
49+ * Encrypt data using AES Cipher (CBC) with 256 bit key
50+ * @param type $key - key to use should be 32 bytes long (256 bits)
51+ * @param type $data - data to encrypt
52+ * @return encrypted data in base64 encoding with iv attached at end after a :
53+ */
54+ static function encrypt (string $ key , string $ data )
55+ {
56+ $ iv = AES256Encryption::getRandomIV ();
57+
58+ if (strlen ($ key ) < AES256Encryption::$ CIPHER_KEY_LEN )
59+ {
60+ $ key = str_pad ($ key , AES256Encryption::$ CIPHER_KEY_LEN , "0 " ); //0 pad to len 32
61+ }
62+ else if (strlen ($ key ) > AES256Encryption::$ CIPHER_KEY_LEN )
63+ {
64+ $ key = substr ($ str , 0 , AES256Encryption::$ CIPHER_KEY_LEN ); //truncate to 32 bytes
65+ }
66+
67+ $ encodedEncryptedData = base64_encode (openssl_encrypt ($ data , AES256Encryption::$ OPENSSL_CIPHER_NAME , $ key , OPENSSL_RAW_DATA , $ iv ));
68+ $ encodedIV = base64_encode ($ iv );
69+ $ encryptedPayload = $ encodedEncryptedData . ": " . $ encodedIV ;
70+ return $ encryptedPayload ;
71+ }
72+
73+ /**
74+ * Decrypt data using AES Cipher (CBC) with 256 bit key
75+ * @param type $key - key to use should be 32 bytes long (256 bits)
76+ * @param type $data - data to be decrypted in base64 encoding with iv attached at the end after a :
77+ * @return decrypted data
78+ */
79+ static function decrypt (string $ key , string $ data )
80+ {
81+ if (strlen ($ key ) < AES256Encryption::$ CIPHER_KEY_LEN )
82+ {
83+ $ key = str_pad ($ key , AES256Encryption::$ CIPHER_KEY_LEN , "0 " ); //0 pad to len 32
84+ }
85+ else if (strlen ($ key ) > AES256Encryption::$ CIPHER_KEY_LEN )
86+ {
87+ $ key = substr ($ str , 0 , AES256Encryption::$ CIPHER_KEY_LEN ); //truncate to 32 bytes
88+ }
89+
90+ $ parts = explode (': ' , $ data ); //Separate Encrypted data from iv.
91+ $ decryptedData = openssl_decrypt (base64_decode ($ parts [0 ]), AES256Encryption::$ OPENSSL_CIPHER_NAME , $ key , OPENSSL_RAW_DATA , base64_decode ($ parts [1 ]));
92+ return $ decryptedData ;
93+ }
94+ }
95+
96+ ?>
0 commit comments