1+ package ciphers ;
2+
3+ import java .util .*;
4+
5+ /**
6+ *
7+ * The simple substitution cipher is a cipher that has been in use for many hundreds of years
8+ * (an excellent history is given in Simon Singhs 'the Code Book').
9+ * It basically consists of substituting every plaintext character for a different ciphertext character.
10+ * It differs from the Caesar cipher in that the cipher alphabet is not simply the alphabet shifted,
11+ * it is completely jumbled.
12+ *
13+ * @author Hassan Elseoudy
14+ */
15+
16+ public class SimpleSubstitutionCipher {
17+
18+ /**
19+ * Encrypt text by replacing each element with its opposite character.
20+ *
21+ * @param message
22+ * @param cipherSmall
23+ * @return Encrypted message
24+ */
25+ public static String encode (String message , String cipherSmall ) {
26+ String encoded = "" ;
27+
28+ // This map is used to encode
29+ Map <Character ,Character > cipherMap = new HashMap <Character ,Character >();
30+
31+ char beginSmallLetter = 'a' ;
32+ char beginCapitalLetter = 'A' ;
33+
34+ cipherSmall = cipherSmall .toLowerCase ();
35+ String cipherCapital = cipherSmall .toUpperCase ();
36+
37+ // To handle Small and Capital letters
38+ for (int i = 0 ; i < cipherSmall .length (); i ++){
39+ cipherMap .put (beginSmallLetter ++,cipherSmall .charAt (i ));
40+ cipherMap .put (beginCapitalLetter ++,cipherCapital .charAt (i ));
41+ }
42+
43+ for (int i = 0 ; i < message .length (); i ++){
44+ if (Character .isAlphabetic (message .charAt (i )))
45+ encoded += cipherMap .get (message .charAt (i ));
46+ else
47+ encoded += message .charAt (i );
48+ }
49+
50+ return encoded ;
51+ }
52+
53+ /**
54+ * Decrypt message by replacing each element with its opposite character in cipher.
55+ *
56+ * @param encryptedMessage
57+ * @param cipherSmall
58+ * @return message
59+ */
60+ public static String decode (String encryptedMessage , String cipherSmall ) {
61+ String decoded = "" ;
62+
63+
64+ Map <Character ,Character > cipherMap = new HashMap <Character ,Character >();
65+
66+ char beginSmallLetter = 'a' ;
67+ char beginCapitalLetter = 'A' ;
68+
69+ cipherSmall = cipherSmall .toLowerCase ();
70+ String cipherCapital = cipherSmall .toUpperCase ();
71+
72+ for (int i = 0 ; i < cipherSmall .length (); i ++){
73+ cipherMap .put (cipherSmall .charAt (i ),beginSmallLetter ++);
74+ cipherMap .put (cipherCapital .charAt (i ),beginCapitalLetter ++);
75+ }
76+
77+ for (int i = 0 ; i < encryptedMessage .length (); i ++){
78+ if (Character .isAlphabetic (encryptedMessage .charAt (i )))
79+ decoded += cipherMap .get (encryptedMessage .charAt (i ));
80+ else
81+ decoded += encryptedMessage .charAt (i );
82+ }
83+
84+ return decoded ;
85+ }
86+
87+ /**
88+ *
89+ * TODO remove main and make JUnit Testing
90+ */
91+ public static void main (String [] args ) {
92+ String a = encode ("defend the east wall of the castle" ,"phqgiumeaylnofdxjkrcvstzwb" );
93+ String b = decode (a ,"phqgiumeaylnofdxjkrcvstzwb" );
94+ System .out .println (b );
95+ }
96+
97+ }
0 commit comments