# Ceasar Cypher: Decryption Key Index Outside the bounds

When using a key to decrypt, i was told to use the modulo operator to “wrap around” the alphabet.

I understand how and why this works but i can’t say the same for the decryption process.

``````//decrypt
char pos = encryptedMessage[i];
int alphabetPos = Array.IndexOf(alphabet, decryptedMessage[i]);
int newLetterPos = (alphabetPos - key) % 26;
char decryptedChar = alphabet[newLetterPos];
decryptedMessage[i] = decryptedChar;
``````

This obviously gets me an index out of bounds.
I get why this doesn’t work the same way as with adding the key.
But i can’t get to a solution for the life of me.

I would appreciate a little pointer!

@bit1906038192 Hello. I saw your topic when I had searched for the solution of the same problem. I found it. I would like to share, maybe in the future, someone will need it.

``````static string CaeserChipherDecrpt(int num, params char[] msg)
{
char[] alphabet = new char[] { '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' };

// Declaring an array for dencryption
char[] decryptedMessage = new char[msg.Length];

// Creating an loop that loops through each character of argument msg
for (int i = 0; i < msg.Length; i++)
{
// Checking whether decryptedMessage includes any symbols
// If no symbols
if (Char.IsLetter(msg[i]))
{
// Holding every letter of argument
char tempLetter = msg[i];

// Finding the index of the character in the alphabet array
int letterPosition = Array.IndexOf(alphabet, tempLetter);

// Implementing the key
//
letterPosition = (letterPosition + (26 - num)) % alphabet.Length;

// Holding the new encrypted letter in a char variable
char decodedLetter = alphabet[letterPosition];

// Adding the encrypted character to the encryptedMessage
decryptedMessage[i] = decodedLetter;
}
}
// Converting the encryptedMessage to a readable string
string finalDecryptedMsg = string.Join("", decryptedMessage);

return finalDecryptedMsg;
}
``````