Advertisement
mrAnderson33

RSA

Jun 8th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Numerics;
  4.  
  5. namespace RC4
  6. {
  7.     public class RCAClass
  8.     {
  9.  
  10.         private int p;
  11.         private int q;
  12.         private int d = 0;
  13.         public long e = 23;
  14.  
  15.         //N больше любог M
  16.         //Сделать все Без Бигинта
  17.         public byte[] Encrypt(byte[] message)
  18.         {
  19.             var pq = Generate();
  20.  
  21.             p = pq.Item1;
  22.             q = pq.Item2;
  23.  
  24.             Console.WriteLine($"p={p}");
  25.             Console.WriteLine($"q={q}");
  26.  
  27.             long eiler = (p - 1) * (q - 1);
  28.             long N = p * q;
  29.             int tmp = 0;
  30.             int t = 0;
  31.             gcdext((int)e, (int)eiler, ref tmp, ref d, ref t);
  32.  
  33.             Console.WriteLine((d * e) % eiler);
  34.             //шифр
  35.  
  36.             var shifr = new long[message.Length];
  37.  
  38.             for (int i = 0; i < message.Length; i++)
  39.                 shifr[i] = ModPow(message[i], e, N);
  40.  
  41.             for (int i = 0; i < shifr.Length; i++)
  42.                 message[i] = (byte)ModPow(shifr[i], d, N);
  43.  
  44.             return message;
  45.         }
  46.  
  47.  
  48.  
  49.         static public Tuple<int, int> Generate()
  50.         {
  51.             var rand = new Random();
  52.  
  53.             int p = rand.Next(0,100);
  54.             int q = rand.Next(0,100);
  55.  
  56.             bool isPSimpl = Test(p);
  57.             bool isQSimpl = Test(q);
  58.  
  59.             do
  60.             {
  61.                 p = rand.Next(0,100);
  62.                 isPSimpl = Test(p);
  63.             }
  64.             while (!isPSimpl);
  65.  
  66.             do
  67.             {
  68.                 q = rand.Next(0, 100);
  69.                 isQSimpl = Test(q);
  70.             }
  71.             while (!isQSimpl);
  72.                
  73.             return new Tuple<int, int>(p, q);
  74.         }
  75.  
  76.         public static bool Test(int num)
  77.         {
  78.             int k = 5;
  79.  
  80.             long b;
  81.  
  82.             var rand = new Random();
  83.  
  84.             for (int i = 0; i < k; i++)
  85.             {
  86.                 b = ModPow(rand.Next(2,num-1),num-1,num);
  87.                 if (b != 1) return false;
  88.             }
  89.  
  90.             return true;
  91.         }
  92.  
  93.         static public long ModPow(long value, long exponent, long modulus)
  94.         {
  95.             if (exponent == 0) return 1;
  96.             long z = ModPow(value, exponent / 2, modulus);
  97.             if (exponent % 2 == 0)
  98.                 return (z * z) % modulus;
  99.             else
  100.                 return (value * z * z) % modulus;
  101.         }
  102.  
  103.  
  104.         static public void gcdext(int a, int b, ref int d,ref int x,ref int y)
  105.  
  106.         {
  107.  
  108.             int s;
  109.  
  110.             if (b == 0)
  111.  
  112.             {
  113.  
  114.                 d = a; x = 1; y = 0;
  115.                 if (d > 1) x = 0;
  116.                 return;
  117.  
  118.             }
  119.  
  120.             gcdext(b, a % b,ref d,ref x,ref y);
  121.  
  122.             s = y;
  123.  
  124.             y = x - (a / b) * (y);
  125.  
  126.             if (d > 1) x = 0;
  127.             else x = s;
  128.  
  129.         }
  130.  
  131.         static public bool[] ReshtoEratosfena(int size)
  132.         {
  133.             var res = new bool[size];
  134.  
  135.             Parallel.For(0, size, (x) => res[x] = true);       
  136.             for (int p=2;p*p<size;)
  137.             {
  138.                 for (int i = 2; i * p < size; i++)
  139.                 {
  140.                     res[i * p] = false;
  141.                 }
  142.  
  143.                 for (int i = p+1; i < size; i++)
  144.                 {
  145.                     if (res[i])
  146.                     {
  147.                         p = i;
  148.                         break;
  149.                     }
  150.  
  151.                 }
  152.             }
  153.  
  154.  
  155.             return res;
  156.         }
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement