教学服务系统

 找回密码
 立即注册
搜索
查看: 508|回复: 5

信息计算2019级1班7号徐佳颖

[复制链接]

8

主题

21

帖子

80

积分

注册会员

Rank: 2

积分
80
发表于 2022-4-22 14:05:06 | 显示全部楼层 |阅读模式

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

21

帖子

80

积分

注册会员

Rank: 2

积分
80
 楼主| 发表于 2022-4-22 15:32:23 | 显示全部楼层

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

21

帖子

80

积分

注册会员

Rank: 2

积分
80
 楼主| 发表于 2022-4-29 21:30:10 | 显示全部楼层

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

21

帖子

80

积分

注册会员

Rank: 2

积分
80
 楼主| 发表于 2022-5-3 10:36:18 | 显示全部楼层
RSA算法实现

1.RSATools.cs

  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. }
复制代码
2.RSA.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;

  4. namespace RSA
  5. {
  6.     class RSA
  7.     {
  8.         private long p, q;
  9.         private long n;
  10.         private long euler_N;
  11.         private long[] publicKey;//0-e 1-n
  12.         private long[] privateKey;//0-d 1-n

  13.         public long P { get => p; set => p = value; }
  14.         public long Q { get => q; set => q = value; }
  15.         public long[] PublicKey { get => publicKey; set => publicKey = value; }
  16.         public long[] PrivateKey { get => privateKey; set => privateKey = value; }
  17.         public long N { get => n; set => n = value; }
  18.         public long Euler_N { get => euler_N; set => euler_N = value; }

  19.         public void InitPublicKey()
  20.         //初始化公钥
  21.         {
  22.             RSATools.getP_Q(out p, out q);
  23.             euler_N = RSATools.getEuler_N(p, q);
  24.             long e;
  25.             n = RSATools.getN(p, q);

  26.             Random radom = new Random();
  27.             while (true)
  28.             {
  29.                 e = radom.Next(655300);
  30.                 if (e > 1 && e < euler_N && RSATools.gcd(e, euler_N) == 1 && RSATools.isPrim(e)) break;
  31.             }

  32.             publicKey = new long[2];
  33.             publicKey[0] = e;
  34.             publicKey[1] = n;
  35.         }
  36.         public void InitPrivateKey()
  37.         //初始化私钥
  38.         {
  39.             long d = RSATools.inverse(publicKey[0], euler_N);
  40.             privateKey = new long[2];
  41.             privateKey[0] = d;
  42.             privateKey[1] = n;
  43.         }

  44.         //加密操作
  45.         public long encrypt(long plaintext, long[] p_k) => RSATools.getMod(p_k[0], plaintext, p_k[1]);
  46.         //解密操作
  47.         public long decrypt(long ciphertext) => RSATools.getMod(privateKey[0], ciphertext, privateKey[1]);

  48.         public string getCiphertext(string plaintext, long[] p_k)
  49.         //加密明文得到密文
  50.         {
  51.             long[] p_long = RSATools.UTF8ToLong(plaintext);
  52.             long[] c_long = new long[p_long.Length];
  53.             for (int i = 0; i < p_long.Length; i++)
  54.             {
  55.                 c_long[i] = encrypt(p_long[i], p_k);
  56.             }
  57.             string c_text = RSATools.longToBase64(c_long);
  58.             return c_text;
  59.         }
  60.         public string getPlaintext(string ciphertext)
  61.         //解密密文得到明文
  62.         {
  63.             long[] c_long = RSATools.Base64ToLong(ciphertext);
  64.             long[] p_long = new long[c_long.Length];
  65.             for (int i = 0; i < c_long.Length; i++)
  66.             {
  67.                 p_long[i] = decrypt(c_long[i]);
  68.             }
  69.             string p_text = RSATools.longToUTF8(p_long);
  70.             return p_text;
  71.         }
  72.     }
  73. }
复制代码



回复

使用道具 举报

8

主题

21

帖子

80

积分

注册会员

Rank: 2

积分
80
 楼主| 发表于 2022-5-3 10:37:08 | 显示全部楼层
3.Program.cs
  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 Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             RSA a = new RSA();
  13.             a.InitPublicKey();
  14.             a.InitPrivateKey();
  15.             string p = a.P.ToString();
  16.             string q = a.Q.ToString();
  17.             string publickey = a.PublicKey[0].ToString() + "-" + a.PublicKey[1].ToString();


  18.             Console.Write("原文:");
  19.             string plaintext = "19561654894513215674894153210324168789413210685749845613216574894156321321564897415631321654896";
  20.             Console.WriteLine(plaintext);

  21.             Console.Write("密文:");
  22.             string ciphertext = a.getCiphertext(plaintext, a.PublicKey);
  23.             Console.WriteLine(ciphertext);

  24.             Console.Write("译文:");
  25.             string decryptedtext = a.getPlaintext(ciphertext);
  26.             Console.WriteLine(decryptedtext);
  27.         }
  28.     }
  29. }
复制代码

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

21

帖子

80

积分

注册会员

Rank: 2

积分
80
 楼主| 发表于 2022-5-3 10:37:38 | 显示全部楼层

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-4-30 11:52 , Processed in 0.019116 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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