Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need Help for encrypt/decrypt.

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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:
    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
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need Help for encrypt/decrypt.

    Something like this?
    screen.println("ENCRYPT & DECRYPT");
    screen.println();
    screen.println("Enter q to exit");
     
    String str  = keyboard.readLine();
    while (!str.equals('q') ){
        ///do work here
      str  = keyboard.readLine();
    }
    Last edited by copeg; September 17th, 2010 at 10:35 AM.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help for encrypt/decrypt.

    I'll try. Thanks!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need Help for encrypt/decrypt.

    Cross posted at Need Help for encrypt/decrypt. - Java Forums