Results 1 to 6 of 6
  1. #1
    superdhebz is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Talking Need Help for encrypt/decrypt.

    Hi! I need help for my program. This program encrypts and decrypts a string. It is actually working but I want to make changes.

    In my program, when you choose to encrypt a word, it encrypts the word then it ends its process. I want to go back to the "main option" where the user can decrypt the string he just encrypted. How do you do that?

    Here's the code:

    Java Code:
    import java.io.*;
    
    class MC14{
    
    	static BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));
      	static PrintWriter screen=new PrintWriter(System.out, true);	//Setting up input/output variables.
    
    
    public static void main(String[] args) throws IOException {
    
    	int choice;
    	String str;
    
    	screen.println("ENCRYPT & DECRYPT");
    	screen.println();
    	screen.println("Please enter the text to be encrypted/decrypted.");
    	screen.println();
    	str = keyboard.readLine();
    
    	screen.println();
    	screen.println();
    	screen.println("Choose an option: ");
    	screen.println();
    	screen.println("1) Encrypt string. \n2) Decrypt string.");
    	screen.println();
    
    	choice = new Integer(keyboard.readLine()).intValue();
    
    
    		switch(choice){
    
    			case 1:
    					screen.println("Encrypted string:");
    					screen.println();
    					screen.println(encrypt(str));
    					screen.println();
    					break;
    			case 2:
    					screen.println("Decrypted string:");
    					screen.println();
    					screen.println(decrypt(str));
    					screen.println();
    					break;
    		}
    
    }
    
    
    public static String encrypt(String str) {
    
    	str = str.toLowerCase(); //convert string to lower case to avoid problems
    
    	int length = str.length();
    	int i,count,inc;
    	char[] array = new char[length];	//variables used in the method
    	String encrypted;
    
    
    
    	for(i=0;i<length;i++){array[i] = str.charAt(i);} //Places each character in the string
    													 //into an array index.
    
    
    	for(count=0;count<array.length;count++){ //for each item in the array the following switch is performed
    
    		switch(array[count]){				//This switch statement changes the character
    											//within each array index. This is the start
    			case 'a':						//of the encryption.
    					array[count] = '7';
    					break;
    
    			case 'b':
    					array[count] = 'k';
    					break;
    
    			case 'c':
    					array[count] = 'r';
    					break;
    
    			case 'd':
    					array[count] = 's';
    					break;
    
    			case 'e':
    					array[count] = '3';
    					break;
    
    			case 'f':
    					array[count] = 'e';
    					break;
    
    			case 'g':
    					array[count] = 'a';
    					break;
    
    			case 'h':
    					array[count] = 'o';
    					break;
    
    			case 'i':
    					array[count] = '8';
    					break;
    
    			case 'j':
    					array[count] = ',';
    					break;
    
    			case 'k':
    					array[count] = 'b';
    					break;
    
    			case 'l':
    					array[count] = 'x';
    					break;
    
    			case 'm':
    					array[count] = 'c';
    					break;
    
    			case 'n':
    					array[count] = 'h';
    					break;
    
    			case 'o':
    					array[count] = '5';
    					break;
    
    			case 'p':
    					array[count] = '?';
    					break;
    
    			case 'q':
    					array[count] = 'd';
    					break;
    
    			case 'r':
    					array[count] = '4';
    					break;
    
    			case 's':
    					array[count] = 'f';
    					break;
    
    			case 't':
    					array[count] = 'm';
    					break;
    
    			case 'u':
    					array[count] = 'z';
    					break;
    
    			case 'v':
    					array[count] = 'g';
    					break;
    
    			case 'w':
    					array[count] = '1';
    					break;
    
    			case 'x':
    					array[count] = 'i';
    					break;
    
    			case 'y':
    					array[count] = '9';
    					break;
    
    			case 'z':
    					array[count] = 'j';
    					break;
    
    			case '1':
    					array[count] = '6';
    					break;
    
    			case '2':
    					array[count] = 'p';
    					break;
    
    			case '3':
    					array[count] = '.';
    					break;
    
    			case '4':
    					array[count] = '!';
    					break;
    
    			case '5':
    					array[count] = '0';
    					break;
    
    			case '6':
    					array[count] = 'l';
    					break;
    
    			case '7':
    					array[count] = 'y';
    					break;
    
    			case '8':
    					array[count] = 'n';
    					break;
    
    			case '9':
    					array[count] = 'u';
    					break;
    
    			case '0':
    					array[count] = 'q';
    					break;
    
    			case '.':
    					array[count] = 'w';
    					break;
    
    			case '!':
    					array[count] = 'v';
    					break;
    
    			case '?':
    					array[count] = '2';
    					break;
    
    			case ',':
    					array[count] = 't';
    					break;
    
    			case ' ':
    					array[count] = ' ';
    					break;
    
    		}
    
    	}
    		encrypted = String.valueOf(array);	//constructing a String object
    											//from char array[]
    
    		StringBuffer reversed = new StringBuffer(encrypted); //creating a StringBuffer object from String encrypted.
    		reversed.reverse();	 //reversing order within StringBuffer object.
    
    		encrypted = new String(reversed); //setting the encrypted String from
    							              //StringBuffer object reversed.
    
    
    		return encrypted;	//returning String encrypted.
    
    
    
    	} // END OF METHOD encrypt();
    
    
    public static String decrypt(String str){
    
    	str = str.toLowerCase(); //convert string to lower case to avoid problems
    
    	int length = str.length();
    	int i,count;						//variable used in the method
    	char[] array = new char[length];
    
    
    	StringBuffer reversed = new StringBuffer(str);
    	reversed.reverse();								//reverses the encrypted string
    
    
    	String encrypted = new String(reversed);
    
    	for(i=0;i<length;i++){array[i] = encrypted.charAt(i);} //places all characters of string into array
    
    
    		for(count=0;count<length;count++){
    
    			switch(array[count]){				//This switch statement changes the character
    														//within each array index. This is the start
    						case 'a':						//of the decryption.
    								array[count] = 'g';
    								break;
    
    						case 'b':
    								array[count] = 'k';
    								break;
    
    						case 'c':
    								array[count] = 'm';
    								break;
    
    						case 'd':
    								array[count] = 'q';
    								break;
    
    						case 'e':
    								array[count] = 'f';
    								break;
    
    						case 'f':
    								array[count] = 's';
    								break;
    
    						case 'g':
    								array[count] = 'v';
    								break;
    
    						case 'h':
    								array[count] = 'n';
    								break;
    
    						case 'i':
    								array[count] = 'x';
    								break;
    
    						case 'j':
    								array[count] = 'z';
    								break;
    
    						case 'k':
    								array[count] = 'b';
    								break;
    
    						case 'l':
    								array[count] = '6';
    								break;
    
    						case 'm':
    								array[count] = 't';
    								break;
    
    						case 'n':
    								array[count] = '8';
    								break;
    
    						case 'o':
    								array[count] = 'h';
    								break;
    
    						case 'p':
    								array[count] = '2';
    								break;
    
    						case 'q':
    								array[count] = '0';
    								break;
    
    						case 'r':
    								array[count] = 'c';
    								break;
    
    						case 's':
    								array[count] = 'd';
    								break;
    
    						case 't':
    								array[count] = ',';
    								break;
    
    						case 'u':
    								array[count] = '9';
    								break;
    
    						case 'v':
    								array[count] = '!';
    								break;
    
    						case 'w':
    								array[count] = '.';
    								break;
    
    						case 'x':
    								array[count] = 'l';
    								break;
    
    						case 'y':
    								array[count] = '7';
    								break;
    
    						case 'z':
    								array[count] = 'u';
    								break;
    
    						case '1':
    								array[count] = 'w';
    								break;
    
    						case '2':
    								array[count] = '?';
    								break;
    
    						case '3':
    								array[count] = 'e';
    								break;
    
    						case '4':
    								array[count] = 'r';
    								break;
    
    						case '5':
    								array[count] = 'o';
    								break;
    
    						case '6':
    								array[count] = '1';
    								break;
    
    						case '7':
    								array[count] = 'a';
    								break;
    
    						case '8':
    								array[count] = 'i';
    								break;
    
    						case '9':
    								array[count] = 'y';
    								break;
    
    						case '0':
    								array[count] = '5';
    								break;
    
    						case '.':
    								array[count] = '3';
    								break;
    
    						case '!':
    								array[count] = '4';
    								break;
    
    						case '?':
    								array[count] = 'p';
    								break;
    
    						case ',':
    								array[count] = 'j';
    								break;
    
    						case ' ':
    								array[count] = ' ';
    								break;
    
    					}
    
    
    			}
    
    
    			String decrypted = new String (String.valueOf(array)); //constructing string from array of char.
    			return decrypted;
    
    
    	} //END OF METHOD decrypt()
    
    
    } //END OF CLASS Encrypto

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,581
    Rep Power
    23

    Default

    I want to go back to the "main option" where the user can decrypt the string he just encrypted. How do you do that?
    Use a loop with a menu of choices at the beginning asking user what he wants to do .
    Perhaps with a boolean to test if he has an encrypted String available to decript.

    Comments on your code:
    With the two loooooooong case statements, it will be impossible to make any changes to it without problems.

    You should have default: case at the end of the switch to print out unhandled chars

  3. #3
    superdhebz is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    Hi! Can you set an dummy example for loop?

    P.S. Sorry for the loooooooong case statements. Hehe :)

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,581
    Rep Power
    23

    Default

    Some pseudo code:
    while(looping)
    ask user what to do
    Test response and do one of the following;
    do this
    do that
    exit loop
    end of while loop

  5. #5
    superdhebz is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    Hmmm. Okay. I'll try that. Thank you :)

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,581
    Rep Power
    23

Similar Threads

  1. How to decrypt SSL?
    By Sergio in forum Advanced Java
    Replies: 1
    Last Post: 08-21-2010, 05:03 AM
  2. Encrypt and decrypt SMS
    By ZeCute in forum CLDC and MIDP
    Replies: 8
    Last Post: 06-13-2010, 05:01 AM
  3. Replies: 2
    Last Post: 04-19-2010, 06:25 PM
  4. Ideas to decrypt this ?
    By ketku in forum Advanced Java
    Replies: 0
    Last Post: 11-16-2009, 10:52 AM
  5. encrypt a folder
    By subramanya2teja in forum Advanced Java
    Replies: 5
    Last Post: 03-20-2009, 12:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •