我的解密程序无法正常工作...
public static void decipherMessage() {
Scanner sc = new Scanner(in);
System.out.println("Enter keyword: ");
String decipher = sc.nextLine();
String plain = ("abcdefghijklmnopqrstuvwxyz");
String cipher = decipher + "abcdefghijklmnopqrstuvwxyz";
System.out.println("Enter enciphered message: ");
String ciphMsg = sc.nextLine();
String decipherMessage = ciphMsg.toLowerCase();
char[] chars = cipher.toCharArray();
Set<Character> charSet = new LinkedHashSet<Character>();
for (char c : chars) {
charSet.add(c);
}
StringBuilder cMap = new StringBuilder();
for (Character character : charSet) {
cMap.append(character);
StringBuilder original = cMap;
}
for (int i = 0; i < 26; i++)
{
char cipherTextChar = cMap.charAt(i);
char plainTextChar = plain.charAt(i);
decipherMessage = decipherMessage.replace(cipherTextChar,plainTextChar);
System.out.println("Deciphered message");
System.out.println(decipherMessage);
}
纯文本字母应该替换加密字母
但是当我运行它时,它只是再次给出加密消息而不解密?
例子。
keyword: stack
plain : abcdefghijklmnopqrstuvwxyz
cipher: stackbdefghijlmnopqruvwxyz
message: overflow
ciphered message: mvhpbimw
除非反过来。使用我在上面显示的代码,它只是再次给出加密消息,而不解密任何内容。