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

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

Vigenere Cipher Algorithm In Java - Decrypted Message to Plaintext

问题

  1. import java.util.Scanner;
  2. public class VigenereCipher {
  3. public static void main(String arg[]) {
  4. String message = "";
  5. String keyword = "KISWAHILI";
  6. Scanner sc = new Scanner(System.in);
  7. System.out.println("Enter a message: ");
  8. message = sc.nextLine();
  9. char msg[] = message.toCharArray();
  10. int msgLength = msg.length;
  11. char key[] = new char[msgLength];
  12. char decryptedText[] = new char[msgLength];
  13. for (int i = 0, j = 0; i < msgLength; i++, j++) {
  14. if (j == keyword.length()) {
  15. j = 0;
  16. }
  17. key[i] = keyword.charAt(j);
  18. }
  19. // Decryption Code
  20. for (int i = 0; i < msgLength; i++) {
  21. decryptedText[i] = (char) (((msg[i] - key[i] + 26) % 26) + 'A');
  22. }
  23. System.out.println("Decrypted Message: " + String.valueOf(decryptedText));
  24. System.out.println("Keyword: " + keyword);
  25. System.out.println("Plaintext: " + String.valueOf(decryptedText));
  26. }
  27. }
英文:

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中的解密消息到明文

  1. import java.util.Scanner;
  2. public class VigenereCipher {
  3. public static void main(String arg[]) {
  4. String message = &quot;&quot;;
  5. String keyword = &quot;KISWAHILI&quot;;
  6. Scanner sc = new Scanner(System.in);
  7. System.out.println(&quot;Enter a message: &quot;);
  8. message = sc.nextLine();
  9. char msg[] = message.toCharArray();
  10. int msgLength = msg.length;
  11. char key[] = new char [msgLength];
  12. char decryptedText[] = new char[msgLength];
  13. for(int i = 0, j = 0; i &lt; msgLength; i++, j++) {
  14. if(j == keyword.length()) {
  15. j = 0;
  16. }
  17. key[i] = keyword.charAt(j);
  18. }
  19. // Decryption Code
  20. for(int i =0; i &lt; msgLength; i++) {
  21. decryptedText[i] = (char)(((key[i] + 26) % 26) + &#39;A&#39;);
  22. }
  23. System.out.println(&quot;Decrypted Message: &quot; + message);
  24. System.out.println(&quot;Keyword: &quot; + keyword);
  25. System.out.println(&quot;Plaintext: &quot; + String.valueOf(decryptedText));
  26. }
  27. }

答案1

得分: 1

代码部分已翻译如下:

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

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

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

  1. for (int i = 0; i < msgLength; i++) {
  2. char c = msg[i];
  3. decryptedText[i] = c == ' ' ? c : (char)(((msg[i] - key[i] + 26) % 26) + 'A');
  4. }

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

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

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

  1. for (int i = 0, j = 0; i &lt; msgLength; i++) {
  2. if (msg[i] == &#39; &#39;) {
  3. key[i] = &#39; &#39;;
  4. } else {
  5. key[i] = keyword.charAt(j++ % keyword.length());
  6. }
  7. }
  8. 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

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

Upon applying these changes, the output is as follows:

  1. Key Message: KISW AH ILIKI SW AHILIKIS WAH ILI KISW AHILIKISW AHIL IKIS
  2. Encrypted Message: XQKP IZ IMWEB LK AUVZCXKW PHL VPE RIKD ASOZZSBZI TOIE ESTD
  3. Keyword: KISWAHILI
  4. 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:

确定