In: Computer Science
Can you fix the code and comment the fix
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -39 at CaesarCipher.encrypt(CaesarCipher.java:28) at CaesarCipher.main(CaesarCipher.java:52)
public class CaesarCipher{
char[] encoder = new char[52];
char[] decoder = new char[52];
public CaesarCipher(int rotation)
{
for(int k=0 ; k < 26 ;
k++)
{
encoder[k] = (char) ('A' + (k + rotation) % 26);
decoder[k] = (char) ('A' + (k - rotation + 26) % 26);
}
for(int j = 26 ; j < 52 ;
j++ )
{
encoder[j] = (char)('a' + (j + rotation) % 26);
decoder[j] = (char)('a' + (j - rotation) % 26);
}
}
public String encrypt(String message)
{
char[] msg =
message.toCharArray();
for(int i = 0 ; i
< msg.length ; i++){
if(Character.isUpperCase(msg[i])){
msg[i] = encoder[msg[i] - 'A'];
}
else{
int n = msg[i] - 'a' ;
msg[i] = encoder[26 + n];
}
}
return new
String(msg);
}
public String decrypt(String secret)
{
char[] msg =
secret.toCharArray();
for(int i = 0 ; i
< msg.length ; i++){
if(Character.isUpperCase(msg[i])){
msg[i] = decoder[msg[i] - 'A'];
}
else{
int n = msg[i] - 'a';
msg[i] = decoder[26 + n];
}
}
return new
String(msg);
}
public static void main(String[] args) {
CaesarCipher cipher = new CaesarCipher(3);
String message = "There Is an APPle";
String coded = cipher.encrypt(message);
System.out.println("Secret: " + coded);
String answer = cipher.decrypt(coded);
System.out.println("Message: " +
answer);
}
}
`Hey,
Note: If you have any queries related to the answer please do comment. I would be very happy to resolve all your queries.
Your code was not accounting for space actually caesar cipher rotates the alphabets not the spaces
Below is the updated
public class CaesarCipher{
char[] encoder = new char[52];
char[] decoder = new char[52];
public CaesarCipher(int rotation)
{
for(int k=0 ; k < 26 ; k++)
{
encoder[k] = (char) ('A' + (k + rotation) % 26);
decoder[k] = (char) ('A' + (k - rotation + 26) % 26);
}
for(int j = 26 ; j < 52 ; j++ )
{
encoder[j] = (char)('a' + (j + rotation) % 26);
decoder[j] = (char)('a' + (j - rotation) % 26);
}
}
public String encrypt(String message) {
char[] msg = message.toCharArray();
for(int i = 0 ; i < msg.length ; i++){
if(Character.isUpperCase(msg[i])){
msg[i] = encoder[msg[i] - 'A'];
}
else if(Character.isLowerCase(msg[i])){
int n = msg[i] - 'a' ;
msg[i] = encoder[26 + n];
}
}
return new String(msg);
}
public String decrypt(String secret) {
char[] msg = secret.toCharArray();
for(int i = 0 ; i < msg.length ; i++){
if(Character.isUpperCase(msg[i])){
msg[i] = decoder[msg[i] - 'A'];
}
else if(Character.isLowerCase(msg[i])){
int n = msg[i] - 'a';
msg[i] = decoder[26 + n];
}
}
return new String(msg);
}
public static void main(String[] args) {
CaesarCipher cipher = new CaesarCipher(3);
String message = "There Is an APPle";
String coded = cipher.encrypt(message);
System.out.println("Secret: " + coded);
String answer = cipher.decrypt(coded);
System.out.println("Message: " + answer);
}
}
Kindly revert for any queries
Thanks.