473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Help with a simple encryption program??

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
3 9829
JosAH
11,448 Expert 8TB
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
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
Never mind I fixed it
Mar 2 '09 #4

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

Similar topics

6
by: Scott Niu | last post by:
Hi, I have this following simple c++ program, it will produce memory leak ( see what I did below ). My observation also showed that: There will be a mem leak when all the 3 conditions are true:...
4
by: Steven Blair | last post by:
I have the following number: 64521234567890 and need to apply some sort of simple encryption. Does c# have any classes for doing this. I cant use 3DES or anything as complex as. The size...
11
by: Beeeeeeeeeeeeves | last post by:
Hi I'm looking for some .net example code on encryption: I don't want anything fancy, no complex third party components with sophisticated algorithms, explanations of principle, or "public/private...
2
by: David J Rose | last post by:
I am looking for a simple encryption method that is common to .net and php. Does anyone know off-hand of such a method? Thanks
8
by: SK | last post by:
Hi I am trying to write a simple C program with devc++ as the complier using the concept of arrays. I cannot get the program to compile without mutiple errors. If anyone has the time to help me...
13
by: asif929 | last post by:
I have been trying to create this chess program from many hours and still couldn't figure out how to complete it. After consume all these efforts i come to this google groups for the first time for...
1
by: googleplexx | last post by:
Hello, I'm trying to make a simple Caesar cipher however instead of A-Z i want to also include 0-9. where 9 wraps back to A. The best i could can do shift letters and integers independently so...
4
by: Wolfman2307 | last post by:
Hi, I want to make a simple encryption program. My idea is to have an input and an output text box. When the user presses a button the program will encode what is in the input text box and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.