Thread: Encryption

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Encryption


    I am having trouble figuring out how to write code so the program will encrypt and decrypt what the user inputs. Here is my code and I am using Eclipse (I know some things don't work right):

    Code:
    import java.util.Scanner;
    
    public class Encryption 
    {
    	public static String message = "";
    	public static boolean hasMessage = false;
    	public static boolean encrypted = false;
    	char a; char b; char c; char d; char e; char f; char g; char h; char i; char j; char k; char l; char m; char n; char o; char p; 
    
    	public static void display()
    	{
    		System.out.println("Message: " + message + "\n");
    	}
    
    	public static void encrypt(String word)
    	{
    		if(!hasMessage)
    		{
    			System.out.println("No message");
    			// Tell the user there is no message
    		}
    		else if(encrypted);
    		{
    			System.out.println("Message is already encrypted");
    			// Tell the user the message is already encrypted
    		}
    		else
    		{
    			message = "";
    			// Reset the message to blank
    
    			for (message.length();;)
    			{		
    				char a=message.charAt(0);
    				char b=message.charAt(1);
    				char c=message.charAt(2);
    				char d=message.charAt(3);
    				char e=message.charAt(4);
    				char f=message.charAt(5);
    				char g=message.charAt(6);
    				char h=message.charAt(7);
    				char i=message.charAt(8);
    				char j=message.charAt(9);
    				char k=message.charAt(10);
    				char l=message.charAt(11);
    				char m=message.charAt(12);
    				char n=message.charAt(13);
    				char o=message.charAt(14);
    				char p=message.charAt(15);
    				
    				int  = (int) a;
    				int  = (int) b;
    				int  = (int) c;
    				int  = (int) d;
    				int  = (int) e;
    				int  = (int) f;
    				int  = (int) g;
    				int  = (int) h;
    				int  = (int) i;
    				int  = (int) j;
    				int  = (int) k;
    				int  = (int) l;
    				int  = (int) m;
    				int  = (int) n;
    				int  = (int) o;
    			}
    			//get char from each letter (increase char each time),	cast as int
    
    			System.out.println(message);
    			encrypted = true;
    		}
    		// Using the parameter word, modify message
    		// to contain a new value following a predictable pattern
    		// Hint:  alter each character's ASCII value by casting
    		//        to an integer and using math operators
    
    		// Display the new message
    		// Set encrypted to true
    
    
    	}
    
    	public static void decrypt(String word)
    	{
    		if(!hasMessage)
    		{
    			System.out.println("No message");
    			// Tell the user there is no message
    		}
    		else if(!encrypted)
    		{
    			System.out.println("Message not encrypted");
    			// Tell the user the message is not encrypted
    
    		}
    		else
    		{
    
    
    			System.out.println(message);
    			// Like encrypt, but in reverse
    		}
    
    	}
    
    	public static void main(String[] args) 
    	{  
    		Scanner sc = new Scanner(System.in);
    		int menuChoice = 0;
    
    		while(menuChoice != 4)
    		{
    			System.out.println(	"[1] Enter Word\n" + 
    					"[2] Encrypt\n" + 
    					"[3] Decrypt\n" + 
    					"[4] Quit\n");
    
    			menuChoice = sc.nextInt();
    
    			if(menuChoice == 1)
    			{
    				System.out.println("Input message");
    				message = sc.next();
    				// prompt user to input message
    				// assign next word to the class-level variable message
    				hasMessage = true;
    				encrypted = false;
    				// set hasMessage to true
    				// set encrypted to false
    
    			}
    			else if(menuChoice == 2)
    			{
    				encrypt(message);
    			}
    			else if(menuChoice == 3)
    			{
    				decrypt(message);
    			}
    		}
    	}
    }
    Edit: I changed it to my new code (I know the I need the ints)
    I'm a bit of a newbie to coding
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Please edit your post and wrap the code in code tags to preserve formatting.
  4. #3
  5. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,540
    Rep Power
    1621

    Encryption/decryption


    Your code cannot be compiled, as it contains syntax error.

    Before you code something complex like encrypting/decrypting a string, you should first learn the basics of Java programming language syntax.

    Numeric literals cannot be used as variable names, as Java variables cannot be declared with names that start with numbers.
    When the programming world turns decent, the real world will turn upside down.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    The code is now all on one line. Can you format the code so each statement is on its own line?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by tvc3mye
    Your code cannot be compiled, as it contains syntax error.

    Before you code something complex like encrypting/decrypting a string, you should first learn the basics of Java programming language syntax.

    Numeric literals cannot be used as variable names, as Java variables cannot be declared with names that start with numbers.
    I said at the beginning I knew there were errors
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    there were errors
    If you want help with the errors, you need to copy the full text and post it here.


    Also check the code's formatting in your first post.
  12. #7
  13. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    java Code:
    int  = (int) a;


    this is not a statement. Code like, for example:

    java Code:
    int someVariable= (int)a;


    is a statement.

    Additionally,

    java Code:
    else if(encrypted);


    ends the statement and really messes up everything below it (a semi-colon is in a very wrong place).

    When the compiler tells you something like:

    Code:
    Encryption.java:27: error: 'else' without 'if'
            else
            ^
    pay attention to it. Look at a line or so before line 27 and see if you've got something messed up. When you're first starting if sometimes feels like the compiler is your worst enemy. But it's not - it communicates far better than most humans you'll interact with. Listen to it.
  14. #8
  15. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,540
    Rep Power
    1621

    Syntax errors


    Originally Posted by SmileyCraft
    I said at the beginning I knew there were errors
    Exactly for that reason I suggested you to first learn the basics of Java programming language syntax. When you first compile the codes, the compiler would have complained lots of errors in your codes, pin pointing the exact location where the error was found in the codes.

    Have you taken the initiative to find out what the error the compiler is complaining about and how to fix those statements so that they conform to the language syntax? Have you even tried correcting those errors yourself before you even ask?

    Syntax errors are easy to fix, as there are tonnes of online Java tutorials you can reference from.

    As NormR posted,
    If you want help with the errors, you need to copy the full text and post it here.
    There are so many syntax errors found in your codes, obviously you are not familiar with the Java language syntax. Again, strongly suggest that you go through more Java language tutorial to build a stronger foundation on basic Java language syntax before you write something more complicated.
    When the programming world turns decent, the real world will turn upside down.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by tvc3mye
    Exactly for that reason I suggested you to first learn the basics of Java programming language syntax. When you first compile the codes, the compiler would have complained lots of errors in your codes, pin pointing the exact location where the error was found in the codes.

    Have you taken the initiative to find out what the error the compiler is complaining about and how to fix those statements so that they conform to the language syntax? Have you even tried correcting those errors yourself before you even ask?

    Syntax errors are easy to fix, as there are tonnes of online Java tutorials you can reference from.

    As NormR posted,


    There are so many syntax errors found in your codes, obviously you are not familiar with the Java language syntax. Again, strongly suggest that you go through more Java language tutorial to build a stronger foundation on basic Java language syntax before you write something more complicated.
    I do know java, I'm taking a class at school, I knew there were errors, I was just putting that in for now.
  18. #10
  19. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,540
    Rep Power
    1621

    Encryption - algorithm?


    There are many algorithms available in cryptography. So, which algorithm are you planning to implement and specifically what help you need with encryption?
    When the programming world turns decent, the real world will turn upside down.

IMN logo majestic logo threadwatch logo seochat tools logo