Hey bytes, you may or may not remember but last time I was here a few months ago I got some help with making a hangman program which went well. Now I'm still doing okay in Java this year but I'm starting a new project for simple encryption and decryption.
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
I asked my teacher for a bit of help but I don't understand what his clues meant. He said something about the number 26 being important to this whole thing...can anyone help me make my program to encrypt letters to letters???
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
Code:
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();
Comment