1 year ago
#384306
Eduardo Waller
I'm using Caesar crypt and I'm able to encrypt my code but am not sure on how I'd decrypt it
I'm using Caesar crypt to encrypt a string sentence and I'm able to encrypt my code but am not sure on how I'd decrypt it. I have my shift pattern set and am able to print to my screen, but like I said I'm still unable to decrypt
import java.util.*;
public class CaesarCipher {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println(" Input the plaintext message : ");
String plaintext = sc.nextLine();
System.out.println("Enter the value by which each character in the plaintext message gets shifted");
int shift = sc.nextInt();
String ciphertext = "";
char alphabet;
for(int i=0; i < plaintext.length();i++)
{
// Shift one character at a time
alphabet = plaintext.charAt(i);
// if alphabet lies between a and z
if(alphabet >= 'a' && alphabet <= 'z')
{
// shift alphabet
alphabet = (char) (alphabet + shift);
// if shift alphabet greater than 'z'
if(alphabet > 'z') {
// reshift to starting position
alphabet = (char) (alphabet+'a'-'z'-1);
}
ciphertext = ciphertext + alphabet;
}
// if alphabet lies between 'A'and 'Z'
else if(alphabet >= 'A' && alphabet <= 'Z') {
// shift alphabet
alphabet = (char) (alphabet + shift);
// if shift alphabet greater than 'Z'
if(alphabet > 'Z') {
//reshift to starting position
alphabet = (char) (alphabet+'A'-'Z'-1);
}
ciphertext = ciphertext + alphabet;
}
else {
ciphertext = ciphertext + alphabet;
}
}
System.out.println(" ciphertext : " + ciphertext);
}
}
java
encryption
caesar-cipher
crypt
0 Answers
Your Answer