问题描述
我正在尝试开发一个编码,解码和打破使用Vigenere密码编码的消息的加密程序。我被卡住的地方是打破[加密]消息(没有钥匙)。我有一个想法如何去做,但我不知道如何编写它。我的想法如下:
该程序将系统地生成潜在的键,长度从1开始,以26的结尾。键将包含英文字母的字母并且不区分大小写。对于每个密钥长度(从1-26的任何位置),密钥将填充字母'a',然后程序将检查它们的密钥是否可以(我有另一种方法)。如果他们的键不正确,则最后一个位置的字母将旋转到字母表中的下一个字母。一旦最后一封信已经通过所有26个可能的位置,第二个到最后一个字母将被旋转,然后最后一个字母和第二个到最后一个字母将相应地旋转,等等(一直回到第一个[潜在]键的字母)。每次生成新密钥时,使用单独的方法检查[潜在]密钥,当找到正确的密钥时该方法停止。关键的创作过程将是这样的:
[从只有1个字母长的键开始]
a
b
c
...
x
y
z
[现在潜在的关键长度变为两个]
aa
ab
ac
ad
...
zw
zx
zy
zz
[最终潜在的密钥长度变为26]
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaab
aaaaaaaaaaaaaaaaaaaaaaaaac
aaaaaaaaaaaaaaaaaaaaaaad
...
zzzzzzzzzzzzzzzzzzzzzzzzzw
zzzzzzzzzzzzzzzzzzzzzzzx
zzzzzzzzzzzzzzzzzzzzzzzzzzzy
zzzzzzzzzzzzzzzzzzzzzzzzzzz
(希望你可以在那里看到模式)
如果任何人有或知道如何执行此操作的代码,或者可以帮助指导会议通过必要的步骤编写代码,这将是非常感谢。
谢谢!
修改(现在我已经数学)
有大约6 * 10 ^ 36种可能的组合,你需要迭代 - 最大的 long 值约为9 * 10 ^ 18 - 一个较小的数字。
说,你会发现一个优化的方法来迭代组合,您可以在其中生成和比较每秒万亿(10 ^ 12)个组合(比您的平均开发人员的机器快得多),您可以在一百万台机器上进行并行化,即(60 * 60 * 24 * 365 * 10 ^ 12)*每年10 ^ 6或约3 *每年检查10 ^ 25个组合。
以极高的速度,仍然需要约1,900亿年才能完成所有组合。 / p>
我强烈要求您调查另一个替代方案来实现您的目标,而不是尝试每个键。
现在回到我原来的答案),如果你有一个合理的大小的密钥子集你想迭代,我可以想象这样做一个直线的数字循环,只是将数字转换为一个修改的基础26的键。
一些伪代码:
public void runAlg orithm(){
for(int i = startKey; I< endKey; i ++){
String key = toBase26(i);
testKey(key);
}
}
public static String toBase26(int number){
number = Math.abs(number);
String converted =;
//重复将数字除以26,并将
//余数转换为适当的字母。
do
{
int remaining = number%26;
converted =(char)(余数+'A')+转换;
number =(number - remaining)/ 26;
} while(number> 0);
返回转换;
}
I am trying to develop a program that encodes, decodes, and breaks the encryption for a message encoded with a Vigenere cipher. Where I am getting stuck is breaking [the encryption on] the message (without the key). I have an idea of how to go about doing it, but I'm not sure how to code it. My idea is a follows:
The program would systematically generate potential keys, with a length starting at 1 and ending at 26. The keys would contain the letters of the English alphabet and are NOT case sensitive. For each key length (anywhere from 1-26), the key would be filled with the letter 'a', and then the program would check to see if they key was corrent (I have another method for that). If they key was incorrect, then the letter in the last position would be rotated to the next letter in the alphabet. Once the last letter had gone through all 26 possible positions, the second to last letter would be rotated, and then the last letter and the second to last letter would rotate accordingly, and so on a so forth (all the way back to the first letter of the [potential] key). Each time a new key is generated, the [potential] key is being checked with a separate method, and the method stops when the correct key is found. The key creation progression would go something like this:
[starting with keys that are only 1 letter long] a b c ... x y z [now the potential key length becomes two] aa ab ac ad ... zw zx zy zz [eventually the potential key length becomes 26] aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaab aaaaaaaaaaaaaaaaaaaaaaaaac aaaaaaaaaaaaaaaaaaaaaaaaad ... zzzzzzzzzzzzzzzzzzzzzzzzzw zzzzzzzzzzzzzzzzzzzzzzzzzx zzzzzzzzzzzzzzzzzzzzzzzzzy zzzzzzzzzzzzzzzzzzzzzzzzzz
(hopefully you can see the pattern there)
If anyone has or knows the code for how to do this, or could help guide met through the steps necessary to code this, it would be much appreciated.
Thanks!
Edit (now that I've done the math)
There are around 6*10^36 possible combinations you'll need to iterate through - the largest long value is about 9*10^18 - a much smaller number.
That being said, say you find an optimized way to iterate over the combinations where you can generate and compare a trillion (10^12) combinations per second (much faster than your average developer's machine), and you can parallelize it across a million machines - that would be (60*60*24*365*10^12)*10^6 per year, or about 3*10^25 combinations checked per year.
At that blazing speed, it would still take ~190 billion years to go over all combinations.
I strongly urge you to investigate another alternative for achieving your goal instead of trying every single key.
Now (back to my original answer), if you had a reasonably sized subset of keys you wanted to iterate over, I could imagine doing something like this with a straight up number loop that just converted the number to a modified base-26 for the key.
Some pseudo-code:
public void runAlgorithm () { for (int i=startKey; i<endKey; i++) { String key = toBase26(i); testKey(key); } }
Use something like this hexavigesimal converter from wikipedia for the impl of toBase26 (copied below):
public static String toBase26(int number){ number = Math.abs(number); String converted = ""; // Repeatedly divide the number by 26 and convert the // remainder into the appropriate letter. do { int remainder = number % 26; converted = (char)(remainder + 'A') + converted; number = (number - remainder) / 26; } while (number > 0); return converted; }
这篇关于Vigenere /多字母密码解密器/ Java中的解密器/断路器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!