Question 2 Cipher 50 points Caesars cipher is a very well kn

Question 2: Cipher (50 points) Caesar\'s cipher is a very well known and simple encryption scheme. The point of an encryption scheme is to transform a message so that only those authorized will be able to read it. Caesar\'s cipher conceals a message by replacing each letter in the original message (the plainteat), by a letter corresponding to a certain number of letters to the right on the alphabet. Of course, the message can be retrieved by replacing each letter in the encoded message (the ciphertezt) with the letter corresponding to the same number of position to the left on the alphabet. To achieve this, the cipher has a key that needs to be kept private. Only those with the key can encode and decode a message. Such a key determines the shift that needs to be performed on each letter. For example, here is how a string containing the entire alphabet will be encrypted using a key equal to 3: Original: abcdefghijklmnopqrstuvwxyz Encrypted: def ghijklmnopgrstuvwxyzabc Vigenere\'s cipl is a slightly more complex encryption scheme, also used to transform a message. The her Page 5 key of this cipher consists of a word and the er works by applying multiple Caesar ciphers based on the letters of the keyword. Each letter can be associated with a number corresponding to its position in the English alphabet (counting from 0). For instance, the letter \'a\' is associated to 0, \'c\' to 2, and \"z\' to 25. Therefore, the keyword of the cipher will provide as many integers as letters in the word and these integers will be used to implement different Caesar ciphers Let\'s see how: suppose the messag to encrypt is \"el and the keyword is \"rats\". The first thing elephants\" to do is to repeat the keyword until its length matches the one of the message e 1 e p h a Message: Keyword: r Now, each letter of \"ratsratsr is associated to both a letter in the message and an integer. We can encrypt each letter of the message using a Caesar cipher where the key corresponds to the integer associated to it through the keyword. In this case r corresponds to 17, so the first letter of the message which is an \'e\' will be encrypted using a v the second letter \'l\' as an \"l\' since \'a\' is associated to 0, and so on. The entire message will be encrypted as \"vlxhyaglj The goal of this exercise is to write several methods in order to create a program that encodes and decodes messages using Caesar s and Vigenere\'s ciphers. For the purpose of this exercise we will only consider messages written using lower case letters and blank spaces. All the code for this question must be placed in a file named Cipher ava.

Solution

Cipher.java

Implements Caeser and Vigenere\'s cipher - both encoding and decoding

public class Cipher {
  
   /**
   * Caeser cipher encoding
   * @param str
   * @param key
   * @return
   */
   public String cipherEncode(String str, int key){
      
       if(key<0 || key>25){
           System.out.println(\"Error: Key should be between 0 and 25\");
           return \"\";
       }
       StringBuilder build = new StringBuilder();
       for(int i=0;i<str.length();i++){
           build.append(charRightShift(str.charAt(i), key));
       }
       return build.toString();
   }
  
   /**
   * Caeser cipher decoding
   * @param str
   * @param key
   * @return
   */
   public String cipherDecode(String str, int key){
      
       if(key<0 || key>25){
           System.out.println(\"Error: Key should be between 0 and 25\");
           return \"\";
       }
      
       StringBuilder build = new StringBuilder();
      
       for(int i=0;i<str.length();i++){
           build.append(charLeftShift(str.charAt(i),key));          
       }
      
       return build.toString();
   }
  
   /**
   * Method to obtain keys of a string
   * @param str
   * @return
   */
   public int[] obtainKeys(String str){
       int arr[] = new int[str.length()];
      
       for(int i=0;i<str.length();i++){
           arr[i] = str.charAt(i)-97;
       }
      
       return arr;
   }
  
   /**
   * Vignere encoding
   * @param str
   * @param key
   * @return
   */
   public String vignereEncode(String str, String key){
      
       for(char ch:key.toCharArray()){
           if(ch<97 || ch>122){
               System.out.println(\"Error: Keyword contains character other than lowercase alphabets\");
               return \"\";
           }
       }
      
       int arr[] = obtainKeys(key);
      
       StringBuilder build = new StringBuilder();
       int j=0;
       for(int i=0;i<str.length();i++){
           build.append(charRightShift(str.charAt(i),arr[j]));
           j= ++j%arr.length;
       }
      
       return build.toString();
   }
  
   /**
   * Vignere Decoding
   * @param str
   * @param key
   * @return
   */
   public String vignereDecode(String str, String key){
      
       for(char ch:key.toCharArray()){
           if(ch<97 || ch>122){
               System.out.println(\"Error: Keyword contains character other than lowercase alphabets\");
               return \"\";
           }
       }
      
       int arr[] = obtainKeys(key);
      
       StringBuilder build = new StringBuilder();
       int j=0;
       for(int i=0;i<str.length();i++){
           build.append(charLeftShift(str.charAt(i),arr[j]));
           j = ++j%arr.length;
       }
      
       return build.toString();
   }
  
   /**
   * Method to shift lowercase alphabets to left
   * @param ch
   * @param key
   * @return
   */
   public char charLeftShift(char ch, int key){
       return charShift(ch,-key);
   }
  
   /**
   * Method to shift lowercase alphabets to right
   * @param ch
   * @param key
   * @return
   */
   public char charRightShift(char ch, int key){
       return charShift(ch,key);
   }
  
   /**
   * Private Method to shift lowercase alphabets to right or left
   * For left, key is negative
   * For right, key is positive
   * @param ch
   * @param key
   * @return
   */
   private char charShift(char ch,int key){
       if(Math.abs(key)>25){
           System.out.println(\"Error message - key should be between 0 and 25\");
           return (char) 0;
       }
      
       //If char is not a lowercase alphabet, return char
       if(ch<97 || ch>122){
           return ch;
       }
      
       key = (26+key)%26;
      
       return (char) ((ch+key-97)%26+97);
   }
  
  
}

Main.java

public class Main {
  
   public static void main(String args[]){
      
       String str = \"cats and dogs\";
      
       Cipher cipher = new Cipher();
       System.out.println(cipher.cipherEncode(str, 5));
      
       System.out.println(cipher.cipherDecode(cipher.cipherEncode(str, 5), 5));
      
       int arr[] = cipher.obtainKeys(\"hello\");
       for(int i=0;i<arr.length;i++){
           System.out.print(arr[i]+\" \");
       }
      
       System.out.println();
       System.out.println(cipher.vignereEncode(\"elephants and hippos\", \"rats\"));
       System.out.println(cipher.vignereDecode(cipher.vignereEncode(\"elephants and hippos\", \"rats\"), \"rats\"));
   }

}

 Question 2: Cipher (50 points) Caesar\'s cipher is a very well known and simple encryption scheme. The point of an encryption scheme is to transform a message
 Question 2: Cipher (50 points) Caesar\'s cipher is a very well known and simple encryption scheme. The point of an encryption scheme is to transform a message
 Question 2: Cipher (50 points) Caesar\'s cipher is a very well known and simple encryption scheme. The point of an encryption scheme is to transform a message
 Question 2: Cipher (50 points) Caesar\'s cipher is a very well known and simple encryption scheme. The point of an encryption scheme is to transform a message

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site