Write a program to encrypt the text with number of alphabet shift (CAESAR SHIFT CIPHER) using JAVA.

  The following code will help you to encrypt the text using Caesar shift cipher.

Table of Contents

Java Program Code

   
 

/*;==========================================
; Title: Write a program to encrypt the text with number of alphabet shift (CAESAR SHIFT CIPHER) using JAVA. 
; Author: codenaive Santosh Kumar  <admin@codenaive.com< <s.8506.a@gmail.com<
; Date:   18 Dec 2021
;==========================================*/
class TextMessage {
	public String message;
	public void addMessage(String msg){
		message=msg;
	}
}

class PlainTextMessage extends TextMessage{
	
	public void addMessage(String msg){
		message=msg;
	}
	public String getMessage(){
		return message;
	}
}
class EncryptedPlainText extends TextMessage {
	
	public void addMessage(String plain_message,int number_of_Shift){
		String[] arr = plain_message.split("");
		String enc="";
		for(int i=0; i<plain_message.length();i++){

			if(plain_message.charAt(i)=='v'){
				if(plain_message.charAt(i)==' '){
					enc+=" ";
				}
				else{
					enc+="a";
				}
			}
			else if(plain_message.charAt(i)=='w'){
				if(plain_message.charAt(i)==' '){
					enc+=" ";
				}
				else{
					enc+="b";
				}
			}
			else if(plain_message.charAt(i)=='x'){
				if(plain_message.charAt(i)==' '){
					enc+=" ";
				}
				else{
					enc+="c";
				}
			}
			else if(plain_message.charAt(i)=='y'){
				if(plain_message.charAt(i)==' '){
					enc+=" ";
				}
				else{
					enc+="d";
				}
			}
			else if(plain_message.charAt(i)=='z'){
				if(plain_message.charAt(i)==' '){
					enc+=" ";
				}
				else{
					enc+="e";
				}
			}
			else{
				if(plain_message.charAt(i)==' '){
					enc+=" ";
				}
				else{
					enc+=(char)((int)(plain_message.charAt(i))+number_of_Shift);
				}
			}
			
		}
		message=enc;
			
	}
	public String getMessage(){
		return message;
	}
}

public class CaesarShiftEncrypt{
	
	public static void main(String []args){
		PlainTextMessage plainLogger= new PlainTextMessage();
		EncryptedPlainText encryptLogger = new EncryptedPlainText();
	    String  message = "The website name is code naive";
		int number_of_Shift = 1;
		plainLogger.addMessage(message);
		encryptLogger.addMessage(message,number_of_Shift);
		
		System.out.println("Plain Text: "+plainLogger.getMessage());
		System.out.println("Cipher Text: "+encryptLogger.getMessage());
	}
	
}

Output

C:\CodeNaive>javac CaesarShiftEncrypt.java

C:\CodeNaive>java CaesarShiftEncrypt
Plain Text: The website name is code naive
Cipher Text: Uif bfctjuf obnf jt dpef objaf

Leave a Reply

Your email address will not be published. Required fields are marked *