In this Caesar cipher i am obtaining the incorrect output for capital letters. The code is as follows:
public class CaesarCipherCaseSensitive
{
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
public static String encrypt(String pt, int shiftKey)
{
String ct = "";
for (int i = 0; i < pt.length(); i++)
{
int charPosition = ALPHABET.indexOf(pt.charAt(i));
int keyVal = (shiftKey + charPosition) % 26;
char replaceVal = ALPHABET.charAt(keyVal);
ct += replaceVal;
}
return ct;
}
public static String decrypt(String ct, int shiftKey)
{
String pt = "";
for (int i = 0; i < ct.length(); i++)
{
int charPosition = ALPHABET.indexOf(ct.charAt(i));
int keyVal = (charPosition - shiftKey) % 26;
if (keyVal < 0)
{
keyVal = ALPHABET.length() + keyVal;
}
char replaceVal = ALPHABET.charAt(keyVal);
pt += replaceVal;
}
return pt;
}
public static void main(String[] args)
{
String message1 = "ABCDEFGHIJKLMOPQRSTUVWXYZ";
System.out.println(encrypt(message1, 3));
System.out.println(decrypt(encrypt(message1, 3), 3));
System.out.println(encrypt(message1.toLowerCase(),5));
System.out.println(decrypt(encrypt(message1.toLowerCase(),5),5));
}
}
The output looks like: ccccccccccccccccccccccccc zzzzzzzzzzzzzzzzzzzzzzzzz fghijklmnopqrtuvwxyzabcde abcdefghijklmopqrstuvwxyz
Desired output is: DEFGHIJKLMOPQRSTUVWXYZABC ABCDEFGHIJKLMOPQRSTUVWXYZ fghijklmnopqrtuvwxyzabcde abcdefghijklmopqrstuvwxyz
The problem is you are passing an upper case letter to the method indexOf()
, which checks ALPHABET
but doesn't find it, so it returns -1. You need to either transform the upper case letter to lower case, or add an upper case alphabet.
My solution, transforming the upper case letter to a lower case letter and then encrypting/decrypting is:
public class CaesarCipherCaseSensitive{
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
public static String encrypt(String pt, int shiftKey){
String ct = "";
for (int i = 0; i < pt.length(); i++){
char letter = pt.charAt(i);
boolean upperCase = false;
if((int)letter < 91){
letter = (char)((int)letter + 32);
upperCase = true;
}
int charPosition = ALPHABET.indexOf(letter);
int keyVal = (shiftKey + charPosition) % 26;
char replaceVal = ALPHABET.charAt(keyVal);
if(upperCase){
replaceVal = (char)((int)replaceVal - 32);
}
ct += replaceVal;
}
return ct;
}
public static String decrypt(String ct, int shiftKey){
String pt = "";
for (int i = 0; i < ct.length(); i++){
char letter = ct.charAt(i);
boolean upperCase = false;
if((int)letter < 91){
letter = (char)((int)letter + 32);
upperCase = true;
}
int charPosition = ALPHABET.indexOf(letter);
int keyVal = (charPosition - shiftKey) % 26;
if (keyVal < 0){
keyVal = ALPHABET.length() + keyVal;
}
char replaceVal = ALPHABET.charAt(keyVal);
if(upperCase){
replaceVal = (char)((int)replaceVal - 32);
}
pt += replaceVal;
}
return pt;
}
public static void main(String[] args){
String message1 = "ABCDEFGHIJKLMOPQRSTUVWXYZ";
System.out.println(encrypt(message1, 3));
System.out.println(decrypt(encrypt(message1, 3), 3));
System.out.println(encrypt(message1.toLowerCase(),5));
System.out.println(decrypt(encrypt(message1.toLowerCase(),5),5));
}
}
Here's a solution,
private static final List<String> upperAlphabets = Arrays.asList(
"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");
private static final List<String> upperAlphabetsRev = Arrays.asList(
"Z", "Y", "X", "W", "V", "U", "T",
"S", "R", "Q", "P", "O", "N", "M",
"L", "K", "J", "I", "H", "G", "F",
"E", "D", "C", "B", "A");
private static List<String> lowerAlphabets = new ArrayList<>();
private static List<String> lowerAlphabetsRev = new ArrayList<>();
private static void init() {
for (String c : upperAlphabets) {
lowerAlphabets.add(c.toLowerCase());
}
for (String c : upperAlphabetsRev) {
lowerAlphabetsRev.add(c.toLowerCase());
}
}
public static String encryptCaesar(String toEncrypt, int shiftTo) throws IllegalArgumentException {
if (shiftTo >= 26 || shiftTo < 0) {
throw new IllegalArgumentException("shiftTo should be lesser than 26 and greater than 0");
}
char[] encryptedString = new char[toEncrypt.length()];
int position = -1, pos, newPos;
boolean upper;
for (char c : toEncrypt.toCharArray()) {
position++;
pos = upperAlphabets.indexOf(String.valueOf(c));
if (pos == -1) {
upper = false;
pos = lowerAlphabets.indexOf(String.valueOf(c));
} else {
upper = true;
}
newPos = (pos + shiftTo) % 26;
encryptedString[position] = upper ? upperAlphabets.get(newPos).charAt(0) : lowerAlphabets.get(newPos).charAt(0);
}
return String.valueOf(encryptedString);
}
public static String decryptCaesar(String toDecrypt, int shiftTo) {
if (shiftTo >= 26 || shiftTo < 0) {
throw new IllegalArgumentException("shiftTo should be lesser than 26 and greater than 0");
}
char[] decryptedString = new char[toDecrypt.length()];
int position = -1, pos, newPos;
boolean upper;
for (char c : toDecrypt.toCharArray()) {
position++;
pos = upperAlphabetsRev.indexOf(String.valueOf(c));
if (pos == -1) {
upper = false;
pos = lowerAlphabetsRev.indexOf(String.valueOf(c));
} else {
upper = true;
}
newPos = (pos + shiftTo) % 26;
decryptedString[position] = upper ? upperAlphabetsRev.get(newPos).charAt(0) : lowerAlphabetsRev.get(newPos).charAt(0);
}
return String.valueOf(decryptedString);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.