0% found this document useful (0 votes)
3 views

NIs code

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

NIs code

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

scanner*;

public class CaesarCipherProgram {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

String k = "y";

while (k.equals("y") || k.equals("Y")) {

String plaintext = "";

int shift = 0;

System.out.println("1. Encrypt Message \n2. Decrypt Message ");

System.out.println("\nEnter your Choice : ");

int ch = sc.nextInt();

System.out.println("Enter Message : ");

plaintext = sc.next();

System.out.println("Enter the value by which each character in the plaintext message gets
shifted : ");

shift = sc.nextInt();

switch (ch) {

case 1:

String ciphertext = encrypt(plaintext, shift);

System.out.println("ciphertext : " + ciphertext);

break;

case 2:

String ptext = decrypt(plaintext, shift);

System.out.println("plaintext : " + ptext);

break;

default:
System.out.println("Option not Selected.. Please Select an option");

System.out.println("Do you want to continue? [y/n] : ");

k = sc.next();

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

String ciphertext = "";

for (int i = 0; i < plaintext.length(); i++) {

char alphabet = plaintext.charAt(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 < ciphertext.length(); i++) {

char alphabet = ciphertext.charAt(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;

You might also like