Blame view

netcore/src/Infrastructure/NCC/DataEncryption/Encryptions/RSAEncryption.cs 2.41 KB
de2bd2f9   “wangming”   项目初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  using NCC.Dependency;
  using System;
  using System.Security.Cryptography;
  using System.Text;
  
  namespace NCC.DataEncryption
  {
      /// <summary>
      /// RSA 加密
      /// </summary>
      [SuppressSniffer]
      public static class RSAEncryption
      {
          /// <summary>
          /// 生成 RSA 秘钥
          /// </summary>
          /// <param name="keySize">大小必须为 2048  16384 之间,且必须能被 8 整除</param>
          /// <returns></returns>
          public static (string publicKey, string privateKey) GenerateSecretKey(int keySize = 2048)
          {
              CheckRSAKeySize(keySize);
  
              using var rsa = new RSACryptoServiceProvider(keySize);
              return (rsa.ToXmlString(false), rsa.ToXmlString(true));
          }
  
          /// <summary>
          /// 加密
          /// </summary>
          /// <param name="text">明文内容</param>
          /// <param name="publicKey">公钥</param>
          /// <param name="keySize"></param>
          /// <returns></returns>
          public static string Encrypt(string text, string publicKey, int keySize = 2048)
          {
              CheckRSAKeySize(keySize);
  
              using var rsa = new RSACryptoServiceProvider(keySize);
              rsa.FromXmlString(publicKey);
  
              var encryptedData = rsa.Encrypt(Encoding.Default.GetBytes(text), false);
              return Convert.ToBase64String(encryptedData);
          }
  
          /// <summary>
          /// 解密
          /// </summary>
          /// <param name="text">密文内容</param>
          /// <param name="privateKey">私钥</param>
          /// <param name="keySize"></param>
          /// <returns></returns>
          public static string Decrypt(string text, string privateKey, int keySize = 2048)
          {
              CheckRSAKeySize(keySize);
  
              using var rsa = new RSACryptoServiceProvider(keySize);
              rsa.FromXmlString(privateKey);
  
              var decryptedData = rsa.Decrypt(Convert.FromBase64String(text), false);
              return Encoding.Default.GetString(decryptedData);
          }
  
          /// <summary>
          /// 检查 RSA 长度
          /// </summary>
          /// <param name="keySize"></param>
          private static void CheckRSAKeySize(int keySize)
          {
              if (keySize < 2048 || keySize > 16384 || keySize % 8 != 0)
                  throw new ArgumentException("The keySize must be between 2048 and 16384 in size and must be divisible by 8.", nameof(keySize));
          }
      }
  }