Download presentation
Presentation is loading. Please wait.
1
Automatic Caesar Cipher Breaker
Yonglei Tao
2
Original Text Encrypted Text
In cryptography, a Caesar cipher, also known as a Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 3, A would be replaced by D, B would become E, and so on. The method is named after Julius Caesar, who used it to communicate with his generals. Ze tipgkfxirgyp, r Trvjri tzgyvi, rcjf befne rj r Trvjri'j tzgyvi, kyv jyzwk tzgyvi, Trvjri'j tfuv fi Trvjri jyzwk, zj fev fw kyv jzdgcvjk reu dfjk nzuvcp befne vetipgkzfe kvtyezhlvj. Zk zj r kpgv fw jlsjkzklkzfe tzgyvi ze nyzty vrty cvkkvi ze kyv gcrzekvok zj ivgcrtvu sp r cvkkvi jfdv wzovu eldsvi fw gfjzkzfej ufne kyv rcgyrsvk. Wfi vordgcv, nzky r jyzwk fw 3, R nflcu sv ivgcrtvu sp U, S nflcu svtfdv V, reu jf fe. Kyv dvkyfu zj erdvu rwkvi Alczlj Trvjri, nyf ljvu zk kf tfddleztrkv nzky yzj xvevircj.
3
English Letter Frequencies
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
4
Class CaesarCipherBreaker
public class CaesarCipherBreaker { float[] given, found; public CaesarCipherBreaker( ) { given = new char[26]; found = new char[26]; } public void ReadFrquencies() { … } public void CalcFrequencies(String fileName) { … } public int findKey() { … }
5
Read Frequencies try { Scanner scan = new Scanner(new File(“letterFreq.txt")); double num; int index = 0; while (scan.hasNext()) { scan.next(); if (scan.hasNextDouble()) num = scan.nextDouble(); given[index++] = num; } scan.close(); catch (Exception exception) { System.out.println("Error processing file: " + exception);
6
Calculate Frequencies
public void calcFrequencies (String encryptedFile) { Scanner scan = new Scanner(new File(encryptedFile)); int[] counters = new int[26]; for (char ch: counters) ch = 0; while ( scan.hasNextLine() ) { String line = scan.nextLine(); char[] list = line.toCharArray(); for (char next: list) if ( Character.isLetter(next) ) counters[Character.toUpperCase(next) - 'A']++; } int total = 0; for (int i=0; i<26; i++) total += counters[i]; for (int j=0; j<26; j++) found[j] = counters[j] / (double)total;
7
Find the Key How to compare two lists of letter frequencies to find the best match? A least squares fit Measur the difference between the two lists by calculating the sum of the squares of the differences of the corresponding elements in the lists Minimize this sum by trying different rotations (or different keys) Identify the number of rotations with the smallest sum, which is the key
8
Find the Key (Cont.) int findKey ( ) { int shift, i, best=-1; float diff, min=1, sub; for (shift=0; shift < 26; shift++) { diff = 0; for (i = 0; i <= 25; i++) { sub = given[ (i + shift)%26] – found[i]; diff += sub * sub; } if (diff < min) { min = diff; best = shift; return best;
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.