我的Della Porta密码有什么问题?

时间:2019-11-24 20:01:05

标签: java for-loop cryptography iteration

我目前正在从事一个涉及Della Porta Cipher的编程项目。我的EncryptMessage方法在用户输入中遇到空格时似乎被挂断了。如果我从用户消息中删除了所有空格,则程序将正确编码消息,但将空格排除在外。我需要空格,并且在打印出来之前,我一直坚持如何将它们重新引入convertedMessage字符串中。

此外,我需要我的密钥在输出中的用户消息上方进行迭代,以便用户可以看到密钥的哪个字符正在影响用户消息中的哪个字符字母。

我当前的代码:

package com.company;

import java.util.Scanner;

public class PortaCypher {

    public static void main(String[] args) {


        Scanner input = new Scanner(System.in);

        System.out.print("Enter message: ");
        String message = input.nextLine();
        char[] messageArray = message.trim().toLowerCase().toCharArray(); //getting rid of spaces gives the right encryption, minus the needed spaces
        System.out.print("enter keyword: ");
        String key = input.nextLine();
        char[] keyArray = key.toLowerCase().replaceAll(" ", "").toCharArray();
        String convertedMessage = String.valueOf(encryptMessage(messageArray, keyArray));

        //final output
        System.out.println();
        System.out.println(getKeyString(keyArray).toUpperCase());
        System.out.println(message.toUpperCase());
        System.out.println(convertedMessage.toUpperCase());

    }

    public static char[] encryptMessage(char[] messageArray, char[] keyArray) {
        char[] convertedMessage = new char[messageArray.length];

        for (int x = 0, y = 0; x < messageArray.length; x++) {
            char characterOfMessage = messageArray[x];
            char characterOfKey = keyArray[(y % keyArray.length)];

            if (characterOfMessage == ' ') {
                convertedMessage[x] = ' ';
                y = -1;  //resets characterOfKey to the beginning character of the key after seeing a space
            }
            else convertedMessage[x] = portaCypherTableau[(((int)characterOfKey)-97) / 2][((int)characterOfMessage)-97]; y++;


        }   return convertedMessage;
    }



    public static String getKeyString(char[] keyArray){
        String finalKey = "";
        for (int i = 0; i < keyArray.length; i++) { finalKey = finalKey + (keyArray[i]); }
        return finalKey;
    }

    public static String getMessageString(char[] messageArray){
        String finalMessage = "";
        for (int i = 0; i < messageArray.length; i++) { finalMessage = finalMessage + (messageArray[i]); }
        return finalMessage;
    }


    //                                                    Keys        a    b    c    d    e    f    g    h    i    j    k    l    m    m    o    p    q    r    s    t    u    v    w    x    y    z
    public static final char  portaCypherTableau[][] = /* A,B */   {{'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm' },
                                                       /* C,D */    {'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'n', 'm', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l' },
                                                       /* E,F */    {'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'n', 'o', 'l', 'm', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' },
                                                       /* G,H */    {'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'n', 'o', 'p', 'k', 'l', 'm', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' },
                                                       /* I,G */    {'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'n', 'o', 'p', 'q', 'j', 'k', 'l', 'm', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' },
                                                       /* K,L */    {'s', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'n', 'o', 'p', 'q', 'r', 'i', 'j', 'k', 'l', 'm', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' },
                                                       /* M,N */    {'t', 'u', 'v', 'w', 'x', 'y', 'z', 'n', 'o', 'p', 'q', 'r', 's', 'h', 'i', 'j', 'k', 'l', 'm', 'a', 'b', 'c', 'd', 'e', 'f', 'g' },
                                                       /* O,P */    {'u', 'v', 'w', 'x', 'y', 'z', 'n', 'o', 'p', 'q', 'r', 's', 't', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'a', 'b', 'c', 'd', 'e', 'f' },
                                                       /* Q,R */    {'v', 'w', 'x', 'y', 'z', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'a', 'b', 'c', 'd', 'e' },
                                                       /* S,T */    {'w', 'x', 'y', 'z', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'a', 'b', 'c', 'd' },
                                                       /* U,V */    {'x', 'y', 'z', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'a', 'b', 'c' },
                                                       /* W,X */    {'y', 'z', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'a', 'b' },
                                                       /* Y,Z */    {'z', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'a' }};
}

正确的输出如下:

enter image description here

0 个答案:

没有答案