教学服务系统

 找回密码
 立即注册
搜索
查看: 536|回复: 1

信息计算2019级1班30号刘毅扬

[复制链接]

8

主题

16

帖子

94

积分

注册会员

Rank: 2

积分
94
发表于 2022-5-3 22:47:51 | 显示全部楼层 |阅读模式

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

8

主题

16

帖子

94

积分

注册会员

Rank: 2

积分
94
 楼主| 发表于 2022-5-3 22:49:17 | 显示全部楼层
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;

  6. namespace RSA
  7. {
  8.     class RSATools
  9.     {
  10.         public static bool isPrim(long num)
  11.         //以6为步进单元判断其是否为素数
  12.         {
  13.             //两个较小数另外处理
  14.             if (num == 2 || num == 3)
  15.                 return true;
  16.             //不在6的倍数两侧的一定不是质数
  17.             if (num % 6 != 1 && num % 6 != 5)
  18.                 return false;
  19.             long tmp = (long)Math.Sqrt(num);
  20.             //在6的倍数两侧的也可能不是质数
  21.             for (long i = 5; i <= tmp; i += 6)
  22.                 if (num % i == 0 || num % (i + 2) == 0)
  23.                     return false;
  24.             //排除所有,剩余的是质数
  25.             return true;
  26.         }
  27.         public static long gcd(long x, long y) => y != 0 ? gcd(y, x % y) : x;
  28.         //采用递归的形式,判断两个数是否互质
  29.         public static long inverse(long number1, long number3)
  30.         //利用欧几里得算法计算m,d的逆元
  31.         {
  32.             long x1 = 1, x2 = 0, x3 = number3, y1 = 0, y2 = 1, y3 = number1;
  33.             long q;
  34.             long number4 = 0;
  35.             long t1, t2, t3;
  36.             while (y3 != 0)
  37.             {
  38.                 if (y3 == 1)
  39.                 {
  40.                     number4 = y2;
  41.                     break;
  42.                 }
  43.                 else
  44.                 {
  45.                     q = (x3 / y3);
  46.                     t1 = x1 - q * y1;
  47.                     t2 = x2 - q * y2;
  48.                     t3 = x3 - q * y3;
  49.                     x1 = y1; x2 = y2; x3 = y3;
  50.                     y1 = t1; y2 = t2; y3 = t3;
  51.                 }
  52.             }
  53.             if (number4 < 0)
  54.                 number4 = number4 + number3;
  55.             return number4;
  56.         }
  57.         public static long getMod(long a, long b, long c)
  58.         //利用快速指数模运算,计算m^e mod n
  59.         {
  60.             //指数 e --> a  底数 m --> b   模数 n --> c
  61.             long number3 = 1;
  62.             while (a != 0)
  63.             {
  64.                 if (a % 2 == 1)
  65.                 {
  66.                     a = a - 1;
  67.                     number3 = (number3 * b) % c;
  68.                 }
  69.                 else
  70.                 {
  71.                     a = (a / 2);
  72.                     b = (b * b) % c;
  73.                 }
  74.             }
  75.             return number3;

  76.         }

  77.         public static long getN(long p, long q) => p * q;
  78.         public static long getEuler_N(long p, long q) => (p - 1) * (q - 1);
  79.         //求N的欧拉函数
  80.         public static void getP_Q(out long p, out long q)
  81.         {
  82.             Random random = new Random();
  83.             while (true)
  84.             {
  85.                 p = random.Next(1000);
  86.                 q = random.Next(2000);
  87.                 if (isPrim(p) && isPrim(q)) return;
  88.             }
  89.         }

  90.         public static byte[] longToBytes(long a)
  91.         {
  92.             byte[] b = new byte[8];
  93.             b[0] = (byte)(a);
  94.             b[1] = (byte)(a >> 8 & 0xFF);
  95.             b[2] = (byte)(a >> 16 & 0xFF);
  96.             b[3] = (byte)(a >> 24 & 0xFF);
  97.             b[4] = (byte)(a >> 32 & 0xFF);
  98.             b[5] = (byte)(a >> 40 & 0xFF);
  99.             b[6] = (byte)(a >> 48 & 0xFF);
  100.             b[7] = (byte)(a >> 56 & 0xFF);
  101.             return b;
  102.         }
  103.         public static long BytesToLong(byte[] input)
  104.         {
  105.             uint num = (uint)(((input[0] | (input[1] << 8)) | (input[2] << 0x10)) | (input[3] << 0x18));
  106.             uint num2 = (uint)(((input[4] | (input[5] << 8)) | (input[6] << 0x10)) | (input[7] << 0x18));
  107.             return (long)((num2 << 0x20) | num);
  108.         }

  109.         public static long[] UTF8ToLong(string input)
  110.         {
  111.             string input_64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(input));
  112.             byte[] inputByte = Convert.FromBase64String(input_64);
  113.             long[] inputLong = new long[inputByte.Length];
  114.             for (int i = 0; i < inputByte.Length; i++)
  115.             {
  116.                 inputLong[i] = (long)inputByte[i];
  117.             }
  118.             return inputLong;
  119.         }
  120.         public static string longToUTF8(long[] input)
  121.         {
  122.             byte[] inputByte = new byte[input.Length];
  123.             for (int i = 0; i < inputByte.Length; i++)
  124.             {
  125.                 inputByte[i] = (byte)input[i];
  126.             }
  127.             string inputBase64 = Convert.ToBase64String(inputByte);
  128.             byte[] outputb = Convert.FromBase64String(inputBase64);
  129.             string output = Encoding.UTF8.GetString(outputb);
  130.             return output;
  131.         }
  132.         public static long[] Base64ToLong(string input)
  133.         {
  134.             List<long> output_l = new List<long>();
  135.             string t = "";
  136.             for (int i = 0; i < input.Length; i++)
  137.             {
  138.                 t += input[i];
  139.                 if (input[i] == '=')
  140.                 {
  141.                     byte[] tByte = Convert.FromBase64String(t);
  142.                     output_l.Add(BytesToLong(tByte));
  143.                     t = "";
  144.                 }
  145.             }
  146.             return output_l.ToArray(); ;
  147.         }
  148.         public static string longToBase64(long[] input)
  149.         {
  150.             byte[][] inputByte = new byte[input.Length][];
  151.             string output = "";
  152.             for (int i = 0; i < input.Length; i++)
  153.             {
  154.                 inputByte[i] = longToBytes(input[i]);
  155.                 output += Convert.ToBase64String(inputByte[i]);
  156.             }
  157.             return output;
  158.         }

  159.     }
  160. }
  161. 复制代码
  162. 2.RSA.cs

  163. using System;
  164. using System.Collections.Generic;
  165. using System.Text;

  166. namespace RSA
  167. {
  168.     class RSA
  169.     {
  170.         private long p, q;
  171.         private long n;
  172.         private long euler_N;
  173.         private long[] publicKey;//0-e 1-n
  174.         private long[] privateKey;//0-d 1-n

  175.         public long P { get => p; set => p = value; }
  176.         public long Q { get => q; set => q = value; }
  177.         public long[] PublicKey { get => publicKey; set => publicKey = value; }
  178.         public long[] PrivateKey { get => privateKey; set => privateKey = value; }
  179.         public long N { get => n; set => n = value; }
  180.         public long Euler_N { get => euler_N; set => euler_N = value; }

  181.         public void InitPublicKey()
  182.         //初始化公钥
  183.         {
  184.             RSATools.getP_Q(out p, out q);
  185.             euler_N = RSATools.getEuler_N(p, q);
  186.             long e;
  187.             n = RSATools.getN(p, q);

  188.             Random radom = new Random();
  189.             while (true)
  190.             {
  191.                 e = radom.Next(655300);
  192.                 if (e > 1 && e < euler_N && RSATools.gcd(e, euler_N) == 1 && RSATools.isPrim(e)) break;
  193.             }

  194.             publicKey = new long[2];
  195.             publicKey[0] = e;
  196.             publicKey[1] = n;
  197.         }
  198.         public void InitPrivateKey()
  199.         //初始化私钥
  200.         {
  201.             long d = RSATools.inverse(publicKey[0], euler_N);
  202.             privateKey = new long[2];
  203.             privateKey[0] = d;
  204.             privateKey[1] = n;
  205.         }

  206.         //加密操作
  207.         public long encrypt(long plaintext, long[] p_k) => RSATools.getMod(p_k[0], plaintext, p_k[1]);
  208.         //解密操作
  209.         public long decrypt(long ciphertext) => RSATools.getMod(privateKey[0], ciphertext, privateKey[1]);

  210.         public string getCiphertext(string plaintext, long[] p_k)
  211.         //加密明文得到密文
  212.         {
  213.             long[] p_long = RSATools.UTF8ToLong(plaintext);
  214.             long[] c_long = new long[p_long.Length];
  215.             for (int i = 0; i < p_long.Length; i++)
  216.             {
  217.                 c_long[i] = encrypt(p_long[i], p_k);
  218.             }
  219.             string c_text = RSATools.longToBase64(c_long);
  220.             return c_text;
  221.         }
  222.         public string getPlaintext(string ciphertext)
  223.         //解密密文得到明文
  224.         {
  225.             long[] c_long = RSATools.Base64ToLong(ciphertext);
  226.             long[] p_long = new long[c_long.Length];
  227.             for (int i = 0; i < c_long.Length; i++)
  228.             {
  229.                 p_long[i] = decrypt(c_long[i]);
  230.             }
  231.             string p_text = RSATools.longToUTF8(p_long);
  232.             return p_text;
  233.         }
  234.     }
  235. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

教学服务系统

GMT+8, 2025-4-30 14:27 , Processed in 0.014821 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表