我在Java
中用BlueJ
编写的程序解密了由26个小写字母(a,b,c,...,x,y,z)组成的字符串密码文本已使用affine cipher
加密。它使用强力搜索,尝试所有可能的密钥。
程序正常工作,但我输出有问题。
我应该输出"加密密钥"和"解密方程"在每次暴力搜索之后,我似乎无法弄清楚如何。
这是我目前所拥有的示例输出:
Decrypting the cipher text:
vwduwljudeehghyhubwklqjlfrxogilqgsohdvhuhwxuqdqbeoxhsulqwviruydxowdqgdodupghvljqedvhgrqzklfkedqnbrxghflghrqldpvhwwlqjxsvdihkrxvhfr
Plaintext:
uvctvkitcddgfgxgtavjkpikeqwnfhkpfrngcugtgvwtpcpadnwgrtkpvuhqtxcwnvcpfcnctofgukipdcugfqpyjkejdcpmaqwfgekfgqpkcougvvkpiwruchgjqwugeq
Hit 0 to continue search or 1 to stop:
0
Plaintext:
yhsphmupsbbctczcpahdmfumkoqntlmftxncsycpchqpfsfabnqcxpmfhylopzsqnhsftsnspwtcymufbsyctofidmkdbsfeaoqtckmtcofmswychhmfuqxyslcdoqycko
Hit 0 to continue search or 1 to stop:
0
...
... many lines later
...
Plaintext:
gfyhfqshyxxuvuduhafrqlsqwkenvtqlvjnuyguhufehlylaxneujhqlfgtkhdyenfylvynyhmvugqslxyguvklcrqwrxyloakevuwqvuklqymguffqlsejgyturkeguwk
Hit 0 to continue search or 1 to stop:
0
Plaintext:
startigrabbedeverythingicouldfindpleasereturnanyblueprintsforvaultandalarmdesignbasedonwhichbankyoudecideoniamsettingupsafehouseco
Hit 0 to continue search or 1 to stop:
1
这是应该显示的示例输出:
Decrypting the cipher text:
vwduwljudeehghyhubwklqjlfrxogilqgsohdvhuhwxuqdqbeoxhsulqwviruydxowdqgdodupghvljqedvhgrqzklfkedqnbrxghflghrqldpvhwwlqjxsvdihkrxvhfr
Encryption key: a = 1 b = 1
Decryption equation: x = 1 *(y - 1)
Plaintext:
uvctvkitcddgfgxgtavjkpikeqwnfhkpfrngcugtgvwtpcpadnwgrtkpvuhqtxcwnvcpfcnctofgukipdcugfqpyjkejdcpmaqwfgekfgqpkcougvvkpiwruchgjqwugeq
Hit enter to continue search or 'S' key to stop:
Encryption key: a = 3 b = 1
Decryption equation: x = 9 *(y - 1)
Plaintext:
yhsphmupsbbctczcpahdmfumkoqntlmftxncsycpchqpfsfabnqcxpmfhylopzsqnhsftsnspwtcymufbsyctofidmkdbsfeaoqtckmtcofmswychhmfuqxyslcdoqycko
Hit enter to continue search or 'S' key to stop:
Encryption key: a = 5 b = 1
Decryption equation: x = 21 *(y - 1)
...
... many lines later
...
Hit enter to continue search or 'S' key to stop:
Encryption key: a = 25 b = 2
Decryption equation: x = 25 *(y - 2)
Plaintext:
hgzigrtizyyvwvevibgsrmtrxlfowurmwkovzhvivgfimzmbyofvkirmghuliezfogzmwzozinwvhrtmyzhvwlmdsrxsyzmpblfwvxrwvlmrznhvggrmtfkhzuvslfhvxl
Hit enter to continue search or 'S' key to stop:
Encryption key: a = 1 b = 3
Decryption equation: x = 1 *(y - 3)
Plaintext:
startigrabbedeverythingicouldfindpleasereturnanyblueprintsforvaultandalarmdesignbasedonwhichbankyoudecideoniamsettingupsafehouseco
Hit enter to continue search or 'S' key to stop: S
The plain text message below was encrypted with a = 1 and b = 3
startigrabbedeverythingicouldfindpleasereturnanyblueprintsforvaultandalarmdesignbasedonwhichbankyoudecideoniamsettingupsafehouseco
这是我的代码:
import java.util.Scanner;
import java.math.BigInteger;
public class AffineCipher{
/**
* Prompts the user to enter a string of cipher text, along with the ablility
* to continue to search or to stop the search. The output or plaintext is
* printed each time.
*/
public static void main(String[] args){
int count = 1;
int choice = 0;
int change = 0;
int stop = 1;
int cont = 0;
Scanner kbd = new Scanner (System.in);
System.out.printf("Decrypting the cipher text:%n");
String input = kbd.next();
System.out.printf("\n");
while (choice != stop){
while (choice == cont){
if (count == 13){
count = count + 2;
}
String deciphered = decrypt(input,change, count);
System.out.println("Plaintext:");
System.out.println(deciphered);
System.out.printf("\n");
if (count == 25){
change = 1;
count = 1;
}
System.out.printf("Hit 0 to continue search or 1 to stop:%n");
choice = kbd.nextInt();
System.out.printf("\n");
count = count + 2;
}
}
}
/**
* Deciphers the string cipher text.
*/
static String decrypt(String input,int change, int count){
if (change == 0){
int firstKey = 0 + count;
int secondKey = 1;
int mod = 26;
StringBuilder builder = new StringBuilder();
BigInteger inverse = BigInteger.valueOf(count).modInverse(BigInteger.valueOf(mod));
for (int in = 0; in < input.length(); in++){
char character = input.charAt(in);
if (Character.isLetter(character)){
int decoded = inverse.intValue() * (character - 'a' - secondKey + mod);
character = (char) (decoded % mod + 'a');
}
builder.append(character);
}
return builder.toString();
} else{
int firstKey = 1;
int mod = 26;
StringBuilder builder = new StringBuilder();
BigInteger inverse = BigInteger.valueOf(firstKey).modInverse(BigInteger.valueOf(mod));
for (int in = 0; in < input.length(); in++){
char character = input.charAt(in);
if (Character.isLetter(character)){
int decoded = inverse.intValue() * (character - 'a' - count + mod);
character = (char) (decoded % mod + 'a');
}
builder.append(character);
}
return builder.toString();
}
}
}
任何帮助将不胜感激。谢谢。