Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java
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.decrypt(cip);
	}
 
	// 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.in);
			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++){
		            if(j<=80){
		                if(plain[i]==chars[j]){
		                    plain[i] = chars[j+5];
		                    break;
		                }  
		            }//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.in);
			 System.out.println("To Decrypt plaintext enter 1");
			 int choice = in.nextInt();
			 if(choice == 1){
				 return cipher; 
			 }
			 else{
				 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];
                                break;
                            }
                                if(cipher1[i] == chars[j] && j<5){
                                    cipher1[i] = chars[81+j];
                                    break;
                      }  //End IF
                } //End nested for loop
            } //End of For loop
	    }
            cipher=String.valueOf(cipher1);
            System.out.println(" Plain text is '"+cipher+"'");  
			return cipher;
        }
}
 

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

1 solution

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

Solution 1

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','+','-','*','/','[',']','{','}','=','<','>','?','_'
 

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

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

  Print Answers RSS
0 OriginalGriff 420
1 CHill60 410
2 Sergey Alexandrovich Kryukov 384
3 PIEBALDconsult 245
4 BillWoodruff 243


Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 22 Mar 2012
Copyright © CodeProject, 1999-2014
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