I need help ASAP, I am only allowed to use for loops and a detailed guide would
ID: 3783712 • Letter: I
Question
I need help ASAP, I am only allowed to use for loops and a detailed guide would be nice!
Worksheet #3: The Caesar Cipher CSC 142 Computer Programming I Due Wednesday 1/25/2017 Background BCE, was a Roman general and politician. Julius Caesar, who lived from 100 BCE to 44 who played a key role in the downfall of the Roman Republic and the rise of the Roman Empire. As a politician, he was the champion of lower class citizens and the working man. As a general, he was dominant, and is considered one of the greatest military commanders in history. After conquering territory, Caesar returned to Rome and used his army and his popularity to instigate a civil war and take He was eventually assassinated by a group of Roman and his assassination led to a power vacuum that transformed Rome from a republic to an empire. Caesar's military and political activities meant secret codes and ciphers were important for him. However, ciphers A B and codes were uncommon and encoding methods were primitive. Caesar encoded messages by shifting e letter forward or backward a certain number of spaces, and the private or secret key was how many letters to shift. For NAAQ example, if the key were 3, Awould become D, B would become E, C would become F, and so on Imagine two disks, with each letter of the alphabet printed in order on the outside of the disk, with a smaller di sk laid on top of a larger disk. By lining up the letters on the two rings then shifting one disk forward by a certain number of places the Caesar shift cipher is easy to apply. You will be performing this shift using software Assignment You will be implementing a Caesar cipher in Java. You will do this by implementing two methods -one method to encrypt (turn a plain text into a cipher text and one to decrypt (turn a cipher text into a plain text). These methods will need to take a parameter that is the private key The principal operation to implement the Caesar cipher is shifting each character by a certain amount. The same operation will work for both encryption and decryption. Hint: explore the char primitive data type and the Character wrapper class. Questions You will use your program to encrypt 3 plain texts and to decrypt 3 cipher texts To encrypt a plain text, you take each letter, and shift it forward a certain number of letters. That number is your secret key. You will be given the secret key and asked to encrypt the message using that key To decrypt a cipher text, you take each letter, and shift it backward a certain number of letters. That number is the secret key. You will not know the secret key. Think about how you can guess the correct key.Explanation / Answer
We can perform encryption and decryption using the below code. But for a proper encryption, we need to know the exact character set. In this case I have used a character set consisting of ony lower case alphabets. This set should be modified if you want to include uppercase letters or numerals.
public class CeasarCipher {
static char[] charSet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
static char escapeChar = '?';
public static void main(String[] args) {
CeasarCipher obj = new CeasarCipher();
System.out.println("Encrypt first text: ");
System.out.println(obj.encrypt(5, "Those who control the past, control the future. Those who control the present, control the past.".toLowerCase()).replace("?", ""));
System.out.println("Encrypt second text: ");
System.out.println(obj.encrypt(10, "It is poor civic hygiene to install technologies that could someday facilitate a police state.".toLowerCase()).replace("?", ""));
System.out.println("Encrypt third text: ");
System.out.println(obj.encrypt(23, "All Gaul is divided into three parts.".toLowerCase()).replace("?", ""));
System.out.println(" First Text: JNEVFCRNPRSERRQBZVFFYNIRELVTABENAPRVFFGERATGU");
for(int i = 0;i<charSet.length;i++){
System.out.println("key: " + i + " Text: " + obj.decrypt(i, "JNEVFCRNPRSERRQBZVFFYNIRELVTABENAPRVFFGERATGU".toLowerCase()));
}
System.out.println(" Second Text: NYNXNSXZKKNHNJSYYTUWTYJHYTZWXJQAJXBNYMQFBXBJSJJIYTUWTYJHYTZWXJQAJXBNYMRFYMJRFYNHX");
for(int i = 0;i<charSet.length;i++){
System.out.println("key: " + i + " Text: " + obj.decrypt(i, "NYNXNSXZKKNHNJSYYTUWTYJHYTZWXJQAJXBNYMQFBXBJSJJIYTUWTYJHYTZWXJQAJXBNYMRFYMJRFYNHX".toLowerCase()));
}
System.out.println(" Third Text: CHQULYPYHNMIZCGJILNUHWYULYNBYLYMOFNIZNLCPCUFWUOMYM");
for(int i = 0;i<charSet.length;i++){
System.out.println("key: " + i + " Text: " + obj.decrypt(i, "CHQULYPYHNMIZCGJILNUHWYULYNBYLYMOFNIZNLCPCUFWUOMYM".toLowerCase()));
}
}
public String encrypt(int key, String plainText){
if (key<0){
key = Math.abs(charSet.length + key) % charSet.length;
}
if(plainText != null){
char[] charArray = new char[plainText.length()];
char[] plainTextArray = plainText.toCharArray();
for(int i=0;i<plainTextArray.length; i++)
{
int charPosition = -1;
//search character set for the character
for(int index = 0; index < charSet.length ; index++){
if (charSet[index] == plainTextArray[i]){
charPosition = index;
break;
}
}
if(charPosition != -1){
int cipherIndex = (charPosition + key) % charSet.length; // determine new position considering the set in circular form
charArray[i] = charSet[cipherIndex];
} else {
charArray[i] = escapeChar;
}
}
return new String(charArray);
}
return null;
}
public String decrypt(int key, String cipherText){
if (key<0){
key = Math.abs(charSet.length + key) % charSet.length;
}
if(cipherText != null){
char[] charArray = new char[cipherText.length()];
char[] cipherTextArray = cipherText.toCharArray();
for(int i=0;i<cipherTextArray.length; i++)
{
int charPosition = -1;
//search character set for the character
for(int index = 0; index < charSet.length ; index++){
if (charSet[index] == cipherTextArray[i]){
charPosition = index;
break;
}
}
if(charPosition != -1){
int cipherIndex = Math.abs(charPosition - key) % charSet.length; // determine new position considering the set in circular form
charArray[i] = charSet[cipherIndex];
} else {
charArray[i] = escapeChar;
}
}
return new String(charArray);
}
return null;
}
}
2) Decryption of cipher text without key and exact characterset is a very complex part as it cannot be automated without help of advanced AI systems. However, we can try to decrypt simple cipher texts manually using brute force method. For example, in case of cipher text NYNXNSXZKKNHNJSYYTUWTYJHYTZWXJQAJXBNYMQFBXBJSJJIYTUWTYJHYTZWXJQAJXBNYMRFYMJRFYNHX, if we decrypt with key as 5, we will get text as 'itisinsufficienttoprotectourselfeseithlaeseeneedtoprotectourselfeseithmathematics'. For better results, we should more information about character set used.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.