vitcord

Untitled

Nov 10th, 2025 (edited)
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.82 KB | None | 0 0
  1. namespace Messages
  2. {
  3.     public class InvalidPasswordException : System.Exception
  4.     {
  5.         public string EncryptedText { get; }
  6.         public string Password { get; }
  7.  
  8.         public InvalidPasswordException() { }
  9.         public InvalidPasswordException(string message) : base(message) { }
  10.         public InvalidPasswordException(string message, string encryptedText, string password) : base(message)
  11.         {
  12.             EncryptedText = encryptedText;
  13.             Password = password;
  14.         }
  15.     }
  16.  
  17.     public class Rot13Encryption : IEncryption
  18.     {
  19.         public string Encrypt(string text)
  20.         {
  21.             if (text == null) return null;
  22.             var chars = new char[text.Length];
  23.             for (int i = 0; i < text.Length; i++) chars[i] = Rot13(text[i]);
  24.             return new string(chars);
  25.         }
  26.  
  27.         public string Decrypt(string text)
  28.         {
  29.             if (text == null) return null;
  30.             var chars = new char[text.Length];
  31.             for (int i = 0; i < text.Length; i++) chars[i] = Rot13(text[i]);
  32.             return new string(chars);
  33.         }
  34.  
  35.         public char GetMark() => 'R';
  36.  
  37.         private char Rot13(char letter)
  38.         {
  39.             if (letter >= 'A' && letter <= 'Z')
  40.                 letter = (char)('A' + (letter - 'A' + 13) % 26);
  41.             else if (letter >= 'a' && letter <= 'z')
  42.                 letter = (char)('a' + (letter - 'a' + 13) % 26);
  43.             return letter;
  44.         }
  45.     }
  46.  
  47.     public class PasswordBasedEncryption : IEncryption
  48.     {
  49.         private readonly string _password;
  50.  
  51.         public PasswordBasedEncryption(string password)
  52.         {
  53.             if (string.IsNullOrEmpty(password))
  54.                 throw new InvalidPasswordException();
  55.             _password = password;
  56.         }
  57.  
  58.         public string Encrypt(string text)
  59.         {
  60.             if (text == null) return null;
  61.             var chars = new char[text.Length];
  62.             for (int i = 0; i < text.Length; i++)
  63.             {
  64.                 int a = text[i];
  65.                 int b = _password[i % _password.Length];
  66.                 chars[i] = (char)(a + b);
  67.             }
  68.             return new string(chars);
  69.         }
  70.  
  71.         public string Decrypt(string text)
  72.         {
  73.             if (text == null) return null;
  74.             var chars = new char[text.Length];
  75.             for (int i = 0; i < text.Length; i++)
  76.             {
  77.                 int a = text[i];
  78.                 int b = _password[i % _password.Length];
  79.                 int c = a - b;
  80.                 if (c < 0)
  81.                     throw new InvalidPasswordException("Invalid password for decryption.", text, _password);
  82.                 chars[i] = (char)c;
  83.             }
  84.             return new string(chars);
  85.         }
  86.  
  87.         public char GetMark() => 'P';
  88.     }
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment