I'm trying to decrypt a cipher encrytion using a java method however my code doesn't seem to be returning correctly. I have tried to reverse the encryption process but I can't see what I'm doing wrong. Apologies, I hope this isn't a stupid question.
public void decrypt()
{
String cipherText = this.message;
String key = this.KEY;
String alphabet = "abcdefghijklmnopqrstuvwxyz";
int alphabetSize = alphabet.length();
int textSize = cipherText.length();
int keySize = key.length();
StringBuilder decryptedText = new StringBuilder(textSize);
for (int i = 0; i < textSize; i++)
{
char encyrptChar = cipherText.charAt(i); // get the current character to be shifted
char keyChar = key.charAt(i % keySize); // use key again if the end is reached
int plainPos = alphabet.indexOf(encyrptChar); // plain character's position in alphabet string
// decrypt the input text
int keyPos = alphabet.indexOf(keyChar); // key character's position in alphabet
int shiftedPos = plainPos-keyPos;
shiftedPos += alphabetSize;
decryptedText.append(alphabet.charAt(shiftedPos));
}
this.message = decryptedText.toString();
}