static void Main(string[] args)
Console.Write("szyfr peppaxa Wpisz tekst: ");
string inputText = Console.ReadLine().ToLower();
Console.Write("Wpisz kod (z małych liter): ");
string key = Console.ReadLine().ToLower();
if (string.IsNullOrEmpty(key) || !key.All(char.IsLower))
Console.WriteLine("Error: klucz musi składać się z małych liter.");
Console.Write("Wybierz (e) zaszyfrować lub (d) rozszyfrować: ");
char choice = Console.ReadLine()[0];
while (choice != 'e' && choice != 'd')
Console.Write("Error: wybierz 'e' (zaszyfrować) lub 'd' (rozszyfrować): ");
choice = Console.ReadLine()[0];
string result = choice == 'e'
? Encrypt(inputText, key)
: Decrypt(inputText, key);
Console.WriteLine($"\nResult: {result}");
static string Encrypt(string text, string key)
return ProcessText(text, key, true);
static string Decrypt(string text, string key)
return ProcessText(text, key, false);
static string ProcessText(string text, string key, bool encrypt)
StringBuilder output = new StringBuilder();
int keyLength = key.Length;
for (int i = 0; i < text.Length; i++)
char currentChar = text[i];
if (char.IsLetter(currentChar))
shift = key[i] - 'a' + 1;
shift = key[keyLength - 1] - 'a' + 1;
shift = encrypt ? shift : -shift;
char shiftedChar = (char)((((currentChar - 'a') + shift + 26) % 26) + 'a');
output.Append(shiftedChar);
output.Append(currentChar);
return output.ToString();