473,320 Members | 1,945 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,320 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 9816
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.