.net core 不支持 RSACryptoServiceProvider

yyxu123 2020-06-28 08:11:53
在.net core 中 使用 RSA 加签的时候 使用pfx证书文件加签

Unable to cast object of type 'System.Security.Cryptography.RSACng' to type 'System.Security.Cryptography.RSACryptoServiceProvider'.



try
{
X509Certificate2 pc = new X509Certificate2(AppDomain.CurrentDomain.BaseDirectory + Config.PrivateLucPath, Config.PrivateLucPwd);

//OpenSSL.X509Certificate2Provider.BaseCertificateProvider fromFileProvider = new ba
RSACryptoServiceProvider p = (RSACryptoServiceProvider) pc.PrivateKey;

RSA p = (RSA)pc.PrivateKey;

byte[] enBytes = p.SignData(Encoding.UTF8.GetBytes(value),HashAlgorithmName.SHA1,RSASignaturePadding.Pkcs1);
p.Dispose();
return Convert.ToBase64String(enBytes);

}
catch (Exception e)
{
//处理异常,这里抛异常 Unable to cast object of type 'System.Security.Cryptography.RSACng' to type 'System.Security.Cryptography.RSACryptoServiceProvider'.

}
return string.Empty;


请问各位牛人怎么处理?
...全文
2073 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dr.Xin 2020-10-13
  • 打赏
  • 举报
回复
好吧 为了世界和平我还是回复一下吧,希望大家好好生活 天天向上,cao!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  var pc = new X509Certificate2(priKeyFile, keyPwd, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
            var rsaParam = pc.GetRSAPrivateKey().ExportParameters(true);
            var rsa = new RSACryptoServiceProvider();
            rsa.ImportParameters(rsaParam);
            byte[] data1 = Encoding.UTF8.GetBytes(data);
            SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider();
            byte[] bCrypto = sha256.ComputeHash(data1);
            var halg = new SHA256CryptoServiceProvider();
            byte[] inArray = rsa.SignData(bCrypto, "SHA256");
            //签名后转Base64
            return Convert.ToBase64String(inArray);
Dr.Xin 2020-10-13
  • 打赏
  • 举报
回复
请问作者解决了吗?我也在处理这个问题 很急啊 不知道core怎么签名证书!!
github_36000833 2020-06-29
  • 打赏
  • 举报
回复
你开发不看参考吗? X509Certificate2.PrivateKey的类型是*AsymmetricAlgorithm*。 AsymmetricAlgorithm可能有如下继承: - DSA - ECDiffieHellman - ECDsa - RSA 而RSA的具体实现有(当前): - RSACng - RSACryptoServiceProvider - RSAOpenSsl 这里不讨论“使用pfx证书”来导入密钥是否合适。但对你的代码来说,你要判断PrivateKey是否RSA,如果是,使用RSA抽象类来签署,而不是盲目使用RSACryptoServiceProvider。
yyxu123 2020-06-28
  • 打赏
  • 举报
回复
没人看到么
Twitter Digg Facebook Del.icio.us Reddit Stumbleupon Newsvine Technorati Mr. Wong Yahoo! Google Windows Live Send as Email Add to your CodeProject bookmarks Discuss this article 85 Print Article Database » Database » Other databasesLicence CPOL First Posted 19 Jan 2012 Views 24,219 Downloads 992 Bookmarked 74 times RaptorDB - The Key Value Store V2 By Mehdi Gholam | 8 Mar 2012 | Unedited contribution C#.NETDBABeginnerIntermediateAdvanceddatabase Even faster Key/Value store nosql embedded database engine utilizing the new MGIndex data structure with MurMur2 Hashing and WAH Bitmap indexes for duplicates. See Also More like this More by this author Article Browse Code Stats Revisions (8) Alternatives 4.95 (56 votes) 1 2 3 4 5 4.95/5 - 56 votes μ 4.95, σa 1.05 [?] Is your email address OK? You are signed up for our newsletters but your email address is either unconfirmed, or has not been reconfirmed in a long time. Please click here to have a confirmation email sent so we can confirm your email address and start sending you newsletters again. Alternatively, you can update your subscriptions. Add your own alternative version Introduction What is RaptorDB? Features Why another data structure? The problem with a b+tree Requirements of a good index structure The MGIndex Page Splits Interesting side effects of MGIndex The road not taken / the road taken and doubled back! Performance Tests Comparing B+tree and MGIndex Really big data sets! Index parameter tuning Performance Tests - v2.3 Using the Code Differences to v1 Using RaptorDBString and RaptorDBGuid Global parameters RaptorDB interface Non-clean shutdowns Removing Keys Unit tests File Formats File Format : *.mgdat File Format : *.mgbmp File Format : *.mgidx File Format : *.mgbmr , *.mgrec History Download RaptorDB_v2.0.zip - 38.7 KB Download RaptorDB_v2.1.zip - 39 KB Download RaptorDB_v2.2.zip - 39 KB Download RaptorDB_v2.3.zip - 39.6 KB D

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧