I'm currently working on a program that encrypts (using Caesar cipher) a user input string by replacing specific letters with custom preset letters. For instance A = R, B = T, C = O, etc.
The current program:
using System;
class Program
{
static void Main(string[] args)
{
String decryptedInput = Console.ReadLine().ToUpper();
String encryptedOutput = decryptedInput.Replace("A", "R")
.Replace("B", "B")
.Replace("C", "T")
.Replace("D", "O")
.Replace("E", "P")
.Replace("F", "M")
.Replace("G", "Z")
.Replace("H", "S")
.Replace("I", "J")
.Replace("J", "K")
.Replace("K", "I")
.Replace("L", "Y")
.Replace("M", "P")
.Replace("N", "G")
.Replace("O", "L")
.Replace("P", "V")
.Replace("Q", "C")
.Replace("R", "X")
.Replace("S", "N")
.Replace("T", "E")
.Replace("U", "H")
.Replace("V", "F")
.Replace("P", "A")
.Replace("X", "U")
.Replace("Y", "Q")
.Replace("Z", "D");
Console.WriteLine(encryptedOutput);
Console.ReadKey();
}
}
I do get the output, but there are some encryption errors. The problem is, that since the lines of code run after each other, letters that are already converted are converted again.
For example: The letter "A" is encrypted/converted to "R". When the program gets to the letter "R", the letter is encrypted/converted again and ends up being "X", which gets converted to "U" later in the code. This happens to almost every letter and then I end up with an encrypted text, which I can never decrypt.
Is there any way to replace all of the letters at the same time, or would you simply recommend me using another function?