From d5ddc351d8bfa6f1a80bdc8f41b945b612ea8c08 Mon Sep 17 00:00:00 2001 From: Hassan Date: Tue, 28 Jan 2020 19:35:16 +0200 Subject: [PATCH 1/2] Simple Substitution Cipher Algorithm added. --- ciphers/SimpleSubstitutionCipher.java | 97 +++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 ciphers/SimpleSubstitutionCipher.java diff --git a/ciphers/SimpleSubstitutionCipher.java b/ciphers/SimpleSubstitutionCipher.java new file mode 100644 index 00000000..983bc4f6 --- /dev/null +++ b/ciphers/SimpleSubstitutionCipher.java @@ -0,0 +1,97 @@ +package ciphers; + +import java.util.*; + +/** + * + * 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 { + + /** + * Encrypt text by replacing each element with its opposite character. + * + * @param message + * @param cipherSmall + * @return Encrypted message + */ + public static String encode(String message, String cipherSmall) { + String encoded = ""; + + // This map is used to encode + Map cipherMap = new HashMap(); + + 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)); + } + + for(int i = 0; i < message.length(); i++){ + if(Character.isAlphabetic(message.charAt(i))) + encoded += cipherMap.get(message.charAt(i)); + else + encoded += message.charAt(i); + } + + return encoded; + } + + /** + * Decrypt message by replacing each element with its opposite character in cipher. + * + * @param encryptedMessage + * @param cipherSmall + * @return message + */ + public static String decode(String encryptedMessage, String cipherSmall) { + String decoded = ""; + + + Map cipherMap = new HashMap(); + + char beginSmallLetter = 'a'; + char beginCapitalLetter = 'A'; + + cipherSmall = cipherSmall.toLowerCase(); + String cipherCapital = cipherSmall.toUpperCase(); + + for(int i = 0; i < cipherSmall.length(); i++){ + cipherMap.put(cipherSmall.charAt(i),beginSmallLetter++); + cipherMap.put(cipherCapital.charAt(i),beginCapitalLetter++); + } + + for(int i = 0; i < encryptedMessage.length(); i++){ + if(Character.isAlphabetic(encryptedMessage.charAt(i))) + decoded += cipherMap.get(encryptedMessage.charAt(i)); + else + decoded += encryptedMessage.charAt(i); + } + + return decoded; + } + + /** + * + * 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); + } + +} \ No newline at end of file From e21d0efd6ae46bf744eb4d4164a4ca2c5e376d7d Mon Sep 17 00:00:00 2001 From: nikhil kala Date: Wed, 29 Jan 2020 01:46:27 +0530 Subject: [PATCH 2/2] Update SimpleSubstitutionCipher.java --- ciphers/SimpleSubstitutionCipher.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ciphers/SimpleSubstitutionCipher.java b/ciphers/SimpleSubstitutionCipher.java index 983bc4f6..d9bae4f0 100644 --- a/ciphers/SimpleSubstitutionCipher.java +++ b/ciphers/SimpleSubstitutionCipher.java @@ -94,4 +94,4 @@ public class SimpleSubstitutionCipher { System.out.println(b); } -} \ No newline at end of file +}