Click here to Skip to main content
15,885,817 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
import java.io.BufferedReader;

import java.io.InputStreamReader;
import java.util.Scanner;
public class Ceasercipher
{
static Scanner sc=new Scanner(System.in);
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[]) throws Exception
{
System.out.println("Enter the String");
String str=br.readLine();
System.out.println("/n Enter the key");
int key=sc.nextInt();
String encrypted=encrypt(str,key);
System.out.println("/n encrypted string"+encrypted);
String decrypted=decrypt(encrypted,key);
System.out.println("/n decrypted string"+decrypted);
System.out.println("/n");
}
public static String encrypt(String str, int key)
{
String encrypted=" ";
for (int i=0;i<str.length();i++)
{
int c="str.charAt(i);
if(Character.isUpperCase(c))
{
c=c+(key%26);
if(c">'Z')
c=c-26;
}
else if(Character.isLowerCase(c))
{
c=c+(key%26);
if(c>'z')
c=c-26;
}
encrypted+=(char)c;
}
return encrypted;
}
public static String decrypt(String encrypted,int key)
{
String decrypted=" ";
for (int i=0;i<encrypted.length();i++)
{
int c="encrypted.charAt(i);
if(Character.isUpperCase(c))
{
c=c-(key%26);
if(c">'A')
c=c+26;
}
else if(Character.isLowerCase(c))
{
c=c-(key%26);
if(c<'a')
c=c+26;
}
decrypted+=(char)c;
}
return decrypted;
}
}


What I have tried:

i am trying to encrypt and decrypt the string
Posted
Updated 4-Mar-20 21:29pm
v2

First off, indent your code: it makes it a whole load more readable, and the more readable it is, the more reliable it is in general - because it's harder to make "silly mistakes" and have them not be spotted.

For example, if you indented your code, you'd see the spurious double quotes in your code that effectively "comment out" chunks of your code:
Java
int c="str.charAt(i);
if(Character.isUpperCase(c))
{
c=c+(key%26);
if(c">'Z')
c=c-26;
}

Second, how do you know that "Encryption is working and decryption is not working"? How have you tested it? OR did you assume that because the encryption generates hard-to-read output it must be correct?

Third, that's not encryption: that's translation; a variant of the Caesar Cypher that can be "cracked" in minutes by almost anyone. Heck they publish whole magazines every month so little old ladies can do it for fun ...

So start with a small known string: "ABCDEF" and manually translate that. Then manually decrypt it. Try it with a variety of "key" values. When that produces the same string as the original, then run the same string through your "encryption" method. Compare the results. Are the absolutely identical? If so, encryption is possibly OK. If not, why not? How are they different?

When that is right, run your manually encrypted string through your "decryption" method. Do you get the original back? If so, try using both methods together. If you don't, why not?

If all that works, run the string through encryption, then pas the result to decryption, and compare the output to the original input. If they are the same, try again with longer, more complex strings. If not, why not?

Whenever you reach a "why not?" reach for the debugger and start using it to look at exactly what your code is doing while it is running. Time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Try to replace
Java
if(c > 'A')
with
Java
if(c < 'A')
You should aim at learning debugging, with it you would have spotted the issue yourself in much fewer time than posting and waiting for an answer.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900