Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Numerics;
- namespace RSA
- {
- class Program
- {
- private static readonly int p = 61;
- private static readonly int q = 53;
- private static readonly int n = p * q;
- private static readonly int e = 17;
- private static readonly int d = 2753;
- static void Main(string[] args)
- {
- string message = "Привет";
- Console.WriteLine($"Оригинальное сообщение: {message}");
- List<BigInteger> encrypted = EncryptMessage(message);
- Console.Write("\nЗашифрованное сообщение: ");
- foreach (var val in encrypted)
- {
- Console.Write(val + " ");
- }
- string decrypted = DecryptMessage(encrypted);
- Console.WriteLine($"\n\nРасшифрованное сообщение: {decrypted}\n\n");
- }
- static BigInteger EncryptChar(char ch)
- {
- BigInteger m = ch;
- return BigInteger.ModPow(m, e, n);
- }
- static char DecryptChar(BigInteger cipher)
- {
- BigInteger m = BigInteger.ModPow(cipher, d, n);
- return (char)(int)m;
- }
- static List<BigInteger> EncryptMessage(string message)
- {
- List<BigInteger> result = new List<BigInteger>();
- foreach (char ch in message)
- {
- result.Add(EncryptChar(ch));
- }
- return result;
- }
- static string DecryptMessage(List<BigInteger> encrypted)
- {
- string result = "";
- foreach (BigInteger cipher in encrypted)
- {
- result += DecryptChar(cipher);
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement