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