Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Main(string[] args)
- {
- Console.WriteLine("Choose an action:\n1. Encryption \n2. Decryption");
- int choice = int.Parse(Console.ReadLine());
- Console.WriteLine("Enter text");
- string text = Console.ReadLine();
- Console.WriteLine("Enter the key (1-31)");
- int key = int.Parse(Console.ReadLine());
- if (key < 0)
- {
- key = 0;
- }
- if (choice == 1)
- {
- string encryptedText = Encrypt(text, key);
- Console.WriteLine($"Encrypted text: {encryptedText}");
- }
- else
- {
- string decryptedText = Decrypt(text, key);
- Console.WriteLine($"Decrypted text: {decryptedText}");
- }
- Console.ReadLine();
- }
- public static string Decrypt(string text, int shift)
- {
- string alphabet = "abcdefghijklmnoprstuwxyz";
- string plainText = "";
- for (int counter = 0; counter < text.Length; counter++)
- {
- char character = text[counter];
- int characterIndex = alphabet.IndexOf(character);
- if (characterIndex == -1)
- {
- plainText += character;
- }
- else
- {
- int newCharacterIndex = (characterIndex + (alphabet.Length - shift)) % alphabet.Length;
- char newCharacter = alphabet[newCharacterIndex];
- plainText += newCharacter;
- }
- }
- return plainText;
- }
- public static string Encrypt(string text, int shift) {...}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement