0% found this document useful (0 votes)
8 views3 pages

Caesar Cipher Program in Java

The document contains a Java program that implements a Caesar cipher for encrypting and decrypting messages. Users can choose to encrypt or decrypt a message by providing a plaintext and a shift value. The program continues to prompt the user for actions until they choose to exit.

Uploaded by

dasmitag147
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Caesar Cipher Program in Java

The document contains a Java program that implements a Caesar cipher for encrypting and decrypting messages. Users can choose to encrypt or decrypt a message by providing a plaintext and a shift value. The program continues to prompt the user for actions until they choose to exit.

Uploaded by

dasmitag147
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

scanner*;

public class CaesarCipherProgram {

public static void main(String args[]) {

Scanner sc = new Scanner([Link]);

String k = "y";

while ([Link]("y") || [Link]("Y")) {

String plaintext = "";

int shift = 0;

[Link]("1. Encrypt Message \n2. Decrypt Message ");

[Link]("\nEnter your Choice : ");

int ch = [Link]();

[Link]("Enter Message : ");

plaintext = [Link]();

[Link]("Enter the value by which each character in the plaintext message gets
shifted : ");

shift = [Link]();

switch (ch) {

case 1:

String ciphertext = encrypt(plaintext, shift);

[Link]("ciphertext : " + ciphertext);

break;

case 2:

String ptext = decrypt(plaintext, shift);

[Link]("plaintext : " + ptext);

break;

default:
[Link]("Option not Selected.. Please Select an option");

[Link]("Do you want to continue? [y/n] : ");

k = [Link]();

private static String encrypt(String plaintext, int shift) {

String ciphertext = "";

for (int i = 0; i < [Link](); i++) {

char alphabet = [Link](i);

if (alphabet >= 'a' && alphabet <= 'z') {

alphabet = (char) (alphabet + shift);

if (alphabet > 'z') {

alphabet = (char) (alphabet + 'a' - 'z' - 1);

ciphertext += alphabet;

else if (alphabet >= 'A' && alphabet <= 'Z') {

alphabet = (char) (alphabet + shift);

if (alphabet > 'Z') {

alphabet = (char) (alphabet + 'A' - 'Z' - 1);

ciphertext += alphabet;

else {

ciphertext += alphabet;

}
return ciphertext;

private static String decrypt(String ciphertext, int shift) {

String decryptMessage = "";

for (int i = 0; i < [Link](); i++) {

char alphabet = [Link](i);

if (alphabet >= 'a' && alphabet <= 'z') {

alphabet = (char) (alphabet - shift);

if (alphabet < 'a') {

alphabet = (char) (alphabet - 'a' + 'z' + 1);

decryptMessage += alphabet;

else if (alphabet >= 'A' && alphabet <= 'Z') {

alphabet = (char) (alphabet - shift);

if (alphabet < 'A') {

alphabet = (char) (alphabet - 'A' + 'Z' + 1);

decryptMessage += alphabet;

else {

decryptMessage += alphabet;

return decryptMessage;

Common questions

Powered by AI

Potential improvements include implementing a validation mechanism for user input to handle non-integer and out-of-range shift values more gracefully. For security, increasing the complexity by allowing shifts to exceed 26 or incorporating more sophisticated encryption, like variable shifts for different characters, could enhance security. Additionally, providing clearer output messages and options could improve usability .

To decrypt a message that was encrypted with a Caesar Cipher shift of 5, the user should use a shift of -5 for decryption. This effectively reverses the effect of the original encryption shift and restores the message to its original form .

A limitation is that the Caesar Cipher program processes uppercase and lowercase letters separately without any normalization, which means that the character's case is preserved. This can lead to a lack of uniformity in encrypted messages if the input text contains a mix of uppercase and lowercase letters . Additionally, since the shift logic wraps each case separately ('z' wraps to 'a' and 'Z' wraps to 'A'), it may not align with some use cases that require a uniform representation even after shifts.

The program uses a loop to continuously prompt the user for input to allow multiple encryptions and decryptions within a single execution session. This loop is controlled by a string variable 'k', which asks the user whether they want to continue after each operation. If the user inputs 'y' or 'Y', the loop continues; otherwise, it terminates .

The program constructs encrypted and decrypted messages by iterating through each character of the input string and applying the Caesar Cipher shift to alphabetic characters. Results are concatenated to form the output string using string addition (+=), which accumulates characters in sequence to build the final result .

Using a static shift value in the Caesar Cipher significantly weakens cryptographic security because it results in a simple substitution cipher easily broken by frequency analysis or brute force, considering only 25 possible shifts (excluding identity). The predictability of such a cipher makes it unsuitable for protecting sensitive information in any secure communication system .

The Caesar Cipher Program leaves non-alphabetic characters unchanged during both encryption and decryption. This is evident in the else clause of both the encrypt and decrypt methods where non-alphabetic characters are appended to the resulting string without any transformation .

The program ensures correct wrapping of the Caesar Cipher shift by adjusting the character value when it exceeds the range of the alphabet. For lowercase letters, if the shifted result is greater than 'z', it wraps by subtracting 'z' and adding 'a'. Similarly, for uppercase letters, if the shifted value is greater than 'Z', it wraps by subtracting 'Z' and adding 'A'. This logic is mirrored for decryption, ensuring characters wrap around correctly to stay within their respective alphabetic ranges .

Character type checks determine how the shift is applied to maintain the case of letters. The program separately checks and processes lowercase (a-z) and uppercase (A-Z) characters to ensure the shift respects the case boundaries, enabling accurate wrapping without changing character cases. This ensures the transformed characters remain within their original alphabetic case range during encryption and decryption .

The program handles the user's selection for encryption or decryption operations through a numeric choice input (1 for encryption, 2 for decryption). A potential risk is that incorrect or non-numeric input can cause runtime errors or unexpected behavior because the input is directly processed using nextInt(), which doesn't handle input validation gracefully .

You might also like