gpt4 book ai didi

c# - 使用 RSA 加密/解密时间歇性出现 CryptographicException

转载 作者:太空狗 更新时间:2023-10-29 21:07:11 24 4
gpt4 key购买 nike

我正在尝试在 C# 中使用 RSA 加密和解密数据。我有以下 MSTest 单元测试:

const string rawPassword = "mypass";

// Encrypt
string publicKey, privateKey;
string encryptedPassword = RSAUtils.Encrypt(rawPassword, out publicKey, out privateKey);
Assert.AreNotEqual(rawPassword, encryptedPassword,
"Raw password and encrypted password should not be equal");

// Decrypt
string decryptedPassword = RSAUtils.Decrypt(encryptedPassword, privateKey);
Assert.AreEqual(rawPassword, decryptedPassword,
"Did not get expected decrypted password");

它在解密过程中失败,但只是偶尔失败。似乎每当我设置断点并单步执行测试时,它就会通过。这让我觉得也许有些事情没有及时完成才能成功解密,我在调试时放慢了步进速度,给了它足够的时间来完成。当它失败时,它似乎失败的行是 decryptedBytes = rsa.Decrypt(bytesToDecrypt, false); 在以下方法中:

public static string Decrypt(string textToDecrypt, string privateKeyXml)
{
if (string.IsNullOrEmpty(textToDecrypt))
{
throw new ArgumentException(
"Cannot decrypt null or blank string"
);
}
if (string.IsNullOrEmpty(privateKeyXml))
{
throw new ArgumentException("Invalid private key XML given");
}
byte[] bytesToDecrypt = ByteConverter.GetBytes(textToDecrypt);
byte[] decryptedBytes;
using (var rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(privateKeyXml);
decryptedBytes = rsa.Decrypt(bytesToDecrypt, false); // fail here
}
return ByteConverter.GetString(decryptedBytes);
}

失败并出现以下异常:

System.Security.Cryptography.CryptographicException: Bad Data

我的加密方法如下:

public static string Encrypt(string textToEncrypt, out string publicKey,
out string privateKey)
{
byte[] bytesToEncrypt = ByteConverter.GetBytes(textToEncrypt);
byte[] encryptedBytes;
using (var rsa = new RSACryptoServiceProvider())
{
encryptedBytes = rsa.Encrypt(bytesToEncrypt, false);
publicKey = rsa.ToXmlString(false);
privateKey = rsa.ToXmlString(true);
}
return ByteConverter.GetString(encryptedBytes);
}

整个过程中使用的 ByteConverter 如下:

public static readonly UnicodeEncoding ByteConverter = new UnicodeEncoding();

我在 StackOverflow 上看到了一些关于使用 .NET 进行 RSA 加密和解密的问题。 This one是由于使用私钥加密并尝试使用公钥解密,但我不认为我正在这样做。 This question和我有同样的异常(exception),但选择的答案是使用 OpenSSL.NET,我不想这样做。

我做错了什么?

最佳答案

能否将 ByteConverter.GetBytes 替换为 Convert.FromBase64String 并将 ByteConverter.GetString 替换为 Convert.ToBase64String看看是否有帮助。 Bad Data 异常通常意味着数据中有无效字符或长度不是解密的正确长度。我认为使用 Convert 函数可能会解决您的问题。

  public static readonly UnicodeEncoding ByteConverter = new UnicodeEncoding();

public static string Encrypt(string textToEncrypt, out string publicKey,
out string privateKey)
{
byte[] bytesToEncrypt = ByteConverter.GetBytes(textToEncrypt);
byte[] encryptedBytes;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
encryptedBytes = rsa.Encrypt(bytesToEncrypt, false);
publicKey = rsa.ToXmlString(false);
privateKey = rsa.ToXmlString(true);
}
return Convert.ToBase64String(encryptedBytes);
}

public static string Decrypt(string textToDecrypt, string privateKeyXml)
{
if (string.IsNullOrEmpty(textToDecrypt))
{
throw new ArgumentException(
"Cannot decrypt null or blank string"
);
}
if (string.IsNullOrEmpty(privateKeyXml))
{
throw new ArgumentException("Invalid private key XML given");
}
byte[] bytesToDecrypt = Convert.FromBase64String(textToDecrypt);
byte[] decryptedBytes;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(privateKeyXml);
decryptedBytes = rsa.Decrypt(bytesToDecrypt, false); // fail here
}
return ByteConverter.GetString(decryptedBytes);
}

关于c# - 使用 RSA 加密/解密时间歇性出现 CryptographicException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3152698/

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