Advertisement
vovanhik_24

Task1.5

Apr 8th, 2025
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Numerics;
  4.  
  5. namespace RSA
  6. {
  7.     class Program
  8.     {
  9.         private static readonly int p = 61;
  10.         private static readonly int q = 53;
  11.         private static readonly int n = p * q;
  12.         private static readonly int e = 17;
  13.         private static readonly int d = 2753;
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             string message = "Привет";
  18.             Console.WriteLine($"Оригинальное сообщение: {message}");
  19.  
  20.             List<BigInteger> encrypted = EncryptMessage(message);
  21.  
  22.             Console.Write("\nЗашифрованное сообщение: ");
  23.             foreach (var val in encrypted)
  24.             {
  25.                 Console.Write(val + " ");
  26.             }
  27.  
  28.             string decrypted = DecryptMessage(encrypted);
  29.             Console.WriteLine($"\n\nРасшифрованное сообщение: {decrypted}\n\n");
  30.         }
  31.  
  32.         static BigInteger EncryptChar(char ch)
  33.         {
  34.             BigInteger m = ch;
  35.             return BigInteger.ModPow(m, e, n);
  36.         }
  37.  
  38.         static char DecryptChar(BigInteger cipher)
  39.         {
  40.             BigInteger m = BigInteger.ModPow(cipher, d, n);
  41.             return (char)(int)m;
  42.         }
  43.  
  44.         static List<BigInteger> EncryptMessage(string message)
  45.         {
  46.             List<BigInteger> result = new List<BigInteger>();
  47.  
  48.             foreach (char ch in message)
  49.             {
  50.                 result.Add(EncryptChar(ch));
  51.             }
  52.  
  53.             return result;
  54.         }
  55.  
  56.         static string DecryptMessage(List<BigInteger> encrypted)
  57.         {
  58.             string result = "";
  59.  
  60.             foreach (BigInteger cipher in encrypted)
  61.             {
  62.                 result += DecryptChar(cipher);
  63.             }
  64.  
  65.             return result;
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement