Question 2 Cipher 50 points Caesars cipher is a very well kn
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\"));
}
}



