Java Eclipse加密麻烦

时间:2012-12-11 00:25:01

标签: java encryption

我需要制作一个程序来加密和解密用户输入的内容。我无法找到一种将所有字符组合起来制作加密字的方法。这是我的代码(我正在使用Eclipse):

          import java.util.Scanner;

        public class Encryption 
        {
            public static String message = "";
            public static boolean hasMessage = false;
            public static boolean encrypted = false;
            static char a = 0; 
            static char b;
            static int w;
            static int x;
            static int y;
            static int z;
        static int i;


            public static void display()
            {
                System.out.println("Message: " + message + "\n");
            }

            public static void encrypt(String word)
            {
                if(!hasMessage)
                {
                    System.out.println("No message");
                    // Tell the user there is no message
                }
                else if(encrypted)
                {
                    System.out.println("Message is already encrypted");
                    // Tell the user the message is already encrypted
                }

                else
                {

                    // Reset the message to blank
            for (int i = 0; i < message.length(); i++) {
                i = j;
                ``char a = message.charAt(i);
            for (int j=0; j==message.length(); j++) 
            {
                int w = (int) a * 2;
                int x = (int) w + 2;  
                char y = (char) x; 
            }
            }
    }

=

                    //get char from each letter (increase char each time),  cast as int


                }
                System.out.println(message);
                encrypted = true;

                // Using the parameter word, modify message
                // to contain a new value following a predictable pattern
                // Hint:  alter each character's ASCII value by casting
                //        to an integer and using math operators

                // Display the new message
                // Set encrypted to true


            }

            public static void decrypt(String word)
            {
                if(!hasMessage)
                {
                    System.out.println("No message");
                    // Tell the user there is no message
                }
                else if(!encrypted)
                {
                    System.out.println("Message not encrypted");
                    // Tell the user the message is not encrypted

                }
                else
                {
                    int a = (int) w / 2;
                    int w = (int) x - 2;  
                    char x = (char) y; 
                    System.out.println(message);
                    // Like encrypt, but in reverse
                }

            }

            public static void main(String[] args) 
            {  
                Scanner sc = new Scanner(System.in);
                int menuChoice = 0;

                while(menuChoice != 4)
                {
                    System.out.println( "[1] Enter Word\n" + 
                            "[2] Encrypt\n" + 
                            "[3] Decrypt\n" + 
                            "[4] Quit\n");

                    menuChoice = sc.nextInt();

                    if(menuChoice == 1)
                    {
                        System.out.println("Input message");
                        message = sc.next();
                        // prompt user to input message
                        // assign next word to the class-level variable message
                        hasMessage = true;
                        encrypted = false;
                        // set hasMessage to true
                        // set encrypted to false

                    }
                    else if(menuChoice == 2)
                    {
                        encrypt(message);
                    }
                    else if(menuChoice == 3)
                    {
                        decrypt(message);
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

可能来自for循环的麻烦。我真的不知道这个for循环应该做什么,所以我无法帮助你改变它。

for (message.charAt(a); a==message.length(); a++) 
        {
            int w = (int) a * 2;
            int x = (int) w + 2;  
            char y = (char) x; 
        }

伪代码的作用是什么:

  1. 要求字符串a中的字符为0,不执行任何操作。这只是浪费时间,所以我不知道这究竟是什么。
  2. 如果是,则检查消息是否为空,否则完全循环。
  3. 执行此循环中的代码。
  4. 我想你想要做的是通过char循环遍历字符串char:

    for (int i = 0; i < message.length(); i++) {
        char a = message.charAt(i);
    }