Ceasar Cipher - Java Details of the program You need to do the following tasks:
ID: 3661515 • Letter: C
Question
Ceasar Cipher - Java
Details of the program
You need to do the following tasks:
1. Implement encryption/decryption functions that take a key (as an integer in 0, 1, 2,..., 25) and a string. The function should only operate on the characters `a', `b', ..., `z' (both upper and lower case), and it should leave any other characters, unchanged.
2. Implement a function that performs a brute force attack on a ciphertext, it should print a list of the keys and associated decryptions. It should also take an optional parameter that takes a substring and only prints out potential plaintexts that contain that decryption.
3. Show the outputof your encrypt function on the following (key, plaintext) pairs:
k = 6, plaintext = "Get me a vanilla ice cream, make it a double."
k = 15, plaintext = "I don't much care for Leonard Cohen."
k = 16, plaintext = "I like root beer oats."
4. Show the output of your decrypt function on the following (key, ciphertext) pairs:
k = 12, ciphertext = "NDUZS FTQ BUZQ OAZQE."
k = 3, ciphertext = "FDHVDU QHHGV WR ORVH ZHLJKW."
k = 20, ciphertext = "UFGIHXM ULY NUMNYS."
5. Show the output of your attack function on the following ciphertexts, if an optional keyword is specied, pass that to your attack function:
ciphertext = "GRYY GURZ GB TB GB NZOEBFR PUNCRY." keyword = "chapel"
ciphertext = "WZIV KYV JYFK NYVE KYV TPDSRCJ TIRJY." keyword = "cymbal"
ciphertext = "BAEEQ KLWOSJL OSK S ESF OZG CFWO LGG EMUZ." no keyword
Explanation / Answer
Cipher.java
public class Cipher
{
public static void main(String[] args)
{
String result="";
String plain1="Get me a vanilla ice cream, make it a double.";
int pkey1=6;
result=encrypt(plain1,pkey1);
System.out.println(result);
String plain2="I don't much care for Leonard Cohen.";
int pkey2=15;
result=encrypt(plain2,pkey2);
System.out.println(result);
String plain3="I like root beer oats.";
int pkey3=16;
result=encrypt(plain3,pkey3);
System.out.println(result);
System.out.println();
String cipher1="NDUZS FTQ BUZQ OAZQE.";
int ckey1=12;
result=decrypt(cipher1,ckey1);
System.out.println(result);
String cipher2="FDHVDU QHHGV WR ORVH ZHLJKW.";
int ckey2=3;
result=decrypt(cipher2,ckey2);
System.out.println(result);
String cipher3="UFGIHXM ULY NUMNYS.";
int ckey3=20;
result=decrypt(cipher3,ckey3);
System.out.println(result);
System.out.println();
String bruteCipher1="GRYY GURZ GB TB GB NZOEBFR PUNCRY.";
String keyWord1="chapel";
bruteForce(bruteCipher1,keyWord1);
String bruteCipher2="WZIV KYV JYFK NYVE KYV TPDSRCJ TIRJY.";
String keyWord2="cymbal";
bruteForce(bruteCipher2,keyWord2);
String bruteCipher3="BAEEQ KLWOSJL OSK S ESF OZG CFWO LGG EMUZ.";
bruteForce(bruteCipher3);
}
//For encrypting the plain text
public static String encrypt(String plainText, int key)
{
if (key == 0)
{
return plainText;
}
String cipherText = "";
for (int i = 0; i < plainText.length(); ++i)
{
int old = (int) plainText.charAt(i);
if ((old >= 65 && old <= 90))
{
int newVal = (((old - 65) + key) % 26) + 65;
cipherText += (char) newVal;
}
else if (old >= 97 && old <= 122)
{
int newVal = (((old - 97) + key) % 26) + 97;
cipherText += (char) newVal;
}
else
{
cipherText += plainText.charAt(i);
}
}
return cipherText;
}
//For decrypting the ciphers
public static String decrypt(String cipherText, int key)
{
String plainText = "";
if (key == 0)
{
return cipherText;
}
for (int i = 0; i < cipherText.length(); ++i)
{
int old = (int) cipherText.charAt(i);
if ((old >= 65 && old <= 90))
{
int newVal = (((old - 65) - key) % 26);
if(newVal<0)
{
newVal+=26;
}
plainText += (char) (newVal+65);
}
else if (old >= 97 && old <= 122)
{
int newVal = (((old - 97) - key) % 26);
if(newVal<0)
{
newVal+=26;
}
plainText += (char) (newVal+97);
}
else
{
plainText += cipherText.charAt(i);
}
}
return plainText;
}
public static void bruteForce(String cipherText, String... keyword)
{
if(keyword.length==0)
{
for(int key=0;key<=25;key++)
{
String plainText=decrypt(cipherText,key);
System.out.println("Key is "+key);
System.out.println(plainText);
}
}
else if(keyword.length==1)
{
for(int key=0;key<=25;key++)
{
String plainText=decrypt(cipherText,key);
if(plainText.toLowerCase().contains(keyword[0].toLowerCase()))
{
System.out.println("Key is "+key);
System.out.println(plainText);
}
}
System.out.println();
}
}
}
Output:
Mkz sk g bgtorrg oik ixkgs, sgqk oz g juahrk.
X sdc'i bjrw rpgt udg Atdcpgs Rdwtc.
Y byau heej ruuh eqji.
BRING THE PINE CONES.
CAESAR NEEDS TO LOSE WEIGHT.
ALMONDS ARE TASTEY.
Key is 13
TELL THEM TO GO TO AMBROSE CHAPEL.
Key is 17
FIRE THE SHOT WHEN THE CYMBALS CRASH.
Key is 0
BAEEQ KLWOSJL OSK S ESF OZG CFWO LGG EMUZ.
Key is 1
AZDDP JKVNRIK NRJ R DRE NYF BEVN KFF DLTY.
Key is 2
ZYCCO IJUMQHJ MQI Q CQD MXE ADUM JEE CKSX.
Key is 3
YXBBN HITLPGI LPH P BPC LWD ZCTL IDD BJRW.
Key is 4
XWAAM GHSKOFH KOG O AOB KVC YBSK HCC AIQV.
Key is 5
WVZZL FGRJNEG JNF N ZNA JUB XARJ GBB ZHPU.
Key is 6
VUYYK EFQIMDF IME M YMZ ITA WZQI FAA YGOT.
Key is 7
UTXXJ DEPHLCE HLD L XLY HSZ VYPH EZZ XFNS.
Key is 8
TSWWI CDOGKBD GKC K WKX GRY UXOG DYY WEMR.
Key is 9
SRVVH BCNFJAC FJB J VJW FQX TWNF CXX VDLQ.
Key is 10
RQUUG ABMEIZB EIA I UIV EPW SVME BWW UCKP.
Key is 11
QPTTF ZALDHYA DHZ H THU DOV RULD AVV TBJO.
Key is 12
POSSE YZKCGXZ CGY G SGT CNU QTKC ZUU SAIN.
Key is 13
ONRRD XYJBFWY BFX F RFS BMT PSJB YTT RZHM.
Key is 14
NMQQC WXIAEVX AEW E QER ALS ORIA XSS QYGL.
Key is 15
MLPPB VWHZDUW ZDV D PDQ ZKR NQHZ WRR PXFK.
Key is 16
LKOOA UVGYCTV YCU C OCP YJQ MPGY VQQ OWEJ.
Key is 17
KJNNZ TUFXBSU XBT B NBO XIP LOFX UPP NVDI.
Key is 18
JIMMY STEWART WAS A MAN WHO KNEW TOO MUCH.
Key is 19
IHLLX RSDVZQS VZR Z LZM VGN JMDV SNN LTBG.
Key is 20
HGKKW QRCUYPR UYQ Y KYL UFM ILCU RMM KSAF.
Key is 21
GFJJV PQBTXOQ TXP X JXK TEL HKBT QLL JRZE.
Key is 22
FEIIU OPASWNP SWO W IWJ SDK GJAS PKK IQYD.
Key is 23
EDHHT NOZRVMO RVN V HVI RCJ FIZR OJJ HPXC.
Key is 24
DCGGS MNYQULN QUM U GUH QBI EHYQ NII GOWB.
Key is 25
CBFFR LMXPTKM PTL T FTG PAH DGXP MHH FNVA
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.