By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,742 Members | 773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,742 IT Pros & Developers. It's quick & easy.

Help with a simple encryption program??

P: 13
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

Expand|Select|Wrap|Line Numbers
  1.     public static void encrypt()
  2.     {
  3.         System.out.println("Enter a message that you would like to encrypt\n");
  4.  
  5.         String s1=In.getString();
  6.         char [] array = s1.toCharArray();
  7.  
  8.         for(int index=0; index<array.length; index++)                                
  9.         {
  10.             array[index]++;
  11.         }
  12.  
  13.         String encrypted = new String(array);
  14.  
  15.         System.out.println("\nYour message has been encrypted to...\n");
  16.  
  17.         System.out.println(encrypted);
  18.  
  19.         In.getChar();
  20.         clear();
  21.     }
  22.  
  23.     public static void decrypt()
  24.     {
  25.         System.out.println("Please enter a message you would like to decrypt\n");
  26.  
  27.         String s2=In.getString();
  28.         char [] array2 = s2.toCharArray();
  29.  
  30.         for (int index=0; index<array2.length; index++)
  31.         {
  32.             array2[index]--;
  33.         }
  34.  
  35.         String decrypted = new String(array2);
  36.  
  37.         System.out.println("\nYour message has been decrypted to...\n");
  38.  
  39.         System.out.println(decrypted);
  40.  
  41.         In.getChar();
  42.         clear();
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???
Feb 26 '09 #1
Share this Question
Share on Google+
3 Replies


Expert 10K+
P: 11,448
Here's a hint: think of this String:

Expand|Select|Wrap|Line Numbers
  1. String enc= "bcdefghijklmnopqrstuvwxyza";
  2.  
If a character, say, x is an element of this String, the next value is >= 0

Expand|Select|Wrap|Line Numbers
  1. int index= enc.indexOf(x);
  2.  
And the encrypted character y is:

Expand|Select|Wrap|Line Numbers
  1. char y= enc.charAt(x-'a');
  2.  
Decrypting a character y is:

Expand|Select|Wrap|Line Numbers
  1. char x= (char)('a'+enc.indexOf(y));
  2.  
note that the enc String could've been "1234567890" or " ".

kind regards,

Jos
Feb 26 '09 #2

P: 13
Ok I figured out a way to fix the problem with spaces and letters and the like. (I don't have the updated code on this computer but it's there I assure you :P)

I want to make an option that allows users to encrypt by a certain number that they choose, how do i do that?

As you can see I can probably place a message that asks by how much do you want to encrypt? If they write 2 for example, it adds two instead of one. I just don't know where to put that...and i need a reply for this soon since my deadline is coming up can anyone help please??

Expand|Select|Wrap|Line Numbers
  1. System.out.println("Enter a message that you would like to encrypt\n");
  2.  
  3.         String s1=In.getString();
  4.         char [] array = s1.toCharArray();
  5.  
  6.         for(int index=0; index<array.length; index++)                                
  7.         {
  8.             array[index]++;
  9.         }
  10.  
  11.         String encrypted = new String(array);
  12.  
  13.         System.out.println("\nYour message has been encrypted to...\n");
  14.  
  15.         System.out.println(encrypted);
Mar 1 '09 #3

P: 13
Never mind I fixed it
Mar 2 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.