RAS加解密帮助类,不记得哪里抄的了
IRSAManager
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
public interface IRSAManager
{
/// <summary>
/// 生成私钥
/// </summary>
/// <returns>私钥base64字符串</returns>
string CreatePrivateKey();
/// <summary>
/// 生成私钥
/// </summary>
/// <param name="dwKeySize">私钥位数</param>
/// <returns>私钥base64字符串</returns>
string CreatePrivateKey(int dwKeySize);
/// <summary>
/// 获取公钥
/// </summary>
/// <param name="priKey">私钥base64字符串</param>
/// <returns>公钥base64字符串</returns>
string GetPublicKey(string priKey);
/// <summary>
/// 签名数据
/// </summary>
/// <param name="priKey">私钥字符串</param>
/// <param name="data">待签名内容</param>
/// <returns>签名后字符串</returns>
string SignData(string priKey, string data);
/// <summary>
/// 验证签名
/// </summary>
/// <param name="pubKey">公钥字符串</param>
/// <param name="signedData">签名后字符串</param>
/// <param name="data">待验证签名内容</param>
/// <returns>是否验证通过</returns>
bool VerifySignedData(string pubKey, string signedData, string data);
}
class RSAManager : IRSAManager
{
/// <summary>
/// 生成私钥
/// </summary>
/// <returns>私钥base64字符串</returns>
public string CreatePrivateKey()
{
var Rsa = new RSACryptoServiceProvider();
var priKey = Convert.ToBase64String(Rsa.ExportCspBlob(true));
return priKey;
}
/// <summary>
/// 生成私钥
/// </summary>
/// <param name="dwKeySize">私钥位数</param>
/// <returns>私钥base64字符串</returns>
public string CreatePrivateKey(int dwKeySize)
{
var Rsa = new RSACryptoServiceProvider(dwKeySize);
var priKey = Convert.ToBase64String(Rsa.ExportCspBlob(true));
return priKey;
}
/// <summary>
/// 获取公钥
/// </summary>
/// <param name="priKey">私钥base64字符串</param>
/// <returns>公钥base64字符串</returns>
public string GetPublicKey(string priKey)
{
var rsa = new RSACryptoServiceProvider();
rsa.ImportCspBlob(Convert.FromBase64String(priKey));
var pubKey = Convert.ToBase64String(rsa.ExportCspBlob(false));
return pubKey;
}
/// <summary>
/// 签名数据
/// </summary>
/// <param name="priKey">私钥字符串</param>
/// <param name="data">待签名内容</param>
/// <returns>签名后字符串</returns>
public string SignData(string priKey, string data)
{
var rsa = new RSACryptoServiceProvider();
rsa.ImportCspBlob(Convert.FromBase64String(priKey));
var signData = rsa.SignData(Encoding.UTF8.GetBytes(data), new SHA1CryptoServiceProvider());
return Convert.ToBase64String(signData);
}
/// <summary>
/// 验证签名
/// </summary>
/// <param name="pubKey">公钥字符串</param>
/// <param name="signedData">签名后字符串</param>
/// <param name="data">待验证签名内容</param>
/// <returns>是否验证通过</returns>
public bool VerifySignedData(string pubKey, string signedData, string data)
{
var rsa = new RSACryptoServiceProvider();
rsa.ImportCspBlob(Convert.FromBase64String(pubKey));
var verify = rsa.VerifyData(Encoding.UTF8.GetBytes(data), new SHA1CryptoServiceProvider(), Convert.FromBase64String(signedData));
return verify;
}
}
本文会经常更新,请阅读原文: https://dashenxian.github.io/post/RSA%E5%8A%A0%E8%A7%A3%E5%AF%86 ,以避免陈旧错误知识的误导,同时有更好的阅读体验。
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 小神仙 (包含链接: https://dashenxian.github.io ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 (125880321@qq.com) 。