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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Custom Error Queue Name when using EasyNetQ for RabbitMQ?(使用 EasyNetQ for RabbitMQ 时自定义错误队列名称?)
Rabbitmq Ack or Nack, leaving messages on the queue(Rabbitmq Ack 或 Nack,将消息留在队列中)
Wait for a single RabbitMQ message with a timeout(等待一条带有超时的 RabbitMQ 消息)
Setup RabbitMQ consumer in ASP.NET Core application(在 ASP.NET Core 应用程序中设置 RabbitMQ 消费者)
How do I set a number of retry attempts in RabbitMQ?(如何在 RabbitMQ 中设置重试次数?)
WebClient set headers(WebClient 设置标头)