c# RSA 加密解密帮助类更新

更新 RSA 帮助类解决加密文本长度

简介

由于旧版本 RSA 帮助类 对加密的文字长度有限制,所以更新代码以加密更长的字符。

代码

RSA 库

依旧是基于 System.Security.Cryptography.RSA 库

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
/// <summary>
/// RSA加密解密帮助类
/// </summary>
public class RSAHelper
{
/// <summary>
/// Rsa 生成秘钥
/// </summary>
/// <param name="xmlPublicKey">公钥</param>
/// <param name="xmlPrivateKey">私钥</param>
public static void GenerateRSAKeys(out string xmlPublicKey, out string xmlPrivateKey)
{
try
{
using RSA rsa = RSA.Create();
xmlPublicKey = rsa.ToXmlString(false);
xmlPrivateKey = rsa.ToXmlString(true);
}
catch (Exception ex)
{
xmlPublicKey = string.Empty;
xmlPrivateKey = string.Empty;
}
}

/// <summary>
/// RSA 加密
/// </summary>
/// <param name="xmlPublicKey">公钥</param>
/// <param name="strPlaintext">明文</param>
/// <returns>RSA密文</returns>
public static string RSAEncrypt(string xmlPublicKey, string strPlaintext)
{
try
{
byte[] originalData = Encoding.UTF8.GetBytes(strPlaintext);
using RSA rsa = RSA.Create();
rsa.FromXmlString(xmlPublicKey);
byte[] encryptedData = rsa.Encrypt(originalData, RSAEncryptionPadding.OaepSHA1);
return Convert.ToBase64String(encryptedData);
}
catch (Exception ex)
{
return string.Empty;
}
}

/// <summary>
/// RSA 解密
/// </summary>
/// <param name="xmlPrivateKey">私钥</param>
/// <param name="strCiphertext">RSA密文</param>
/// <returns>明文</returns>
public static string RSADecrypt(string xmlPrivateKey, string strCiphertext)
{
try
{
byte[] encryptedData = Convert.FromBase64String(strCiphertext);
using RSA rsa = RSA.Create();
rsa.FromXmlString(xmlPrivateKey);
byte[] decryptedData = rsa.Decrypt(encryptedData, RSAEncryptionPadding.OaepSHA1);
return Encoding.UTF8.GetString(decryptedData);
}
catch (Exception ex)
{
return string.Empty;
}
}
}

Portable.BouncyCastle 库

由于 System.Security.Cryptography.RSA 库只能使用自身生成的 xml 公钥与私钥,而想使用 OpenSSL 生成的 pem 公钥与私钥,则无法读取,使用 Portable.BouncyCastle 库可以读取编解码,但是实测似乎与 C++ OpenSSL 无法互相加密解密。

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/// <summary>
/// 基于 BouncyCastle 库加密解密 RSA
/// </summary>
public class BouncyCastleHelper
{
/// <summary>
/// 读取 OpenSSL 生成的私钥文件 private.pem 为 AsymmetricKeyParameter
/// </summary>
/// <param name="privateKeyFilePath">OpenSSL 生成 RSA 私钥路径</param>
/// <returns></returns>
public static AsymmetricKeyParameter ReadPrivateKeyFile(string privateKeyFilePath)
{
using var reader = File.OpenText(privateKeyFilePath);
var pemReader = new PemReader(reader);
var keyObject = pemReader.ReadObject();

if (keyObject is AsymmetricCipherKeyPair keyPair)
{
return keyPair.Private;
}
else if (keyObject is AsymmetricKeyParameter keyParam)
{
return keyParam;
}
else
{
throw new InvalidOperationException("Invalid private key format.");
}
}

/// <summary>
/// 读取 OpenSSL 生成的公钥文件 public.pem 为 AsymmetricKeyParameter
/// </summary>
/// <param name="publicKeyFilePath">OpenSSL 生成 RSA 公钥路径</param>
/// <returns></returns>
public static AsymmetricKeyParameter ReadPublicKeyFile(string publicKeyFilePath)
{
using var reader = File.OpenText(publicKeyFilePath);
var pemReader = new PemReader(reader);
var keyObject = pemReader.ReadObject();

if (keyObject is AsymmetricKeyParameter keyParam)
{
return keyParam;
}
else
{
throw new InvalidOperationException("Invalid public key format.");
}
}

/// <summary>
/// 读取 OpenSSL 生成的私钥文件为 AsymmetricKeyParameter
/// </summary>
/// <param name="privateKey">OpenSSL 生成 RSA 私钥</param>
/// <returns></returns>
public static AsymmetricKeyParameter ReadPrivateKey(string privateKey)
{
StringReader stringReader = new StringReader(privateKey);
var pemReader = new PemReader(stringReader);
var keyObject = pemReader.ReadObject();

if (keyObject is AsymmetricCipherKeyPair keyPair)
{
return keyPair.Private;
}
else if (keyObject is AsymmetricKeyParameter keyParam)
{
return keyParam;
}
else
{
throw new InvalidOperationException("Invalid private key format.");
}
}

/// <summary>
/// 读取 OpenSSL 生成的公钥为 AsymmetricKeyParameter
/// </summary>
/// <param name="publicKey">OpenSSL 生成 RSA 公钥</param>
/// <returns></returns>
public static AsymmetricKeyParameter ReadPublicKey(string publicKey)
{
StringReader stringReader = new StringReader(publicKey);
var pemReader = new PemReader(stringReader);
var keyObject = pemReader.ReadObject();

if (keyObject is AsymmetricKeyParameter keyParam)
{
return keyParam;
}
else
{
throw new InvalidOperationException("Invalid public key format.");
}
}

/// <summary>
/// 根据 AsymmetricKeyParameter 生成 System.Security.Cryptography.RSA 秘钥
/// </summary>
/// <param name="xmlPublicKey">公钥</param>
/// <param name="xmlPrivateKey">私钥</param>
public static void GenerateRSAKeys(AsymmetricKeyParameter rsaKey, out string xmlPublicKey, out string xmlPrivateKey)
{
try
{
if (rsaKey.IsPrivate)
{
using RSA rsa = RSA.Create(DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters)rsaKey));
xmlPublicKey = rsa.ToXmlString(false);
xmlPrivateKey = rsa.ToXmlString(true);
}
else
{
using RSA rsa = RSA.Create(DotNetUtilities.ToRSAParameters((RsaKeyParameters)rsaKey));
xmlPublicKey = rsa.ToXmlString(false);
xmlPrivateKey = string.Empty;
}
}
catch (Exception ex)
{
xmlPublicKey = string.Empty;
xmlPrivateKey = string.Empty;
}
}

/// <summary>
/// RSA 加密
/// </summary>
/// <param name="publicKey">公钥</param>
/// <param name="strPlaintext">明文</param>
/// <returns>RSA密文</returns>
public static string RSAEncrypt(AsymmetricKeyParameter publicKey, string strPlaintext)
{
try
{
byte[] inputData = Encoding.UTF8.GetBytes(strPlaintext);
IAsymmetricBlockCipher cipher = new RsaEngine();
cipher.Init(true, publicKey);
byte[] encryptedData = cipher.ProcessBlock(inputData, 0, inputData.Length);
return Convert.ToBase64String(encryptedData);
}
catch (Exception ex)
{
return string.Empty;
}
}

/// <summary>
/// RSA 解密
/// </summary>
/// <param name="privateKey">私钥</param>
/// <param name="strCiphertext">RSA密文</param>
/// <returns>明文</returns>
public static string RSADecrypt(AsymmetricKeyParameter privateKey, string strCiphertext)
{
try
{
byte[] encryptedData = Convert.FromBase64String(strCiphertext);
IAsymmetricBlockCipher cipher = new RsaEngine();
cipher.Init(false, privateKey);
byte[] decryptedData = cipher.ProcessBlock(encryptedData, 0, encryptedData.Length);
return Encoding.UTF8.GetString(decryptedData);
}
catch (Exception ex)
{
return string.Empty;
}
}
}