rsacryptoserviceprovider 使用 x509 证书 c# [英] rsacryptoserviceprovider using x509 certificates c#

查看:20
本文介绍了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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆