Help with a simple encryption program??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yottabyte
    New Member
    • Nov 2008
    • 13

    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

    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();
    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???
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Here's a hint: think of this String:

    Code:
    String enc= "bcdefghijklmnopqrstuvwxyza";
    If a character, say, x is an element of this String, the next value is >= 0

    Code:
    int index= enc.indexOf(x);
    And the encrypted character y is:

    Code:
    char y= enc.charAt(x-'a');
    Decrypting a character y is:

    Code:
    char x= (char)('a'+enc.indexOf(y));
    note that the enc String could've been "1234567890 " or " ".

    kind regards,

    Jos

    Comment

    • yottabyte
      New Member
      • Nov 2008
      • 13

      #3
      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??

      Code:
      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);

      Comment

      • yottabyte
        New Member
        • Nov 2008
        • 13

        #4
        Never mind I fixed it

        Comment

        Working...