55class Cipher
66{
77 private $ key ;
8+ private $ cipher ;
89
9- public function __construct ($ key )
10+ public function __construct ($ key, $ cipher )
1011 {
1112 $ this ->key = $ key ;
13+ $ this ->cipher = $ cipher ;
1214 }
1315
14- public function encrypt ($ message )
16+ public function encrypt ($ data )
1517 {
16- // Custom encryption algorithm
17- $ encrypted = str_rot13 ($ message ); // Example: using ROT13 substitution
18-
19- // Additional encryption steps using the key
20- $ encrypted = $ this ->xorEncrypt ($ encrypted );
21-
22- return $ encrypted ;
18+ $ iv = openssl_random_pseudo_bytes (openssl_cipher_iv_length ($ this ->cipher ));
19+ $ encrypted = openssl_encrypt ($ data , $ this ->cipher , $ this ->key , OPENSSL_RAW_DATA , $ iv );
20+ return base64_encode ($ iv . $ encrypted );
2321 }
2422
25- public function decrypt ($ encryptedMessage )
23+ public function decrypt ($ encryptedData )
2624 {
27- // Reverse the additional encryption steps using the key
28- $ decrypted = $ this ->xorDecrypt ($ encryptedMessage );
29-
30- // Custom decryption algorithm
31- $ decrypted = str_rot13 ($ decrypted ); // Example: reversing ROT13 substitution
32-
33- return $ decrypted ;
34- }
35-
36- private function xorEncrypt ($ message )
37- {
38- $ key = $ this ->key ;
39- $ keyLength = strlen ($ key );
40- $ messageLength = strlen ($ message );
41- $ encrypted = '' ;
42-
43- for ($ i = 0 ; $ i < $ messageLength ; $ i ++) {
44- $ encrypted .= $ message [$ i ] ^ $ key [$ i % $ keyLength ];
45- }
46-
47- return base64_encode ($ encrypted );
48- }
49-
50- private function xorDecrypt ($ encryptedMessage )
51- {
52- $ key = $ this ->key ;
53- $ keyLength = strlen ($ key );
54- $ encryptedMessage = base64_decode ($ encryptedMessage );
55- $ messageLength = strlen ($ encryptedMessage );
56- $ decrypted = '' ;
57-
58- for ($ i = 0 ; $ i < $ messageLength ; $ i ++) {
59- $ decrypted .= $ encryptedMessage [$ i ] ^ $ key [$ i % $ keyLength ];
60- }
61-
62- return $ decrypted ;
25+ $ encryptedData = base64_decode ($ encryptedData );
26+ $ ivLength = openssl_cipher_iv_length ($ this ->cipher );
27+ $ iv = substr ($ encryptedData , 0 , $ ivLength );
28+ $ encrypted = substr ($ encryptedData , $ ivLength );
29+ return openssl_decrypt ($ encrypted , $ this ->cipher , $ this ->key , OPENSSL_RAW_DATA , $ iv );
6330 }
6431}
6532
6633
67- // Usage example:
6834/*
6935$key = "your_secret_key";
70- $cipher = new Cipher($key);
71- $message = "Hello, World!";
72- $encryptedMessage = $cipher->encrypt($message);
73- $decryptedMessage = $cipher->decrypt($encryptedMessage);
36+ $cipher = "AES-256-CBC";
37+ $encryption = new Cipher($key, $cipher);
38+
39+ $plainText = "Hello, World!";
40+ $encryptedText = $encryption->encrypt($plainText);
41+ $decryptedText = $encryption->decrypt($encryptedText);
7442
75- echo "Original message : " . $message . "\n ";
76- echo "Encrypted message : " . $encryptedMessage . "\n ";
77- echo "Decrypted message : " . $decryptedMessage . "\n ";
43+ echo "Plain Text : " . $plainText . "<br /> ";
44+ echo "Encrypted Text : " . $encryptedText . "<br /> ";
45+ echo "Decrypted Text : " . $decryptedText . "<br /> ";
7846*/
0 commit comments