Advertisement
ada1711

Untitled

Aug 29th, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. static void Main(string[] args)
  2. {
  3. Console.WriteLine("Choose an action:\n1. Encryption \n2. Decryption");
  4. int choice = int.Parse(Console.ReadLine());
  5.  
  6. Console.WriteLine("Enter text");
  7. string text = Console.ReadLine();
  8.  
  9. Console.WriteLine("Enter the 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 = Encrypt(text, key);
  19. Console.WriteLine($"Encrypted text: {encryptedText}");
  20. }
  21. else
  22. {
  23. string decryptedText = Decrypt(text, key);
  24. Console.WriteLine($"Decrypted text: {decryptedText}");
  25. }
  26.  
  27. Console.ReadLine();
  28. }
  29.  
  30. public static string Decrypt(string text, int shift)
  31. {
  32. string alphabet = "abcdefghijklmnoprstuwxyz";
  33. string plainText = "";
  34.  
  35. for (int counter = 0; counter < text.Length; counter++)
  36. {
  37. char character = text[counter];
  38. int characterIndex = alphabet.IndexOf(character);
  39.  
  40. if (characterIndex == -1)
  41. {
  42. plainText += character;
  43. }
  44. else
  45. {
  46. int newCharacterIndex = (characterIndex + (alphabet.Length - shift)) % alphabet.Length;
  47. char newCharacter = alphabet[newCharacterIndex];
  48. plainText += newCharacter;
  49. }
  50. }
  51. return plainText;
  52. }
  53.  
  54. public static string Encrypt(string text, int shift) {...}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement