- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试在 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/
我有一些数据要保护,所以我使用 ProtectedData 将其加密到文件中。当我尝试读取和解密数据时,我遇到了最奇怪的异常: CryptographicException - 无法更新密码。为新密码
解密 key 时出现错误: CryptographicException was unhandled by user code. Length of the data to decrypt is in
我有这段用于解密文件的代码,但如果我运行它,它会在 using 语句 using (CryptoStream .. .) { ... } public static void DecryptFile(
我正在使用 Thinktecture Identity 服务器并在 azure 中运行它。我遇到的问题是,有时当我读取联合元数据 XML 文件或登录时,我会收到此 Keyset does not ex
我正在使用 Thinktecture Identity 服务器并在 azure 中运行它。我遇到的问题是,有时当我读取联合元数据 XML 文件或登录时,我会收到此 Keyset does not ex
我在 Azure 网站中部署的 SignalR 中遇到了此异常。在调试环境下运行良好。它是 SignalR 1.0.1,我使用 .NET MVC 和 WebApi The data protectio
我正在尝试使用 .NET 中的 RijndaelManaged 类来加密文本字符串。但是,我不断收到 CryptographicException(“要解密的数据长度无效”)。此消息并不是很有帮助,尤
我连接到一个外部 Web 服务,该服务在 2018 年 5 月中旬之前一直完美无缺地运行。突然间,我开始收到带有消息“格式错误的 XML 签名”的 CryptographicException。我的调
每次我将我的 Blazor 服务器项目发布到我的网站域并打开该网站时,都会发生此异常,谷歌搜索几乎没有帮助: 它说 AppState.cs: line 21,所以这是它的代码行: 调试本地主机时不会发
加载站点核心站点的默认页面时遇到问题。日志错误如下: System.Security.Cryptography.CryptographicException 未处理 Message=系统找不到指定的路
我有一些代码可以调用使用 X.509 认证保护的第三方 Web 服务。 如果我直接调用代码(使用单元测试),它就可以正常工作。 部署后,将通过 WCF 服务调用此代码。我添加了第二个调用 WCF 服务
public RSAKeyPair() { string keyContainerName="pEncKey" CspParameters cspp = new
我正在尝试在 C# 中使用 RSA 加密和解密数据。我有以下 MSTest 单元测试: const string rawPassword = "mypass"; // Encrypt string p
我有一个 .p12 证书文件,我这样创建我的证书: var certificate = new X509Certificate2(certFileLocation, "mySecret", X509K
您好,感谢您的帮助。我们试图让 Sitecore 站点在 WS 2008 R2 标准服务器上正常运行,但一直遇到以下异常: System.Security.Cryptography.Cryptogra
我有一个托管在 Azure 上的 Identity Server Web 应用。它的根目录中有一个用于签名的 .pfx 文件。问题是,当新发布时它工作得很好,但一段时间后它开始抛出Cryptograp
我正在使用 Xamarin 创建 Monodroid 应用程序。 我与后端的通信是通过 https 协议(protocol)进行的。这会导致 Debug模式下出现“System.Security.Cr
在我的 ASP.NET WebForms 应用程序中 (如果重要的话,该应用程序在 windows server 2008 R2、IIS 7.5 和 Runtime v4.0 集成模式应用程序池上运行
我有一个使用 RSACryptoServiceProvider 使用已知私钥(存储在变量中)解密一些数据的应用程序。 当 IIS 应用程序池配置为使用网络服务时,一切运行正常。 但是,当我们配置 II
尝试启动 Docker 桌面应用程序时出现此错误。我尝试卸载并重新安装 Docker。 最佳答案 我不得不删除凭据管理器中的 Docker 主机文件系统访问记录。 关于Docker System.Se
我是一名优秀的程序员,十分优秀!