Question: We want to create a program to solve cryptograms. A cryptogram is a word puzzle where the letters have been changed through a substitution cipher.
We want to create a program to solve cryptograms. A cryptogram is a word puzzle where the letters have been changed through a "substitution cipher." For instance, all the A's might be K's. And all the B's might be G's. And so on.
This encryption fails because you can count the frequency of the letters in the cipher and compare that to the frequency of the letters in english. Then create a "map" of the most common letters in the cipher to the most common letters in english.
Just using that technique, you can get pretty close to solving it. However, the map may be off. So also want to implement the ability to swap the map around.
Implementation
Read Charles Dickens' Bleak House to calcluate the letter frequency for english. (the program reads, not you...)
Read the cipher text and generate the letter frequency for the cryptogram.
Create a key of letters that is sorted from most frequent to least frequent for both the book and cipher frequencies.
Create a map of the cipher characters to book characters in sorted order.
Decipher the text based off of the map created.
Edit the map to reflect any small inaccuracies encountered.
-Fields
book is a File to hold the reference to the Bleak House text
bookKey is an array of characters containing lowercase letters of the alphabet in sorted order (sorted by the frequency of appearance).
bookFrequency is an array of ints containing how many times the character has appeared.
The index of the array represents the letter. Where 0 is a, 1 is b, ... 25 is z.
The value of the array at the index is how many times that letter has appeared.
cipher is a File to hold the reference to the cipher text
cipherKey is an array of characters containing lowercase letters of the alphabet in sorted order (sorted by the frequency of appearance).
cipherFrequency is an array of ints containing how many times the character has appeared.
The index of the array represents the letter. Where 0 is a, 1 is b, ... 25 is z.
The value of the array at the index is how many times that letter has appeared.
map is an array of characters that maps the cipher keys to the book keys.
The index of the array represents letters from the cipher text. Where 0 is a, 1 is b, ... 25 is z.
The value of the array represents the letters from the book.
MethodsCryptogramSolver
Sets up our cryptogram solver
Takes a book File and cipher File as parameters
Initialize all the arrays with default values
The keys and map arrays should have a-z as their default values.
Call calculateFrequency and generateKey for the Book file.
Call calculateFrequency and generateKey for the Cipher file.
Call generateMap with the two keys.
calculateFrequency
Reads a file and calculates the frequency of how many times each letter appears in the file. Ignores any other character.
Receives a file to read and a int array to fill with the frequency.
indexOfSmallest
Returns the index of the smallest value in the array.
Receives an array to search through and a length to search up to.
generateKey
The key is an array of characters ordered by the most frequent appearing characters.
Use the frequency array passed to figure out the order of the key.
generateMap
Creates a map from one character to another.
editMap
Modifies the map by changing the "out" character of the map.
decipher
Prints out the deciphered message by using the map created.
You can use this main method for testing.
public static void main(String[] args)
{ CryptogramSolver cs = new CryptogramSolver(new File("BleakHouse.txt"), new File("cipher.txt"));
cs.displayMap();
cs.decipher();
cs.editMap('k', 'p');
cs.editMap('q', 'x');
cs.displayMap();
cs.decipher(); }
IZXY TK YMJ HMFXJ - GD WFD BDQNJ MZGGFWI XT, N HTRJ ITBS KWTR TPQFMTRF BNYM F UNXYTQ NS RD GTTY F UFNW TK INHJ, F IJHP TK HFWIX, F GNGQJ NS RD XZNY N HTRJ MJWJ FX YMJ 'HFZXJ TK YJFWX, N'R OZXY F HWDNSL XMFRJ XJAJS XYZI TW JYJWSFQ GQTTI, N'R QTTPNS' KTW F LFRJ N ITZGQJ HWTXXJI YMJ XYFYJ TK YJCFX FSI YMJD LNAJ RJ F QNYYQJ YNRJ N YFZLMY RDXJQK YT ITZGQJ HZY YMJ HFWIX FSI MTQI XHWNUYZWJX NS RD RNSI N QJFWSJI YT QTAJ YMJ YZRGQNS' INHJ FSI YT GJQNJAJ YMJ BTWI YTRGXYTSJX TW WTQQNSL GTSJX, GJFYX FSDYMNSL N JAJW MJFWI UFYNJSHJ NX F ANWYZJ YMFY N ITS'Y UTXXJXX FSI N HFS'Y IJSD YMFY MJFAJS QNJX GJSJFYM F HTYYTS IWJXX MTB XRFQQ F UFWY TK YNRJ BJ XMFWJ 'YNQQ BJ MJFW YMJ XTZSI TK BNSLX N'R QTXY NS YMJ IZXY TK YMJ HMFXJ YMFY RD QNKJ GWNSLX N MFAJ BFQPJI YMWTZLM LTI'X LWJJS UFXYZWJX GJSJFYM YMJXJ WNHM GQZJ XPNJX N MFAJ XJJS YMJ KFQQ TK RFS FSI YMJ PNSLITR MNIIJS KWTR YMJNW JDJX N MFAJ MJFWI YMJ WTFW TK YMZSIJW FSI N'AJ KJQY YMJ QNLMYJSNSL GTQY FSI BMJS N BFQP YMWTZLM YMJ AFQQJD TK YMJ XMFITB TK IJFYM N YFPJ FQTSL XFRZJQ HTQY JAJWD SNLMY N PNXX YMJ HFWIX FSI N MTQI YMJR YT RD GWJFXY FSI BMJS N XJJ YMJ PNSL TK MJFWYX N PSTB YMFY N FR GQJXXJI FSI YMTZLM RD JDJX FWJ GQNSI XTRJYNRJX, N PSTB YMJWJ'X XTRJYMNSL YMJWJ XT BMJS YMJ YNRJX FY MFSI FSI N PNQQ F RFS, N, N XFD F QNYYQJ UWFDJW XT N HTRJ ITBS KWTR TPQFMTRF BNYM F UNXYTQ NS RD GTTY F UFNW TK INHJ, F IJHP TK HFWIX, F GNGQJ NS RD XZNY MTB XRFQQ F UFWY TK YNRJ BJ XMFWJ YNQQ BJ MJFW YMJ XTZSI TK BNSLX N'R QTXY NS YMJ IZXY TK YMJ HMFXJ YMFY RD QNKJ GWNSLX MTB XRFQQ F UFWY TK YNRJ BJ XMFWJ YNQQ BJ MJFW YMJ XTZSI TK BNSLX N'R QTXY NS YMJ IZXY TK YMJ HMFXJ YMFY RD QNKJ GWNSLX TTTTTTTTTSSSSSSSSSSSSSSSSSSSXWWWWWIIIIII ZZZZZZZZZZZZZZZZZZZZZOOOCCDDDDDDDDDDDDDDDHHHHH VVVEEHHHHHHBBBBBBBLLLLLLLLPPPPPPPPPPPPPP
Cipher -> Book a -> v b -> w c -> q d -> y e -> z f -> a g -> b h -> c i -> d j -> e k -> f l -> g m -> h n -> i o -> j p -> p q -> l r -> m s -> n t -> o u -> k v -> x w -> r x -> s y -> t z -> u
dust of the chase - by ray wylie hubbard so, i come down from oplahoma with a kistol in my boot a kair of dice, a decp of cards, a bible in my suit i come here as the 'cause of tears, i'm just a crying shame seven stud or eternal blood, i'm loopin' for a game i double crossed the state of teqas and they give me a little time i taught myself to double cut the cards and hold scriktures in my mind i learned to love the tumblin' dice and to believe the word tombstones or rolling bones, beats anything i ever heard katience is a virtue that i don't kossess and i can't deny that heaven lies beneath a cotton dress how small a kart of time we share 'till we hear the sound of wings i'm lost in the dust of the chase that my life brings i have walped through god's green kastures beneath these rich blue spies i have seen the fall of man and the pingdom hidden from their eyes i have heard the roar of thunder and i've felt the lightening bolt and when i walp through the valley of the shadow of death i tape along samuel colt every night i pizz(ss) the cards and i hold them to my breast and when i see the ping of hearts i pnow that i am blessed and though my eyes are blind sometimes, i pnow there's something there so when the times at hand and i pill a man, i, i say a little krayer so i come down from oplahoma with a kistol in my boot a kair of dice, a decp of cards, a bible in my suit how small a kart of time we share till we hear the sound of wings i'm lost in the dust of the chase that my life brings how small a kart of time we share till we hear the sound of wings i'm lost in the dust of the chase that my life brings ooooooooonnnnnnnnnnnnnnnnnnnsrrrrrdddddd uuuuuuuuuuuuuuuuuuuuujjjqqyyyyyyyyyyyyyyyccccc xxxzzccccccwwwwwwwggggggggpppppppppppppp
Cipher -> Book a -> v b -> w c -> x d -> y e -> z f -> a g -> b h -> c i -> d j -> e k -> f l -> g m -> h n -> i o -> j p -> k q -> l r -> m s -> n t -> o u -> p v -> q w -> r x -> s y -> t z -> u
dust of the chase - by ray wylie hubbard so, i come down from oklahoma with a pistol in my boot a pair of dice, a deck of cards, a bible in my suit i come here as the 'cause of tears, i'm just a crying shame seven stud or eternal blood, i'm lookin' for a game i double crossed the state of texas and they give me a little time i taught myself to double cut the cards and hold scriptures in my mind i learned to love the tumblin' dice and to believe the word tombstones or rolling bones, beats anything i ever heard patience is a virtue that i don't possess and i can't deny that heaven lies beneath a cotton dress how small a part of time we share 'till we hear the sound of wings i'm lost in the dust of the chase that my life brings i have walked through god's green pastures beneath these rich blue skies i have seen the fall of man and the kingdom hidden from their eyes i have heard the roar of thunder and i've felt the lightening bolt and when i walk through the valley of the shadow of death i take along samuel colt every night i kiss the cards and i hold them to my breast and when i see the king of hearts i know that i am blessed and though my eyes are blind sometimes, i know there's something there so when the times at hand and i kill a man, i, i say a little prayer so i come down from oklahoma with a pistol in my boot a pair of dice, a deck of cards, a bible in my suit how small a part of time we share till we hear the sound of wings i'm lost in the dust of the chase that my life brings how small a part of time we share till we hear the sound of wings i'm lost in the dust of the chase that my life brings ooooooooonnnnnnnnnnnnnnnnnnnsrrrrrdddddd uuuuuuuuuuuuuuuuuuuuujjjxxyyyyyyyyyyyyyyyccccc qqqzzccccccwwwwwwwggggggggkkkkkkkkkkkkkk
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
