0

I need to make a program that will encrypt and decrypt what the user enters. I am having trouble figuring out a way that will combine all the chars to make the encrypted word. Here is my code (I am using 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);
                    }
                }
            }
        }
nitind
  • 19,089
  • 4
  • 34
  • 43
  • 2
    There are some serious problems with this for loop: `for (message.charAt(a); a==message.length(); a++)` – irrelephant Dec 11 '12 at 00:27
  • The real problem is that you're trying to write your own encryption algorithm. Don't. Use an existing algorithm that's already been written and tested. Unless you are a true cryptography expert you will create a weak (easily broken) algorithm, and "good enough for my purposes" never is. See http://stackoverflow.com/q/20227/17300 – Stephen P Dec 11 '12 at 01:06

1 Answers1

0

The trouble probably from the for loop. I don't really know what this for loop should do so I can't help you to change it.

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

What this does in pseudocode:

  1. Ask for the character at 0 in string a and do nothing. This is only wasting time so I don't know what this actually is for.
  2. Check if the message is empty if yes go on otherwise quite the loop.
  3. Execute the code in this loop.

I guess what you want to do is loop through the string char by char like this:

for (int i = 0; i < message.length(); i++) {
    char a = message.charAt(i);
}
SIGKILL
  • 390
  • 1
  • 9