Caesar Java Program: Writing Code & Decrypting Messages

In summary, the conversation is about a code for encrypting and decrypting messages using a Caesar cipher. The code provided works, but there is some confusion about the hint and how to handle letters that go beyond the alphabet. The speaker suggests using ASCII characters and provides an alternative code that takes into account uppercase and lowercase letters. They also mention a possible alternative for the nested if statement.
  • #1
Hiche
84
0

Homework Statement



1z3vywx.png


Homework Equations


The Attempt at a Solution



Code:
public class Caesar 
{
	public static void main(String[] args)
	{
		int k = Integer.parseInt(args[0]);
		char c;
		
		String strMsg = StdIn.readString();
		System.out.print("Encrypted sentence is: " );
		
		for (int i = 0; i < strMsg.length(); i++)
		{
			c = strMsg.charAt(i); // get characters 
			c = (char) (c + k); // shift characters of string k times
			System.out.print(c);
			
		}
		System.out.println();
	}
}

Okay, this works, well almost. Suppose that the shifting value is k = 3, then it will look like the original/caesar in the above image. If I want to decrypt (or encrypt? You get the point.) the message Wxyz, then the output will result in characters different than Zabc. Basically, the 'Hint' part is where I am having trouble making.

And is the code I wrote equivalent to the question/problem asked? I'm not positive I should input a String.
 
Last edited:
Physics news on Phys.org
  • #2
for example, if a correspond to 1, b to 2 etc...
then you can write up a statement saying if c + K >26 then the character becomes ... [what do you do when you are trying to get the "28'th" letter when you actually need the 2nd?]

but this poses a problem on case sensitivity, judging from the difficulty of this exercise you probably don't need to worry about it
 
  • #3
Okay, so I was thinking of another way, since we must study both cases(lower- and upper-case). I used the ASCII character to decimal table to do my code..

Code:
public class Caesar 
{
	public static void main(String[] args)
	{
		int k = Integer.parseInt(args[0]); // value to shift letters
		
		System.out.print("Enter a message to encrypt: ");
		
		while (!StdIn.isEmpty())
		{
			String strMsg = StdIn.readString();
			for (int i = 0; i < strMsg.length(); i++)
			{
				char ltr = strMsg.charAt(i);
				if (ltr >= 'A' && ltr <= 'Z') // if letters are upper-cased
				{
					if((int) ltr <= 90 - k)
					{
						ltr = (char) (ltr + k);
						System.out.print(ltr);
					}
					else
					{
						ltr = (char) (((int) ltr + k) - 26);
						System.out.print(ltr);
					}
				}
				else if (ltr >= 'a' && ltr <= 'z') // if letters are lower-cased
				{
					if((int) ltr <= 122 - k)
					{
						ltr = (char) (ltr + k);
						System.out.print(ltr);
					}
					else
					{
						ltr = (char) (((int) ltr + k) - 26);
						System.out.print(ltr);
					}
				}
				else if (!Character.isLetter(ltr)) // if character not letter
				{
					System.out.print(ltr); // print the character as is
				}
			}
			System.out.print(" ");
		}
	}
}

In the nested ifs, is there another way for the expression? I don't know if we can use that, but it worked perfectly fine.
 

What is the Caesar Java Program and how does it work?

The Caesar Java Program is a computer program that uses the Caesar cipher to encrypt and decrypt messages. It works by shifting each letter in a message by a certain number of places in the alphabet, according to the chosen key. For example, with a key of 3, the letter "A" would become "D", "B" would become "E", and so on.

What is the purpose of the Caesar Java Program?

The purpose of the Caesar Java Program is to provide a tool for encrypting and decrypting messages using the Caesar cipher, a simple and historical method of cryptography. It can be used for educational purposes or for fun, but it should not be relied on for secure communication as it is easily cracked.

How do I write code for the Caesar Java Program?

To write code for the Caesar Java Program, you will need to have a basic understanding of the Java programming language. You can start by defining variables for the message, key, and alphabet, and then writing a loop to shift each letter in the message according to the key. It is recommended to use a Java IDE (Integrated Development Environment) for easier coding.

How can I decrypt a message using the Caesar Java Program?

To decrypt a message using the Caesar Java Program, you will need to know the key that was used to encrypt the message. Input the encrypted message and the key into the program, and it will shift each letter in the message back to its original position in the alphabet, revealing the decrypted message.

Is the Caesar Java Program a secure method of encryption?

No, the Caesar cipher is not a secure method of encryption as it is easily cracked through frequency analysis. It is a very basic form of cryptography and should not be relied on for secure communication. It is recommended to use more advanced and secure encryption methods for sensitive information.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
942
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top