SHOW:
|
|
- or go back to the newest paste.
| 1 | static void Main(string[] args) | |
| 2 | {
| |
| 3 | Console.WriteLine("Choose action:\n1.Encryption \n2.Decryption");
| |
| 4 | int choice = int.Parse(Console.ReadLine()); | |
| 5 | ||
| 6 | Console.WriteLine("Submit text");
| |
| 7 | string text = Console.ReadLine(); | |
| 8 | ||
| 9 | Console.WriteLine("Submit key (1-31)");
| |
| 10 | int key = int.Parse(Console.ReadLine()); | |
| 11 | if (key < 0) | |
| 12 | {
| |
| 13 | key = 0; | |
| 14 | } | |
| 15 | ||
| 16 | if (choice == 1) | |
| 17 | {
| |
| 18 | string encryptedText = Encryption(text, key); | |
| 19 | Console.WriteLine($"Encrypted text: {encryptedText}");
| |
| 20 | } | |
| 21 | else | |
| 22 | {
| |
| 23 | string decryptedText = Decryption(text, key); | |
| 24 | Console.WriteLine($"Decrypted text: {decryptedText}");
| |
| 25 | } | |
| 26 | ||
| 27 | Console.ReadLine(); | |
| 28 | ||
| 29 | } | |
| 30 | public static string Encryption(string text, int key) | |
| 31 | {
| |
| 32 | string alphabet = "abcdefghijklmnopqrstuvwxyz"; | |
| 33 | string encryptedText = ""; | |
| 34 | ||
| 35 | for (int counter = 0; counter < text.Length; counter++) | |
| 36 | {
| |
| 37 | ||
| 38 | char character = text[counter]; | |
| 39 | ||
| 40 | int characterNumber = alphabet.IndexOf(character); | |
| 41 | ||
| 42 | if (characterNumber == -1) | |
| 43 | {
| |
| 44 | encryptedText += character; | |
| 45 | } | |
| 46 | else | |
| 47 | {
| |
| 48 | ||
| 49 | int newCharacterNumber = (characterNumber + key) % alphabet.Length; | |
| 50 | char newCharacter = alphabet[newCharacterNumber]; | |
| 51 | encryptedText += newCharacter; | |
| 52 | } | |
| 53 | } | |
| 54 | ||
| 55 | return encryptedText; | |
| 56 | } | |
| 57 | ||
| 58 | public static string Decryption(string text, int key) | |
| 59 | {
| |
| 60 | string alphabet = "abcdefghijklmnopqrstuvwxyz"; | |
| 61 | string decryptedText = ""; | |
| 62 | ||
| 63 | for (int counter = 0; counter < text.Length; counter++) | |
| 64 | {
| |
| 65 | ||
| 66 | char character = text[counter]; | |
| 67 | ||
| 68 | int characterNumber = alphabet.IndexOf(character); | |
| 69 | ||
| 70 | if (characterNumber == -1) | |
| 71 | {
| |
| 72 | decryptedText += character; | |
| 73 | } | |
| 74 | else | |
| 75 | {
| |
| 76 | int newCharacterNumber = (characterNumber + (alphabet.Length - key)) % alphabet.Length; | |
| 77 | char newCharacter = alphabet[newCharacterNumber]; | |
| 78 | decryptedText += newCharacter; | |
| 79 | } | |
| 80 | } | |
| 81 | ||
| 82 | return decryptedText; | |
| 83 | } |