rsacryptoserviceprovider 使用 x509 证书 c#

rsacryptoserviceprovider using x509 certificates c#(rsacryptoserviceprovider 使用 x509 证书 c#)
本文介绍了rsacryptoserviceprovider 使用 x509 证书 c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用由 makecert 生成的证书,该证书具有私钥和公钥.Java 端使用此公钥加密数据,.net 将其解密.

i am using a certificate generated by makecert which has both private and public key. The java side uses this public key to encrypt the data and .net decrypts it back.

我正在尝试解密 Java 的加密 64 位编码字符串并获取错误数据.

I am trying to decrypt Java's encrypted 64 bit encoded string and getting bad data.

为了查看.Net 端是否一切正常,我首先尝试使用公钥加密,然后使用相同的证书使用私钥解密.我的代码如下所示.

To see if all is good on.Net end, I frist tried to encrypt with the public key and then decrypt with private using the same certificate. My code looks like this.

X509Certificate2 cert = GetCert(key, StoreName.My, StoreLocation.LocalMachine);
RSACryptoServiceProvider provider =  (RSACryptoServiceProvider)cert.PrivateKey;

RSACryptoServiceProvider publicprovider = (RSACryptoServiceProvider)cert.PublicKey.Key;

if (cert.HasPrivateKey)
    MessageBox.Show("Got private key");

byte[] encrypted = publicprovider.Encrypt(Encoding.UTF8.GetBytes(text), false);
byte[] decryptedBytes = provider.Decrypt(encrypted, false); 

即使在这里我也遇到了错误.我错过了什么吗?

Even here I am getting the error. Am i Missing something?

证书看起来对公钥和私钥都有效.

The certificate looks valid with both public and private key.

推荐答案

我终于找到了问题所在.我没有把 makecert 的密钥定义为 RSA 加密密钥.

I finally found the problem. I wasn't putting the key to makecert to define it as RSA Crypto key.

这篇关于rsacryptoserviceprovider 使用 x509 证书 c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)