public class Brutus {
static String alphabet = "abcdefghijklmnopqrstuvwxyz"; // Use a string - same as iterating through an array using .charAt().
// Initialise alphabet outside of methods but inside class so it can be used my multiple methods.
/* Takes the encrypted string given to it, loops through each possible shift value and utilises other methods
to get a final score back and it keeps track of the best score and its corresponding shift value. */
public static void main(String[] args) {
String encryptedString = args[0];
String decryptedString = encryptedString;
double decryptLowestScore = 10000.0; // flag set as a high value to be beaten.
int decryptShiftValue = 0; // flag to remember the best solution.
int i;
// loop through each shift value from 0-26 (anything outside that range is modulated to be in that range).
for(i = 0; i <=26; i++) {
// send encrypted text to be shifted
String decryptTest = Caesar.rotate(i, encryptedString);
// needs to get the parameter that will be sent to chiSquared method.
double[] freqValues = frequency(decryptTest);
// call chiSquared method with attained parameter above and english array.
double decryptScore = chiSquared(english, freqValues);
if (decryptScore < decryptLowestScore){ // if there is a better score:
decryptLowestScore = decryptScore; // set the better score as current score.
decryptShiftValue = i; // and set the decrypt shift value to i as this is current best decryption key.
}
}
decryptedString = Caesar.rotate(decryptShiftValue, encryptedString); //finally call Caesar again to undo the shift with the key.
System.out.println(decryptedString); // Output the decrypted message to the user.
}
// Contains the frequency for each letter in the english language.
public static final double[] english = {
0.0855, 0.0160, 0.0316, 0.0387, 0.1210, 0.0218, 0.0209, 0.0496, 0.0733,
0.0022, 0.0081, 0.0421, 0.0253, 0.0717, 0.0747, 0.0207, 0.0010, 0.0633,
0.0673, 0.0894, 0.0268, 0.0106, 0.0183, 0.0019, 0.0172, 0.0011
};
// I have done the exact same thing in frequency and just changed the output - do not need or use this.
public static int[] count(String text) {
int[] lettersCount = new int[26];
text = text.toLowerCase();
int i;
int j;
for (i = 0; i < alphabet.length(); i++) { // loops through each char in alphabet.
for (j = 0; j < text.length(); j++) { // loops through each letter in the given text.
if (alphabet.charAt(i) == text.charAt(j)) { // checks to see if the letter from first loop is the letter at position j.
lettersCount[i]++; // if it is, increments the element in the array by 1. alphabet and letters count have equal length.
}
else {
lettersCount[i] = lettersCount[i] + 0;
}
}
}
return lettersCount;
}
/* Gets the amount of each letter of the english alphabet in the string given to it and divides it by the length
of the string given to it in order to calculate the letters frequency in the text. */
public static double[] frequency(String text){
double[] letterFrequency = new double[alphabet.length()];
// First I want to strip the text of any punctuation and spacing so I can get the number of letters in the string.
// to do this i'm going to implement a regex that replaces anything not within a specified range with "".
String strippedText = text.replaceAll("[^A-Za-z]+", "").toLowerCase(); // also make everything lower case.
int i;
int j;
for(i = 0; i < alphabet.length(); i++){ // loops through each value of the alphabet.
int count = 0; // resets count to 0 after for loop below is done.
for (j = 0; j < text.length(); j++){ // loops through each character in the text.
if(alphabet.charAt(i) == text.charAt(j)){
count++;
}
}
double letterfreq = ((double) count / text.length()); // cast count to a double to stop the loss of digits after decimal point to truncation via integer division.
letterFrequency[i] += (letterfreq); // add the frequency to the array.
}
return(letterFrequency);
}
/* Implements the Chi Squared formula to get a total chi score for the string
by summing the chi score of each letter.*/
public static double chiSquared(double[] englishFreq, double[] textFreq) {
double chiScore = 0;
//outer for loop that will run through each letter:
for (int l = 0; l < textFreq.length; l++){
chiScore += (Math.pow((textFreq[l]-englishFreq[l]), 2) / englishFreq[l]); // chiSquared formula applied.
// chi score is summed for each letter.
}
return chiScore;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
CaesarCipher:适用于Comp122的CaeserCipher Java任务
共16个文件
xml:6个
java:2个
class:2个
需积分: 9 1 下载量 78 浏览量
2021-03-05
02:30:05
上传
评论
收藏 11KB ZIP 举报
温馨提示
CaesarCipher:适用于Comp122的CaeserCipher Java任务
资源详情
资源评论
资源推荐
收起资源包目录
CaesarCipher-master.zip (16个子文件)
CaesarCipher-master
src
Caesar.java 3KB
Brutus.java 5KB
CaesarCipher.iml 419B
Brutus.class 2KB
.idea
.gitignore 176B
misc.xml 264B
vcs.xml 183B
modules.xml 264B
Caesar.class 1KB
CaesarCipherRemote.iml 423B
.DS_Store 6KB
.idea
.gitignore 176B
misc.xml 256B
vcs.xml 167B
.name 11B
modules.xml 276B
共 16 条
- 1
向着程序媛生长的
- 粉丝: 23
- 资源: 4593
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0