1
+ <?php
2
+
3
+ namespace Monster \App \Models ;
4
+
5
+ class Cipher
6
+ {
7
+ private $ key ;
8
+
9
+ public function __construct ($ key )
10
+ {
11
+ $ this ->key = $ key ;
12
+ }
13
+
14
+ public function encrypt ($ message )
15
+ {
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 ;
23
+ }
24
+
25
+ public function decrypt ($ encryptedMessage )
26
+ {
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 ;
63
+ }
64
+ }
65
+
66
+
67
+ // Usage example:
68
+ /*
69
+ $key = "your_secret_key";
70
+ $cipher = new Cipher($key);
71
+ $message = "Hello, World!";
72
+ $encryptedMessage = $cipher->encrypt($message);
73
+ $decryptedMessage = $cipher->decrypt($encryptedMessage);
74
+
75
+ echo "Original message: " . $message . "\n";
76
+ echo "Encrypted message: " . $encryptedMessage . "\n";
77
+ echo "Decrypted message: " . $decryptedMessage . "\n";
78
+ */
0 commit comments