I’ve been working on encrypting user input using a custom encryption method I developed. However, I’m encountering an issue where the output is incorrect, and I can’t figure out how to resolve it.
For instance, when I try to encrypt simple input like “aaaa”, it works as expected, producing the output “bbbb”. But for input with more distant characters in the alphabet, such as “p” or words like “apple”, the encryption fails. For example, “apple” results in the incorrect output “bccef”.
static string Encrypt(string unconverted)
List<char> word = new List<char>();
List<char> encrypted = new List<char>();
char[] alphabet = { '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' };
string encrypted_word = "";
foreach (char c in unconverted)
{
word.Add(c);
}
foreach (char x in word)
{
int index = word.IndexOf(x);
encrypted.Add(alphabet[index += 1]);
}
foreach (char i in encrypted)
{
encrypted_word += i;
}
return encrypted_word;
1