473,761 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with a simple encryption program??

13 New Member
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 9873
JosAH
11,448 Recognized Expert MVP
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
yottabyte
13 New Member
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
yottabyte
13 New Member
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
2566
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: 1. CallControlData does not provide a operator= 2. INUserHandle's operator= returns a object instead of a reference 3. Use CC -g main_leak.C instead of CC main_leak.C to compile. You change any of the above condition, there will be NO mem leak.
4
2105
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 of the string cannot increase either (no more than 14 chars) and must be numeric only.
11
6532
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 key" confusion (which seems to be all google can come up with) - I just want a SIMPLE example of how I can encrypt a string and then get back the SAME STRING. I found an example on MSDN which involves compiling a DLL then using it in a test form, but...
2
2123
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
2129
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 I would really apprecaite it. Thanks SK //Specs to be added later //C Libraries #include <stdio.h>
13
2708
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 help. i would appreciate if somebody help me out of this to debug the program below. The chess board suppose to be printed with "*" and space " " as you can see below as an example. Again I would appreciate for your help in advance. sample...
1
2293
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 a string like "TESTING SHIFT Z 9" with a key of 1 becomes "UFTUJOH TIJGU A 0" 9 wraps to 0 and Z wraps to A
4
3710
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 output it into the output text box. I also want to encrypt it with a secret key where the user enters a letter or set of letters into a separate text box which will effect the output. This process can be reversed to decrypt. I have a few ideas but im...
0
9554
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9377
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.