Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
import java.io.*;
import java.lang.*;
import java.awt.*;
public class EncryptDecryptTest
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input, input1;
int choice;
int convert;
String inputString;
//try{
do{
System.out.println("\n\t Menu");
System.out.println("\t----------");
System.out.println("\t1. Encrypt");
System.out.println("\t2. Decrypt");
System.out.println("\t3. Exit");
System.out.print("\nEnter choice: ");
choice = in.read();
if(choice == '1')
{
System.out.print("Enter msg to encrypt: ");
input = br.readLine();
inputString = input.toString();
System.out.println("\nThe msg is enrypted to: ");for(int i = input.length() -1; i>= 0; i--)
{
convert = input.charAt(i);
System.out.println(convert);
}
}else if(choice == '2')
{System.out.print("\nEnter encrypted msg to decrypt: ");
input1 = br.readLine();
System.out.println("The msg has been decrypted to: ");
//}
}
else if(choice == '3')
{
System.exit(0);
}
}while (choice != 3);//}
//catch(IOException ioe){}
}
}

It's tough to know what you're trying to do here, but it looks like you're "encrypting" a string by replacing characters with their ascii equivalent. So I guess you want to decrypt from the ascii number to the character. Where does hex come in to play? You're encoding them to just regular old integers. If this is the case, the user will have to separate each ascii value with a space, so you know where one begins and ends. Then you can use the stringTokenizer object (look at the documentation on java.sun.com) to read in each ascii number as a string, the Integer.parseInt(string) to get the integer out of it, then (char)i where i is an integer to get it back to the character value. Use a while loop with the stringTokenizer.numTokens() as a sentinel. I didn't write the code all out because I wasn't sure if I understood the problem properly. If this doesn't help, post back. Good luck,
SN

![]() |
discussion board
|
I need your suggestion
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |