From 8b243667d736be7d0413cc77159378aa02459db0 Mon Sep 17 00:00:00 2001 From: khalil2535 Date: Mon, 9 Apr 2018 21:59:13 +0300 Subject: [PATCH 1/3] Update Caesar.java --- ciphers/Caesar.java | 219 +++++++++++++++++++++----------------------- 1 file changed, 106 insertions(+), 113 deletions(-) diff --git a/ciphers/Caesar.java b/ciphers/Caesar.java index 32bc87b86e95..b9f8e3f4bb87 100644 --- a/ciphers/Caesar.java +++ b/ciphers/Caesar.java @@ -1,117 +1,110 @@ -/** -Author : FAHRI YARDIMCI +package ciphers; -A Java implementation of Caesar Cipher. -/It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. / -**/ import java.util.Scanner; + +/** + * + * A Java implementation of Caesar Cipher. /It is a type of substitution cipher + * in which each letter in the plaintext is replaced by a letter some fixed + * number of positions down the alphabet. / + * + * @author FAHRI YARDIMCI + * @author khalil2535 + */ public class Caesar { -public static String encode (String message,int shift) -{ - String encoded = ""; - for(int i = 0 ; i=65 && current<= 90) - { - int numAlphabet = message.charAt(i); - if(shift + numAlphabet > 90) - { - int j = 90 - numAlphabet; - char nextKey = (char)(65 + (shift - j - 1)); - encoded += nextKey; - - } - else - { - char nextKey = (char)(current + shift); - encoded += nextKey; - } - } - else if (current>=97 && current <= 122) - { - int numAlphabet = message.charAt(i); - if(shift + numAlphabet > 122) - { - int j = 122 - numAlphabet; - char nextKey = (char)(97 + (shift - j - 1)); - encoded += nextKey; - } - else - { - char nextKey = (char)(current + shift); - encoded += nextKey; - } - } - } - return encoded; -} -public static String decode (String message,int shift) -{ - String decoded = ""; - for(int i = 0 ; i=65 && current<= 90) - { - int numAlphabet = message.charAt(i); - if(numAlphabet - shift < 65) - { - int j = numAlphabet - 65; - char nextKey = (char)(90 - (shift - j - 1)); - decoded += nextKey; - - } - else - { - char nextKey = (char)(current - shift); - decoded += nextKey; - } - } - else if (current>=97 && current <= 122) - { - int numAlphabet = message.charAt(i); - if(numAlphabet - shift < 97) - { - int j = numAlphabet - 97; - char nextKey = (char)(122 - (shift - j - 1)); - decoded += nextKey; - } - else - { - char nextKey = (char)(current - shift); - decoded += nextKey; - } - } - } - return decoded; -} -public static void main(String[] args) -{ - Scanner input = new Scanner(System.in); - System.out.println("Please enter the message (Latin Alphabet)"); - String message = input.nextLine(); - System.out.println(message); - System.out.println("Please enter the shift number"); - int shift = input.nextInt() % 26; - System.out.println("(E)ncode or (D)ecode ?"); - char choice = input.next().charAt(0); - if(choice == 'E' || choice=='e') - System.out.println("ENCODED MESSAGE IS \n" + encode(message,shift)); //send our function to handle - if(choice =='D' || choice =='d') - System.out.println("DECODED MESSAGE IS \n" + decode(message,shift)); -} -} \ No newline at end of file + /** + * Encrypt text by shifting every Latin char by add number shift for ASCII + * Example : A + 1 -> B + * + * @param message + * @param shift + * @return Encrypted message + */ + public static String encode(String message, int shift) { + String encoded = ""; + + while (shift >= 26) { // 26 = number of latin letters + shift -= 26; + } + + final int length = message.length(); + for (int i = 0; i < length; i++) { + +// int current = message.charAt(i); //using char to shift characters because ascii is in-order latin alphabet + char current = message.charAt(i); // Java law : char + int = char + + if (current >= 'A' && current <= 'Z') { + + current += shift; + encoded += (char) (current > 'Z' ? current - 26 : current); // 26 = number of latin letters + + } else if (current >= 'a' && current <= 'z') { + + current += shift; + encoded += (char) (current > 'z' ? current - 26 : current); // 26 = number of latin letters + + } else { + encoded += current; + } + } + return encoded; + } + + /** + * Decrypt message by shifting back every Latin char to previous the ASCII + * Example : B - 1 -> A + * + * @param encryptedMessage + * @param shift + * @return message + */ + public static String decode(String encryptedMessage, int shift) { + String decoded = ""; + + while (shift >= 26) { // 26 = number of latin letters + shift -= 26; + } + + final int length = encryptedMessage.length(); + for (int i = 0; i < length; i++) { + char current = encryptedMessage.charAt(i); + if (current >= 'A' && current <= 'Z') { + + current -= shift; + decoded += (char) (current < 'A' ? current + 26 : current);// 26 = number of latin letters + + } else if (current >= 'a' && current <= 'z') { + + current -= shift; + decoded += (char) (current < 'a' ? current + 26 : current);// 26 = number of latin letters + + } else { + decoded += current; + } + } + return decoded; + } + + /** + * + * @deprecated TODO remove main and make JUnit Testing + */ + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.println("Please enter the message (Latin Alphabet)"); + String message = input.nextLine(); + System.out.println(message); + System.out.println("Please enter the shift number"); + int shift = input.nextInt() % 26; + System.out.println("(E)ncode or (D)ecode ?"); + char choice = input.next().charAt(0); + if (choice == 'E' || choice == 'e') { + System.out.println("ENCODED MESSAGE IS \n" + encode(message, shift)); //send our function to handle + } + if (choice == 'D' || choice == 'd') { + System.out.println("DECODED MESSAGE IS \n" + decode(message, shift)); + } + } + +} From 9060d2da29b8312f43f3e4b0841caa24a0558a35 Mon Sep 17 00:00:00 2001 From: khalil2535 Date: Wed, 11 Apr 2018 22:01:20 +0300 Subject: [PATCH 2/3] Update Caesar.java --- ciphers/Caesar.java | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/ciphers/Caesar.java b/ciphers/Caesar.java index b9f8e3f4bb87..d646f826edec 100644 --- a/ciphers/Caesar.java +++ b/ciphers/Caesar.java @@ -69,12 +69,12 @@ public static String decode(String encryptedMessage, int shift) { final int length = encryptedMessage.length(); for (int i = 0; i < length; i++) { char current = encryptedMessage.charAt(i); - if (current >= 'A' && current <= 'Z') { + if (IsCapitalLatinLetter(current)) { current -= shift; decoded += (char) (current < 'A' ? current + 26 : current);// 26 = number of latin letters - } else if (current >= 'a' && current <= 'z') { + } else if (IsSmallLatinLetter(current)) { current -= shift; decoded += (char) (current < 'a' ? current + 26 : current);// 26 = number of latin letters @@ -86,6 +86,24 @@ public static String decode(String encryptedMessage, int shift) { return decoded; } + /** + * + * @param c + * @return true if character is capital Latin letter or false for others + */ + private static boolean IsCapitalLatinLetter(char c) { + return c >= 'A' && c <= 'Z'; + } + + /** + * + * @param c + * @return true if character is small Latin letter or false for others + */ + private static boolean IsSmallLatinLetter(char c) { + return c >= 'a' && c <= 'z'; + } + /** * * @deprecated TODO remove main and make JUnit Testing @@ -99,11 +117,14 @@ public static void main(String[] args) { int shift = input.nextInt() % 26; System.out.println("(E)ncode or (D)ecode ?"); char choice = input.next().charAt(0); - if (choice == 'E' || choice == 'e') { - System.out.println("ENCODED MESSAGE IS \n" + encode(message, shift)); //send our function to handle - } - if (choice == 'D' || choice == 'd') { - System.out.println("DECODED MESSAGE IS \n" + decode(message, shift)); + switch (choice) { + case 'E': + case 'e': + System.out.println("ENCODED MESSAGE IS \n" + encode(message, shift)); //send our function to handle + break; + case 'D': + case 'd': + System.out.println("DECODED MESSAGE IS \n" + decode(message, shift)); } } From ca8f6dbb384c2a64cb5e72c334b3e3a61481c414 Mon Sep 17 00:00:00 2001 From: khalil2535 Date: Wed, 11 Apr 2018 22:07:37 +0300 Subject: [PATCH 3/3] Update Caesar.java --- ciphers/Caesar.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ciphers/Caesar.java b/ciphers/Caesar.java index d646f826edec..77cd5b9ebcf6 100644 --- a/ciphers/Caesar.java +++ b/ciphers/Caesar.java @@ -34,12 +34,12 @@ public static String encode(String message, int shift) { // int current = message.charAt(i); //using char to shift characters because ascii is in-order latin alphabet char current = message.charAt(i); // Java law : char + int = char - if (current >= 'A' && current <= 'Z') { + if (IsCapitalLatinLetter(current)) { current += shift; encoded += (char) (current > 'Z' ? current - 26 : current); // 26 = number of latin letters - } else if (current >= 'a' && current <= 'z') { + } else if (IsSmallLatinLetter(current)) { current += shift; encoded += (char) (current > 'z' ? current - 26 : current); // 26 = number of latin letters