5
5
class Cipher
6
6
{
7
7
private $ key ;
8
+ private $ cipher ;
8
9
9
- public function __construct ($ key )
10
+ public function __construct ($ key, $ cipher )
10
11
{
11
12
$ this ->key = $ key ;
13
+ $ this ->cipher = $ cipher ;
12
14
}
13
15
14
- public function encrypt ($ message )
16
+ public function encrypt ($ data )
15
17
{
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 );
23
21
}
24
22
25
- public function decrypt ($ encryptedMessage )
23
+ public function decrypt ($ encryptedData )
26
24
{
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 );
63
30
}
64
31
}
65
32
66
33
67
- // Usage example:
68
34
/*
69
35
$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);
74
42
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 /> ";
78
46
*/
0 commit comments