JavaAlgorithms/Ciphers/SimpleSubstitutionCipher.java

84 lines
2.7 KiB
Java
Raw Normal View History

2021-09-26 15:57:21 +08:00
package Ciphers;
import java.util.*;
/**
2020-10-24 18:23:28 +08:00
* The simple substitution cipher is a cipher that has been in use for many hundreds of years (an
* excellent history is given in Simon Singhs 'the Code Book'). It basically consists of
* substituting every plaintext character for a different ciphertext character. It differs from the
* Caesar cipher in that the cipher alphabet is not simply the alphabet shifted, it is completely
* jumbled.
*
* @author Hassan Elseoudy
*/
public class SimpleSubstitutionCipher {
2020-10-24 18:23:28 +08:00
/**
* Encrypt text by replacing each element with its opposite character.
*
* @return Encrypted message
*/
public static String encode(String message, String cipherSmall) {
2021-09-26 15:57:21 +08:00
StringBuilder encoded = new StringBuilder();
2020-10-24 18:23:28 +08:00
// This map is used to encode
2021-09-26 15:57:21 +08:00
Map<Character, Character> cipherMap = new HashMap<>();
2020-10-24 18:23:28 +08:00
char beginSmallLetter = 'a';
char beginCapitalLetter = 'A';
cipherSmall = cipherSmall.toLowerCase();
String cipherCapital = cipherSmall.toUpperCase();
// To handle Small and Capital letters
for (int i = 0; i < cipherSmall.length(); i++) {
cipherMap.put(beginSmallLetter++, cipherSmall.charAt(i));
cipherMap.put(beginCapitalLetter++, cipherCapital.charAt(i));
}
2020-10-24 18:23:28 +08:00
for (int i = 0; i < message.length(); i++) {
2021-09-26 15:57:21 +08:00
if (Character.isAlphabetic(message.charAt(i))) encoded.append(cipherMap.get(message.charAt(i)));
else encoded.append(message.charAt(i));
2020-10-24 18:23:28 +08:00
}
2021-09-26 15:57:21 +08:00
return encoded.toString();
2020-10-24 18:23:28 +08:00
}
2020-10-24 18:23:28 +08:00
/**
* Decrypt message by replacing each element with its opposite character in cipher.
*
* @return message
*/
public static String decode(String encryptedMessage, String cipherSmall) {
2021-09-26 15:57:21 +08:00
StringBuilder decoded = new StringBuilder();
2021-09-26 15:57:21 +08:00
Map<Character, Character> cipherMap = new HashMap<>();
2020-10-24 18:23:28 +08:00
char beginSmallLetter = 'a';
char beginCapitalLetter = 'A';
2020-10-24 18:23:28 +08:00
cipherSmall = cipherSmall.toLowerCase();
String cipherCapital = cipherSmall.toUpperCase();
2020-10-24 18:23:28 +08:00
for (int i = 0; i < cipherSmall.length(); i++) {
cipherMap.put(cipherSmall.charAt(i), beginSmallLetter++);
cipherMap.put(cipherCapital.charAt(i), beginCapitalLetter++);
}
2020-10-24 18:23:28 +08:00
for (int i = 0; i < encryptedMessage.length(); i++) {
if (Character.isAlphabetic(encryptedMessage.charAt(i)))
2021-09-26 15:57:21 +08:00
decoded.append(cipherMap.get(encryptedMessage.charAt(i)));
else decoded.append(encryptedMessage.charAt(i));
}
2021-09-26 15:57:21 +08:00
return decoded.toString();
2020-10-24 18:23:28 +08:00
}
/** TODO remove main and make JUnit Testing */
public static void main(String[] args) {
String a = encode("defend the east wall of the castle", "phqgiumeaylnofdxjkrcvstzwb");
String b = decode(a, "phqgiumeaylnofdxjkrcvstzwb");
System.out.println(b);
}
2020-01-29 04:16:27 +08:00
}