Mostafa_Mohamed5

Encryption Algorithms

Nov 17th, 2025
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.95 KB | None | 0 0
  1. using System.Text;
  2.  
  3. namespace EncryptionAlgos {
  4.     public class CaesarCipher {
  5.         public string Text { get; set; } = String.Empty;
  6.         public long Key;
  7.         public CaesarCipher(long key) {
  8.             Key = key % 26;
  9.         }
  10.         public CaesarCipher(string text, long key) {
  11.             if (text == null) throw new ArgumentNullException("text");
  12.             if (key < 1) throw new ArgumentOutOfRangeException("key should be a positive integer");
  13.             Text = text;
  14.             Key = key % 26;
  15.         }
  16.         public string Encrypt() {
  17.             var encryptedText = new StringBuilder();
  18.             foreach (char t in Text) {
  19.                 if (!Char.IsAsciiLetter(t)) {
  20.                     encryptedText.Append(t);
  21.                     continue;
  22.                 }
  23.                 char offset = Char.IsUpper(t) ? 'A' : 'a';
  24.                 char c = (char)((t - offset + Key) % 26 + offset);
  25.                 encryptedText.Append(c);
  26.             }
  27.             return encryptedText.ToString();
  28.         }
  29.  
  30.         public static string Encrypt(string text, int key) {
  31.             if (text == null) throw new ArgumentNullException("text");
  32.             if (key < 1) throw new ArgumentOutOfRangeException("key should be a positive integer");
  33.             key %= 26;
  34.             var encryptedText = new StringBuilder();
  35.             foreach (char t in text) {
  36.                 if (!Char.IsAsciiLetter(t)) {
  37.                     encryptedText.Append(t);
  38.                     continue;
  39.                 }
  40.                 char offset = Char.IsUpper(t) ? 'A' : 'a';
  41.                 char c = (char)((t - offset + key) % 26 + offset);
  42.                 encryptedText.Append(c);
  43.             }
  44.             return encryptedText.ToString();
  45.         }
  46.  
  47.         public static string Decrypt(string encryptedText, long key) {
  48.             if (encryptedText == null) throw new ArgumentNullException("encryptedText");
  49.             if (key < 1) throw new ArgumentOutOfRangeException("key should be a positive integer");
  50.             key %= 26;
  51.             var decryptedText = new StringBuilder();
  52.             foreach (char c in encryptedText) {
  53.                 if (!Char.IsAsciiLetter(c)) {
  54.                     decryptedText.Append(c);
  55.                     continue;
  56.                 }
  57.                 char offset = Char.IsUpper(c) ? 'A' : 'a';
  58.                 char t = (char)((c - offset - key + 26) % 26 + offset);
  59.                 decryptedText.Append(t);
  60.             }
  61.             return decryptedText.ToString();
  62.         }
  63.  
  64.         public static List<string> Decrypt(string encryptedText) {
  65.             if (encryptedText == null) throw new ArgumentNullException("encryptedText");
  66.             var list = new List<string>();
  67.             for (int key = 1; key <= 25; ++key)
  68.                 list.Add(Decrypt(encryptedText, key));
  69.             return list;
  70.         }
  71.     }
  72.  
  73.     public class MonoAlphabeticCipher {
  74.         public string Text { get; set; }
  75.  
  76.         public string Key { get; set; }
  77.         public Dictionary<char, char> dict;
  78.  
  79.         public MonoAlphabeticCipher(string text) {
  80.             if (text == null) throw new ArgumentNullException("text");
  81.             Text = text;
  82.             var key = new StringBuilder();
  83.             for (char c = 'a'; c <= 'z'; ++c)
  84.                 key.Append(c);
  85.             for (char c = 'A'; c <= 'Z'; ++c)
  86.                 key.Append(c);
  87.             var rnd = new Random();
  88.             for (int i = 0; i < key.Length; ++i) {
  89.                 int j = rnd.Next(0, key.Length);
  90.                 (key[i], key[j]) = (key[j], key[i]);
  91.             }
  92.             dict = new Dictionary<char, char>();
  93.             int idx = 0;
  94.             for (char c = 'a'; c <= 'z'; ++c)
  95.                 dict.Add(c, key[idx++]);
  96.             for (char c = 'A'; c <= 'Z'; ++c)
  97.                 dict.Add(c, key[idx++]);
  98.             Key = key.ToString();
  99.         }
  100.         public string Encrypt() {
  101.             var encryptedText = new StringBuilder();
  102.             foreach (char t in Text) {
  103.                 if (!Char.IsAsciiLetter(t)) {
  104.                     encryptedText.Append(t);
  105.                     continue;
  106.                 }
  107.                 encryptedText.Append(dict[t]);
  108.             }
  109.             return encryptedText.ToString();
  110.         }
  111.  
  112.         public static string Encrypt(string text, string key) {
  113.             if (text == null) throw new ArgumentNullException("text");
  114.             if (key == null) throw new ArgumentNullException("key");
  115.  
  116.             if (!ValidKey(key))
  117.                 throw new Exception("invalid key");
  118.             var d = new Dictionary<char, char>();
  119.             int idx = 0;
  120.             for (char c = 'a'; c <= 'z'; ++c)
  121.                 d.Add(c, key[idx++]);
  122.             for (char c = 'A'; c <= 'Z'; ++c)
  123.                 d.Add(c, key[idx++]);
  124.             var encryptedText = new StringBuilder();
  125.             foreach (char t in text) {
  126.                 if (!Char.IsAsciiLetter(t)) {
  127.                     encryptedText.Append(t);
  128.                     continue;
  129.                 }
  130.                 encryptedText.Append(d[t]);
  131.             }
  132.             return encryptedText.ToString();
  133.         }
  134.  
  135.         public static string Decrypt(string encryptedText, string key) {
  136.             if (encryptedText == null) throw new ArgumentNullException("encryptedText");
  137.             if (key == null) throw new ArgumentNullException("key");
  138.             if (!ValidKey(key))
  139.                 throw new Exception("invalid key");
  140.             var d = new Dictionary<char, char>();
  141.             int idx = 0;
  142.             for (char c = 'a'; c <= 'z'; ++c)
  143.                 d.Add(key[idx++], c);
  144.             for (char c = 'A'; c <= 'Z'; ++c)
  145.                 d.Add(key[idx++], c);
  146.             var decryptedText = new StringBuilder();
  147.             foreach (char c in encryptedText) {
  148.                 if (!Char.IsAsciiLetter(c)) {
  149.                     decryptedText.Append(c);
  150.                     continue;
  151.                 }
  152.                 decryptedText.Append(d[c]);
  153.             }
  154.             return decryptedText.ToString();
  155.         }
  156.  
  157.         static bool ValidKey(string key) {
  158.             if (key.Length != 52) return false;
  159.             var st = new HashSet<char>();
  160.             foreach (char ch in key)
  161.                 st.Add(ch);
  162.             for (char c = 'a'; c <= 'z'; ++c)
  163.                 if (!st.Contains(c)) return false;
  164.             for (char c = 'A'; c <= 'Z'; ++c)
  165.                 if (!st.Contains(c)) return false;
  166.             foreach (char c in key)
  167.                 if (!Char.IsAsciiLetter(c)) return false;
  168.             return true;
  169.         }
  170.     }
  171.  
  172.     public class PolyAlphabeticCipher {
  173.         public string Text { get; set; }
  174.         public string Key { get; set; }
  175.         public PolyAlphabeticCipher(string text) {
  176.             if (text == null) throw new ArgumentNullException("text");
  177.             Text = text.ToUpper();
  178.             var rnd = new Random();
  179.             var key = new StringBuilder();
  180.             for (int i = 0; i < text.Length; ++i) {
  181.                 char c = (char)('A' + rnd.Next(0, 26));
  182.                 key.Append(c);
  183.             }
  184.             Key = key.ToString();
  185.         }
  186.  
  187.         public PolyAlphabeticCipher(string text, string key) {
  188.             if (text == null) throw new ArgumentNullException("text");
  189.             if (String.IsNullOrEmpty(key)) throw new ArgumentException("key cannot be empty");
  190.             Text = text.ToUpper();
  191.             Key = key.ToUpper();
  192.         }
  193.  
  194.         static int ToNumber(char c) => c - 'A';
  195.  
  196.         public string Encrypt() {
  197.             var encryptedText = new StringBuilder();
  198.             int idx = 0;
  199.             foreach (char t in Text) {
  200.                 if (!Char.IsAsciiLetter(t)) {
  201.                     encryptedText.Append(t);
  202.                     continue;
  203.                 }
  204.                 char c = (char)((ToNumber(t) + ToNumber(Key[idx++])) % 26 + 'A');
  205.                 idx %= Key.Length;
  206.                 encryptedText.Append(c);
  207.             }
  208.             return encryptedText.ToString();
  209.         }
  210.  
  211.         public static string Encrypt(string text, string key) {
  212.             if (text == null) throw new ArgumentNullException("text");
  213.             if (string.IsNullOrEmpty(key)) throw new ArgumentException("key cannot be empty");
  214.             var encryptedText = new StringBuilder();
  215.             int idx = 0;
  216.             foreach (char t in text) {
  217.                 if (!Char.IsAsciiLetter(t)) {
  218.                     encryptedText.Append(t);
  219.                     continue;
  220.                 }
  221.                 char c = (char)((ToNumber(t) + ToNumber(key[idx++])) % 26 + 'A');
  222.                 idx %= key.Length;
  223.                 encryptedText.Append(c);
  224.             }
  225.             return encryptedText.ToString();
  226.         }
  227.  
  228.         public static string Decrypt(string encryptedText, string key) {
  229.             if (encryptedText == null) throw new ArgumentNullException("encryptedText");
  230.             if (string.IsNullOrEmpty(key)) throw new ArgumentException("key cannot be empty");
  231.             var decryptedText = new StringBuilder();
  232.             int idx = 0;
  233.             foreach (char c in encryptedText) {
  234.                 if (!Char.IsAsciiLetter(c)) {
  235.                     decryptedText.Append(c);
  236.                     continue;
  237.                 }
  238.                 char t = (char)((ToNumber(c) - ToNumber(key[idx++]) + 26) % 26 + 'A');
  239.                 idx %= key.Length;
  240.                 decryptedText.Append(t);
  241.             }
  242.             return decryptedText.ToString();
  243.         }
  244.     }
  245.     internal class Program {
  246.         static void Main(string[] args) {
  247.             string s = Console.ReadLine()!;
  248.             var cipher = new MonoAlphabeticCipher(s);
  249.             Console.WriteLine("Key = " + cipher.Key);
  250.             string encryption = cipher.Encrypt();
  251.             Console.WriteLine("encrypted text = " + encryption);
  252.             Console.WriteLine("decrypted text = " + MonoAlphabeticCipher.Decrypt(encryption, cipher.Key));
  253.         }
  254.     }
  255. }
  256.  
Advertisement
Add Comment
Please, Sign In to add comment