gpt4 book ai didi

c# - 如何将私钥导入 c# RSAParameters 对象?

转载 作者:太空宇宙 更新时间:2023-11-03 10:22:08 25 4
gpt4 key购买 nike

我想在c#中用rsa加密和解密数据。我写了这段代码,它可以很好地加密,但我在解密时遇到了问题。我的问题是什么?!

这是我的代码:

using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;

namespace Cry
{
public class CryptoUtils
{
public CryptoUtils ()
{
UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
byte[] encryptedData;
byte[] decryptedData;
string decrptedStr = "";

var pub = Convert.FromBase64String("MCgCIQCfkl4xV5T/v3r1bifOc1mVHa9yak5pGjUfAv0r+s6+AwIDAQAB");
var prv = Convert.FromBase64String("MIGsAgEAAiEAn5JeMVeU/7969W4nznNZlR2vcmpOaRo1HwL9K/rOvgMCAwEAAQIg\nMce6pM/6xpIYrMoxluE7JBkVe9Sme9d6NPPJJX3NyBECEgCmwIarl1hSBnTqZNwJ\n8hZhqwIQAPT6CO/l/ma1sDi7eM7tCQISAKH90lYLlr9IinfSN3hp95g1AhAAlyNf\nuioqX1G+y/GVogyJAhEmQQB52juSQ574HnampzXUpQ==");

using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) {
RSAParameters myRSAParameters = RSA.ExportParameters(false);
myRSAParameters.Modulus = pub;
myRSAParameters.Exponent = ByteConverter.GetBytes("65537");
myRSAParameters.D = prv;
encryptedData = RSAEncrypt(dataToEncrypt, myRSAParameters, false);
decryptedData = RSADecrypt(encryptedData, myRSAParameters, false);
decrptedStr = ByteConverter.GetString(decryptedData);
}
}

static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding) {
try {
byte[] encryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) {
RSA.ImportParameters(RSAKeyInfo);
encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
}
return encryptedData;
} catch (CryptographicException e) {
Console.WriteLine(e.Message);

return null;
}

}

static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding) {
try {
byte[] decryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) {
RSA.ImportParameters(RSAKeyInfo);
decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
}
return decryptedData;
} catch (CryptographicException e) {
Console.WriteLine(e.ToString());

return null;
}

}
}
}

在行 encryptedData = RSAEncrypt(dataToEncrypt, myRSAParameters, false); 数据加密但在下一行我有这个错误:

System.Security.Cryptography.CryptographicException: PKCS1 decoding error.
at System.Security.Cryptography.RSAPKCS1KeyExchangeDeformatter.DecryptKeyExchange (System.Byte[] rgbIn) [0x00000] in <filename unknown>:0
at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt (System.Byte[] rgb, Boolean fOAEP) [0x00000] in <filename unknown>:0

我想我没有正确填写 myRSAParameters 属性,但我不知道如何填写!

最佳答案

在 .NET 中,如果您指定 RSAParameters.D,您还必须为 P、Q、DP、DQ 和 InverseQ 指定(正确的)值。

虽然从技术上讲,模数、指数和 D 都是执行 RSA 所必需的,但实际上很少有实现使用 D,因为执行 RSA via the Chinese Remainder Theorem 效率更高。 .

例如,虽然 OpenSSL 将退回到仅使用 D; .NET 不会。

关于c# - 如何将私钥导入 c# RSAParameters 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33047476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com