November 28th, 2012, 04:08 PM
-
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
November 28th, 2012, 04:28 PM
-
Please edit your post and wrap the code in code tags to preserve formatting.
November 29th, 2012, 12:45 AM
-
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.

November 29th, 2012, 07:36 AM
-
The code is now all on one line. Can you format the code so each statement is on its own line?
November 29th, 2012, 06:58 PM
-
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
November 29th, 2012, 07:01 PM
-
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.
November 29th, 2012, 10:07 PM
-
this is not a statement. Code like, for example:
java Code:
int someVariable= (int)a;
is a statement.
Additionally,
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.
December 3rd, 2012, 02:22 AM
-
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.

December 3rd, 2012, 03:06 PM
-
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.
December 4th, 2012, 02:01 AM
-
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.
