Matt and Henrik have the right idea.
Write a shift function to do all the magic.
public class Shift
{
static char shift(char letter, int distance)
{
if (letter >= 'A' && letter <= 'Z')
{
letter -= 'A';
letter += distance;
letter %= 26;
letter += 'A';
}
else if (letter >= 'a' && letter <= 'z')
{
letter -= 'a';
letter += distance;
letter %= 26;
letter += 'a';
}
return letter;
}
public static string Encrypt(string text, int distance)
{
StringBuilder sb = new StringBuilder(text);
for (int i = 0; i < sb.Length; i++)
sb[i] = shift(sb[i], distance);
return sb.ToString();
}
public static string Decrypt(string text, int distance)
{
distance %= 26;
return Encrypt(text, 26 - distance);
}
}
When using Encrypt with Decrypt, the same shift value must be used. The shift value is assumed to be positive.
Bonus link:
https://en.wikipedia.org/wiki/ROT13[
^]