问题描述
我正在尝试开发一个编码,解码和打破使用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);
返回转换;
}
[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)
Edit (now that I've done the math)
At that blazing speed, it would still take ~190 billion years to go over all combinations.
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中的解密器/断路器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!