【发布时间】:2014-06-24 14:36:55
【问题描述】:
描述:我想想出一个程序来破解使用凯撒密码算法的加密消息。听起来很简单,问题是您必须弄清楚用于制作加密消息的位置才能解密编码消息。
所以我有一个名为public void train(String trainingFileName) 的方法,它读取一个包含大量文本的文件,以确定存储在double array [] 中的每个lowercase alphabetical English character (a - z) 的频率。该方法效果很好,因此除非您愿意,否则无需查看,但我确实在我遇到问题的方法中使用了大部分代码,即我的 public int decrypt(String cipherTextFileName, String outputFileName) 方法。
当 Caesar Cipher 设置为 3 个位置时,代码运行良好,但其他任何事情都会给我带来很多问题。
我的public int decrypt(String cipherTextFileName, String outputFileName) 方法中有一个do-while 循环,它将解密从0 位置开始的编码消息,然后使用我正在使用的“距离”公式(注意:我不能使用任何其他公式)来找到最小值我的加密消息中knownFrequencies 和observedFreq 之间的距离。现在我将我的do-while 循环设置为如果distance 是less than 0.6 然后停止循环。理论上,当我在凯撒密码中拥有正确数量的位置时,距离应该低于该值。
问题:当numberOfPositions 为 3 时,程序运行良好,但是当我使用未在凯撒密码中使用 3 个位置的加密消息时,distance 永远不会低于 1,并且在我设置 @ 时处于调试模式987654335@应该是什么解密消息,消息仍然是加密的。
问题:如何更好地实施此方法,这样我就不会在“硬”值上测试我的distance 以停止do-while 循环?我尝试使用Math.min(),但这不起作用。为什么凯撒密码的位置不是 3 时,我无法解码消息。
我现在将向您展示我的代码。如果你想在你的系统上测试它。您将需要 3 个文本文件。一个文件必须很长,其中包含一堆单词……至少 1000 个。该文件将在 train 方法中读取。您需要一个包含加密消息的文件和另一个文件供程序写入解密消息。
这是一条加密消息,首先使用 3 个位置的凯撒密码,然后使用 5 个位置。
Wkh surjudp zdv krvwhg eb dfwru Slhufh Eurvqdq dqg kdg frpphqwdub iurp pdqb Kroobzrrg dfwruv dqg iloppdnhuv Prylh txrwdwlrqv wkdw ylhzhuv xvh lq wkhlu rzq olyhv dqg vlwxdwlrqv
Ymj uwtlwfr bfx mtxyji gd fhytw Unjwhj Gwtxsfs fsi mfi htrrjsyfwd kwtr rfsd Mtqqdbtti fhytwx fsi knqrrfpjwx Rtanj vztyfyntsx ymfy anjbjwx zxj ns ymjnw tbs qnajx fsi xnyzfyntsx
解密时应该说:
The program was hosted by actor Pierce Brosnan and had commentary from many Hollywood actors and filmmakers Movie quotations that viewers use in their own lives and situations
这是我写的课程(你需要所有的导入),我要感谢任何提前提供帮助的人:
public class CodeBreaker {
public final int NUMBER_OF_LETTERS = 26;
private double[] knownFrequencies = new double[NUMBER_OF_LETTERS];
public double[] getKnownFrequencies() {
return knownFrequencies;
}
public void setKnownFrequencies(double[] knownFrequencies) {
this.knownFrequencies = knownFrequencies;
}
/**
* Method reads in a file with a lot of text in it and
* then use that to figure out the frequencies of each character
*
* @param trainingFileName
*/
public void train(String trainingFileName) {
try {
Scanner fileIO = new Scanner(new File(trainingFileName));
int total = 0;
String temp = "";
while (fileIO.hasNext()) {
//reading into file and storing it into a string called temp
temp += fileIO.next().toLowerCase().replaceAll("[ -,!?';:.]+", "");
//converting temp string into a char array
}
char[] c = temp.toCharArray();
total += c.length; // how many characters are in text
int k = (int) 'a'; // int value of lowercase letter 'a'
int[] counter = new int[NUMBER_OF_LETTERS];
for (int j = 0; j < total; j++) {
for (int i = k - k; i < knownFrequencies.length; i++) {
char[] d = new char[knownFrequencies.length];
d[i] = (char) (k + i);
if (c[j] == d[i]) {//checking to see if char in text equals char in d array
counter[i]++;
knownFrequencies[i] = (double) counter[i] / total;
}
}
}
fileIO.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.err.println(e);
System.exit(0);
}
}
/**
* Main decryption method used to take coded text from a file, figure out the positions in the CaesarCipher
* and then decode it onto another file.
*
* @param cipherTextFileName
* @param outputFileName
* @return
*/
public int decrypt(String cipherTextFileName, String outputFileName) {
Scanner fileIO;
int numberOfPositions = 0;
double distance = 0.000000;
try {
fileIO = new Scanner(new File(cipherTextFileName));
PrintWriter writer = new PrintWriter(new File(outputFileName));
String temp = "";
while (fileIO.hasNext()) {
//reading into file and storing it into a string called temp
temp += fileIO.next().toLowerCase().replaceAll(" ", "");
}
fileIO.close();
do {
distance = 0.0;
int total = 0;
double[] observedFreq = new double[NUMBER_OF_LETTERS];
temp = decrypt(temp, numberOfPositions);
char[] c = temp.toCharArray(); //store decrypted chars into an array
total += c.length; // how many characters are in text
int k = (int) 'a'; // int value of lowercase letter 'a'
int[] counter = new int[NUMBER_OF_LETTERS]; //use to count the number of characters in text
for (int j = 0; j < total; j++) {
for (int i = k - k; i < observedFreq.length; i++) {
char[] d = new char[observedFreq.length];
d[i] = (char) (k + i);
if (c[j] == d[i]) { //checking to see if char in text equals char in d array
counter[i]++;
observedFreq[i] = (double) counter[i] / total;
}
}
}
//Formula for finding distance that will determine the numberOfPositions in CaesarCipher
for (int j = 0; j < knownFrequencies.length; j++) {
distance += Math.abs(knownFrequencies[j] - observedFreq[j]); //This is the part of the code I am having trouble with
}
numberOfPositions = numberOfPositions + 1;
} while (distance > 0.6); //This is the part of the code I am having trouble with
Scanner fileIO2 = new Scanner(new File(cipherTextFileName));
while (fileIO2.hasNextLine()) {
//reading into file and storing it into a string called temp
temp = fileIO2.nextLine();
writer.println(decrypt(temp, numberOfPositions));
}
writer.close();
fileIO2.close();
System.out.println(distance);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.err.println(e);
System.exit(0);
}
return numberOfPositions;
}
/**
* CaesarCipher decrypt and encrypt methods
*
* @param ciphertext
* @param numberOfPositions
* @return
*/
public String decrypt(String ciphertext, int numberOfPositions) {
// TODO Auto-generated method stub
return encrypt(ciphertext, -numberOfPositions);
}
public String encrypt(String msg, int offset) {
offset = offset % 26 + 26;
StringBuilder encoded = new StringBuilder();
for (char i : msg.toCharArray()) {
if (Character.isLowerCase(i)) {
int j = (i - 'a' + offset) % 26;
encoded.append((char) (j + 'a'));
} else if (Character.isUpperCase(i)) {
int h = (i - 'A' + offset) % 26;
encoded.append((char) (h + 'A'));
} else {
encoded.append(i);
}
}
return encoded.toString();
}
// barebones main method to test your code
public static void main(String[] args) {
// args[0] contains the filename of the training file
// args[1] contains the filename of the cipher text file
// args[2] contains the filename of the output file
CodeBreaker cb = new CodeBreaker();
cb.train(args[0]);
System.out.println(cb.decrypt(args[1], args[2]));
}
}
【问题讨论】:
-
我可以告诉你,你已经在你的帖子中付出了努力,这很好,但你的问题可能会被认为太冗长。代码和解释的每一部分都需要了解您的问题吗?尝试删除不相关的内容,这样人们就可以消化不那么不相关的信息。
-
阅读频率分析。英文文本中最常见的字母是
e,因此凯撒密码英文文本中最常见的字母很可能是'e'+n,其中n是关键。 -
您可能会发现此代码很有用:pajhome.org.uk/crypt/vigenere.html
-
@Jeroen Vannevel 我担心这个问题有点冗长,但看起来很复杂,所以我想确保人们准确地理解我想要做的事情,而不是发布一些代码并说“在这里解决这个问题”没有解释。
标签: java encryption