Click here to Skip to main content
13,550,805 members
Rate this:
Please Sign up or sign in to vote.
See more:
Code is written for Caesar cipher. Encryption is correctly done. Problems are with decryption function.

For plaintext = VictorY or 56789..

encryption is correctly done but decryption is not correct..
Here's the full code
import java.util.Scanner; 
public class Caesar {

	public static void main(String[] args){
		String cip=Caesar.encrypt();

	// Caesar Encryption Function
		private static String encrypt() {
		char chars[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','!','@','#','$','%','^','&','(',')','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','+','-','*','/','[',']','{','}','=','<','>','?','_'};
	    String empty = "empty"; 
		    Scanner input = new Scanner(;
			System.out.println("Enter the plaintext"); 
		    String plainText = input.nextLine(); 
		    String cipher = null;
		    char[] plain = plainText.toCharArray(); 
		    for(int i = 0;i<plain.length;i++){>
		        for(int j = 0 ; j<85;j++){
		                    plain[i] = chars[j+5];
		            }//End nested If
		            else if(plain[i] == chars[j]){
		                plain[i] = chars [j-81];
		            }  //End else
		         } //End nested for loop 
		      } //End of For loop
		     cipher = String.valueOf(plain);
			 System.out.println(" cipher text is "+cipher);  
			 Scanner in = new Scanner(;
			 System.out.println("To Decrypt plaintext enter 1");
			 int choice = in.nextInt();
			 if(choice == 1){
				 return cipher; 
				 System.out.println("Thank you");}
			 return empty;
	// Caesar Decryption Function	
	private static String decrypt(String cip) {
		char chars[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','!','@','#','$','%','^','&','(',')','+','-,'*','/','[',']','{','}','=','<','>','?','_'};
	    String cipher = null;
	    String empty = "empty";
	    char[] cipher1 = cip.toCharArray();
	    if(cip .equals(empty)){
	    	System.out.println(" No text is Decrypted");
	    else{      //char[] cipher1 = cip.toCharArray();
                  for(int i = 0;i<cipher1.length;i++){>
                        for(int j = 0 ; j<85;j++){
                            if(j>=5 && cipher1[i]==chars[j]){
                                cipher1[i] = chars[j-5];
                                if(cipher1[i] == chars[j] && j<5){
                                    cipher1[i] = chars[81+j];
                      }  //End IF
                } //End nested for loop
            } //End of For loop
            System.out.println(" Plain text is '"+cipher+"'");  
			return cipher;

[edit]Code block added - OriginalGriff[/edit]
Posted 21-Mar-12 9:03am
Updated 21-Mar-12 9:42am
SAKryukov 21-Mar-12 15:47pm
Not clear what's the problem.
suganya madhesh 17-Jul-12 5:17am
Need a c program for caesar's sipher code in c with client server

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


both arrays were not in same order... when I arranged them in same order.. correct encryption and its correct decryption was done..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180515.1 | Last Updated 22 Mar 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100