1- <?php
2- // A mono-alphabetic cipher is a simple substitution cipher
3- // https://www.101computing.net/mono-alphabetic-substitution-cipher/
4-
5- function monoAlphabeticCipher ($ key , $ alphabet , $ text ){
6-
7- $ cipherText = '' ; // the cipher text (can be decrypted and encrypted)
8-
9- if ( strlen ($ key ) != strlen ($ alphabet ) ) { return false ; } // check if the text length matches
10- $ text = preg_replace ('/[0-9]+/ ' , '' , $ text ); // remove all the numbers
11-
12- for ( $ i = 0 ; $ i < strlen ($ text ); $ i ++ ){
13- $ index = strripos ( $ alphabet , $ text [$ i ] );
14- if ( $ text [$ i ] == " " ){ $ cipherText .= " " ; }
15- else { $ cipherText .= ( ctype_upper ($ text [$ i ]) ? strtoupper ($ key [$ index ]) : $ key [$ index ] ); }
16- }
17- return $ cipherText ;
18- }
19-
20- function maEncrypt ($ key , $ alphabet , $ text ){
21-
22- return monoAlphabeticCipher ($ key , $ alphabet , $ text );
23-
24- }
25-
26- function maDecrypt ($ key , $ alphabet , $ text ){
27-
28- return monoAlphabeticCipher ($ alphabet , $ key , $ text );
29-
30- }
31-
32- ?>
1+ <?php
2+
3+ // A mono-alphabetic cipher is a simple substitution cipher
4+ // https://www.101computing.net/mono-alphabetic-substitution-cipher/
5+
6+ function monoAlphabeticCipher ($ key , $ alphabet , $ text )
7+ {
8+ $ cipherText = '' ; // the cipher text (can be decrypted and encrypted)
9+
10+ // check if the text length matches
11+ if (strlen ($ key ) != strlen ($ alphabet )) {
12+ return false ;
13+ }
14+
15+ $ text = preg_replace ('/[0-9]+/ ' , '' , $ text ); // remove all the numbers
16+
17+ for ($ i = 0 ; $ i < strlen ($ text ); $ i ++) {
18+ $ index = strripos ($ alphabet , $ text [$ i ]);
19+ if ($ text [$ i ] == " " ) {
20+ $ cipherText .= " " ;
21+ } else {
22+ $ cipherText .= ( ctype_upper ($ text [$ i ]) ? strtoupper ($ key [$ index ]) : $ key [$ index ] );
23+ }
24+ }
25+
26+ return $ cipherText ;
27+ }
28+
29+ function maEncrypt ($ key , $ alphabet , $ text )
30+ {
31+ return monoAlphabeticCipher ($ key , $ alphabet , $ text );
32+ }
33+
34+ function maDecrypt ($ key , $ alphabet , $ text )
35+ {
36+ return monoAlphabeticCipher ($ alphabet , $ key , $ text );
37+ }
0 commit comments