The way I set up the program to work is just to add 1 to each character to change a message... (example: "hello" becomes "ifmmp") or subtract 1 from each character to decrypt a message the user enters.
But doing the encryption that way has problems too. I want to have a range of numbres from 0-9, letters from a-z, and A-Z. The problem is if I have the letter z, it doesn't go back to a...which is logically correct since the next character in the unicode is {
Same with spaces. I want spaces to go ignored in the encrypted version of the message instead of becoming "!"...
Here is the code
Expand|Select|Wrap|Line Numbers
- public static void encrypt()
- {
- System.out.println("Enter a message that you would like to encrypt\n");
- String s1=In.getString();
- char [] array = s1.toCharArray();
- for(int index=0; index<array.length; index++)
- {
- array[index]++;
- }
- String encrypted = new String(array);
- System.out.println("\nYour message has been encrypted to...\n");
- System.out.println(encrypted);
- In.getChar();
- clear();
- }
- public static void decrypt()
- {
- System.out.println("Please enter a message you would like to decrypt\n");
- String s2=In.getString();
- char [] array2 = s2.toCharArray();
- for (int index=0; index<array2.length; index++)
- {
- array2[index]--;
- }
- String decrypted = new String(array2);
- System.out.println("\nYour message has been decrypted to...\n");
- System.out.println(decrypted);
- In.getChar();
- clear();