So for school I have to write a program that implements a Caesar Cipher. I was doing this and it was going well, however, the encryption method itself is not working out how I planned.
For example, let's say we're going to encrypt the string "abc"
It goes in as:
abc
It comes out as:
bbc
Now, I know exactly which lines of code are causing this issue. The only problem is I don't exactly know how to fix it.
Here is my code:
public static void encrypt(String toencrypt)
{
unencrypted = toencrypt.toLowerCase();
char[] sEn = unencrypted.toCharArray();
char[] enEd = new char[sEn.length];
if(toencrypt.length() > 0)
{
for(int i = 0; i < ALPHABET.length; i++)
{
for(int j = 0; j < sEn.length; j++)
{
if(sEn[j] == ALPHABET[i])
{
sEn[j] = CIPHERBET[i];
}
//Below is the 'if' statement causing the issue
if(enEd[j] == 0)
{
enEd[j] = sEn[j];
}
}
}
String bts = new String(enEd);
encrypted = bts;
System.out.println("The encrypted message is: " + encrypted);
}
else
{
System.out.println("Please enter a string: ");
}
}
In case you are confused on any of the data types that are not explicitly stated in the method, here they are:
- ALPHABET is a character array
- CIPHERBET is also a character array
I will extremely appreciative of any and all help I can receive.
Thank you in advance.