这里是一个加密的消息,首先使用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
解密时应该说:该节目由演员皮尔斯·布鲁斯南主持,有许多好莱坞演员和电影制作人的评论,观众在自己的生活和处境中使用的电影语录
好的,这是我写的类(您将需要所有的导入),我想感谢任何提前帮助的人:
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]));
}
}
解码凯撒密码的标准方法被称为“顺着字母表跑下来”。本质上是一种强力解决方案;你尝试了所有的可能性。因为只有26个可能的键,所以没有那么难。
举你的例子:
WKH SURJUDP ZDV KRVWHG ...
wkh surjudp zdv krvwhg ...
xli tvskveq aew lswxih ...
ymj uwtlwfr bfx mtxyji ...
znk vxumxgs cgy nuyzkj ...
aol wyvnyht dhz ovzalk ...
bpm xzwoziu eia pwabml ...
cqn yaxpajv fjb qxbcnm ...
dro zbyqbkw gkc rycdon ...
esp aczrclx hld szdepo ...
ftq bdasdmy ime taefqp ...
gur cebtenz jnf ubfgrq ...
hvs dfcufoa kog vcghsr ...
iwt egdvgpb lph wdhits ...
jxu fhewhqc mqi xeijut ...
kyv gifxird nrj yfjkvu ...
lzw hjgyjse osk zgklwv ...
max ikhzktf ptl ahlmxw ...
nby jlialug qum bimnyx ...
ocz kmjbmvh rvn cjnozy ...
pda lnkcnwi swo dkopaz ...
qeb moldoxj txp elpqba ...
rfc npmepyk uyq fmqrcb ...
sgd oqnfqzl vzr gnrsdc ...
the program was hosted ...
uif qsphsbn xbt iptufe ...
vjg rtqitco ycu jquvgf ...
wkh surjudp zdv krvwhg ...
对于一个人来说,只要26条线就能找到正确的线是很简单的。对于计算机来说,这就更难了。你数字母频率的主意很好。您还可以标记字母对,如“qx”,标记字母对,如“th”。计算所有26个可能结果的得分,并选择最高的得分结果。只要你调整好了你的得分方法,那么你就有很好的机会找到正确的解决方案。
问题内容: 我通过java进行了凯撒密码,它可以运行,但是在用户输入密钥后不对任何内容进行加密! 这是我的代码 跑: 问题答案: 使用效率不是很高…您可以对值进行整数运算以获得其索引。 我在代码中添加了注释以解释更多信息,但这就是我想出的。 输出应该是这样的
我需要编写一个python、hacker和wow这些单词的加密文本,并且使用不包括使用RAW_INPUT的python中的凯撒密码,距离为3。这是我到目前为止,但我不断得到一个错误消息,我不知道如何修复它。
我想知道如何限制加密的ASCII范围从32-126。 这个是用来解密的 我的加密工作得很好(当我引用解密函数时),但我的解密并不像它应该的那样工作。 额外注意:我们只需要在编码时进行右移,给出一个字符串供整个程序加密和解密(“这是C++”) 注意:在添加更多代码描述的过程中
首先,我是编程的新手,所以要温柔。而且,我一直在做这个任务,但没有任何结果。任务是创建一个凯撒密码程序,它加密或解密一个段落最多100个字符。实际上是两个独立的实验室。第一个实验是加密,然后第二个实验是解密。一旦我弄清楚了如何创建加密程序,解密程序应该很简单,因为我可以只进行语义更改来解密而不是加密。无论如何,这是我到目前为止的代码。它通过了他们给我们的5个测试中的4个,但出于某种原因,有一个测试
所以,我试图想出一种方法来对凯撒密码进行加密/解密(在这里,你输入一个值,然后将字母沿着字母表移动很多次,例如,如果密钥是3,那么a将变成d,b将变成e,c将变成f,以此类推),但我需要使用递归,而不是迭代。到目前为止,我只对最后一个字符进行了加密,并输出“encrypted-w”,这对我来说是没有意义的。