维吉尼亚密码算法在Java中的解密消息到明文

huangapple go评论76阅读模式
英文:

Vigenere Cipher Algorithm In Java - Decrypted Message to Plaintext

问题

import java.util.Scanner;

public class VigenereCipher {
    public static void main(String arg[]) {
        String message = "";
        String keyword = "KISWAHILI";

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a message: ");
        message = sc.nextLine();

        char msg[] = message.toCharArray();
        int msgLength = msg.length;

        char key[] = new char[msgLength];
        char decryptedText[] = new char[msgLength];

        for (int i = 0, j = 0; i < msgLength; i++, j++) {
            if (j == keyword.length()) {
                j = 0;
            }
            key[i] = keyword.charAt(j);
        }

        // Decryption Code
        for (int i = 0; i < msgLength; i++) {
            decryptedText[i] = (char) (((msg[i] - key[i] + 26) % 26) + 'A');
        }
        System.out.println("Decrypted Message: " + String.valueOf(decryptedText));
        System.out.println("Keyword: " + keyword);
        System.out.println("Plaintext: " + String.valueOf(decryptedText));
    }
}
英文:

I am currently trying to write the Vigenere Cipher algorithm in Java. I have to change the decrypted message to the plaintext but having trouble. Below is what I have so far.

When I run it, the message is not deciphered properly.
维吉尼亚密码算法在Java中的解密消息到明文

import java.util.Scanner;

public class VigenereCipher {
		public static void main(String arg[]) {
			String message = &quot;&quot;;
			String keyword = &quot;KISWAHILI&quot;;
			
			Scanner sc = new Scanner(System.in);
			System.out.println(&quot;Enter a message: &quot;);
			message = sc.nextLine();
			
			char msg[] = message.toCharArray();
			int msgLength = msg.length;
		
			
			char key[] = new char [msgLength];
			char decryptedText[] = new char[msgLength];
			
			for(int i = 0, j = 0; i &lt; msgLength; i++, j++) {
				if(j == keyword.length()) {
					j = 0;
				}
				key[i] = keyword.charAt(j);
			}
			
			// Decryption Code
			for(int i =0; i &lt; msgLength; i++) {
				decryptedText[i] = (char)(((key[i] + 26) % 26) + &#39;A&#39;);
			}
			System.out.println(&quot;Decrypted Message: &quot; + message);
			System.out.println(&quot;Keyword: &quot; + keyword);
			System.out.println(&quot;Plaintext: &quot; + String.valueOf(decryptedText));
		}
}

答案1

得分: 1

代码部分已翻译如下:

似乎在填充key数组时需要跳过空格:

for (int i = 0, j = 0; i < msgLength; i++) {
    if (msg[i] == ' ') {
        key[i] = ' ';
    } else {
        key[i] = keyword.charAt(j++ % keyword.length());
    }
}
System.out.println("关键消息:       " + new String(key));

类似地,在解密循环中也需要考虑空格。并且解密需要进行修复:

for (int i = 0; i < msgLength; i++) {
    char c = msg[i];

    decryptedText[i] = c == ' ' ? c : (char)(((msg[i] - key[i] + 26) % 26) + 'A');
}

应用这些更改后,输出如下所示:

关键消息:       KISW AH ILIKI SW AHILIKIS WAH ILI KISW AHILIKISW AHIL IKIS
加密消息: XQKP IZ IMWEB LK AUVZCXKW PHL VPE RIKD ASOZZSBZI TOIE ESTD
关键词: KISWAHILI
明文: NIST IS ABOUT TO ANNOUNCE THE NEW HASH ALGORITHM THAT WILL
英文:

It seems that whitespaces need to be skipped while populating key array:

for (int i = 0, j = 0; i &lt; msgLength; i++) {
    if (msg[i] == &#39; &#39;) {
        key[i] = &#39; &#39;;
    } else {
        key[i] = keyword.charAt(j++ % keyword.length());
    }
}
System.out.println(&quot;Key Message:       &quot; + new String(key));

Similarly, it needs to be taken into account in the decrypting loop.
And decryption has to be fixed:<br/>
D<sub>i</sub> = (M<sub>i</sub> - K<sub>i</sub> + 26 ) mod 26

for (int i =0; i &lt; msgLength; i++) {
    char c = msg[i];
        	
    decryptedText[i] = c == &#39; &#39; ? c : (char)(((msg[i] - key[i] + 26) % 26) + &#39;A&#39;);
}

Upon applying these changes, the output is as follows:

Key Message:       KISW AH ILIKI SW AHILIKIS WAH ILI KISW AHILIKISW AHIL IKIS
Encrypted Message: XQKP IZ IMWEB LK AUVZCXKW PHL VPE RIKD ASOZZSBZI TOIE ESTD
Keyword: KISWAHILI
Plaintext: NIST IS ABOUT TO ANNOUNCE THE NEW HASH ALGORITHM THAT WILL

huangapple
  • 本文由 发表于 2020年10月4日 05:53:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64189371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定