System.IO.IOException: -----未找到结束 RSA 私钥
System.IO.IOException: -----END RSA PRIVATE KEY not found
我正在尝试为服务器使用 PHP 创建在线数据库应用程序,为客户端使用 C# 表单应用程序。
在服务器上,我使用 public RSA 密钥和 PHPSecLib 加密了一个简单的字符串。然后 C# 应用程序接收该字符串并尝试使用相应的私钥对其进行解密。
字节在服务器上进行 base64 编码,并由 C# 再次解码为字节。我使用 PHPSecLib 创建了密钥对。
这是我在客户端应用程序中使用的代码:
public string rsa_decrypt(string encryptedText, string privateKey) {
byte[] bytesToDecrypt = Convert.FromBase64String(encryptedText);
Pkcs1Encoding decrypter = new Pkcs1Encoding(new RsaEngine());
//the error occurs on this line:
AsymmetricCipherKeyPair RSAParams = (AsymmetricCipherKeyPair)new PemReader(new StringReader(privateKey)).ReadObject();
decrypter.Init(false, RSAParams.Private);
byte[] decryptedBytes = decrypter.ProcessBlock(bytesToDecrypt, 0, bytesToDecrypt.Length);
string decryptedString = Convert.ToBase64String(decryptedBytes);
return decryptedString;
}
但是,我在上面指定的行中收到以下错误^。
An unhandled exception of type 'System.IO.IOException' occurred in
BouncyCastle.Crypto.dll
Additional information: -----END RSA PRIVATE KEY not found
我相信密钥对组合没有任何问题,因为我什至在尝试解密任何内容之前就收到错误消息。
privateKey
参数当前使用以下格式硬编码到脚本中:
string privateKey = "-----BEGIN RSA PRIVATE KEY-----XXXXXXXX-----END RSA PRIVATE KEY-----";
所以在我看来,页脚实际上包含在字符串中...我到处调试和搜索,但我似乎无法解决它。我是 RSA 和 Bouncycastle 的新手,所以也许我只是使用了错误的方法。
希望能帮到你,谢谢!
- G4A
P.S。这是我的第一个 Whosebug 问题,我刚刚创建了一个帐户,所以如果您也可以就我提出这个问题的方式给我一些反馈;太棒了!
需要在pre/post封装边界文本和Base64数据之间加一行,所以:
string privateKey = "-----BEGIN RSA PRIVATE KEY-----\r\nXXX\r\n-----END RSA PRIVATE KEY-----";
这是因为 pem 规范允许在两者之间存在其他文本headers。
如果这不起作用
"-----BEGIN RSA PRIVATE KEY-----\r\nXXXXXXXX\r\n-----END RSA PRIVATE KEY-----"
请试试这个
"-----BEGIN RSA PRIVATE KEY-----
XXXXXXXX
-----END RSA PRIVATE KEY-----"
我们将 BOX 私钥转换为 Base64 格式并将其存储在 Azure Vault 中。
使用 Base64Encode 方法将密钥转换为 Base64,存储在 Azure Key Vault 中。
在代码中检索编码字符串,使用 Base64Decode 方法解码回来。
public static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
public static string Base64Decode(string base64EncodedData)
{
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
我正在尝试为服务器使用 PHP 创建在线数据库应用程序,为客户端使用 C# 表单应用程序。 在服务器上,我使用 public RSA 密钥和 PHPSecLib 加密了一个简单的字符串。然后 C# 应用程序接收该字符串并尝试使用相应的私钥对其进行解密。 字节在服务器上进行 base64 编码,并由 C# 再次解码为字节。我使用 PHPSecLib 创建了密钥对。
这是我在客户端应用程序中使用的代码:
public string rsa_decrypt(string encryptedText, string privateKey) {
byte[] bytesToDecrypt = Convert.FromBase64String(encryptedText);
Pkcs1Encoding decrypter = new Pkcs1Encoding(new RsaEngine());
//the error occurs on this line:
AsymmetricCipherKeyPair RSAParams = (AsymmetricCipherKeyPair)new PemReader(new StringReader(privateKey)).ReadObject();
decrypter.Init(false, RSAParams.Private);
byte[] decryptedBytes = decrypter.ProcessBlock(bytesToDecrypt, 0, bytesToDecrypt.Length);
string decryptedString = Convert.ToBase64String(decryptedBytes);
return decryptedString;
}
但是,我在上面指定的行中收到以下错误^。
An unhandled exception of type 'System.IO.IOException' occurred in BouncyCastle.Crypto.dll
Additional information: -----END RSA PRIVATE KEY not found
我相信密钥对组合没有任何问题,因为我什至在尝试解密任何内容之前就收到错误消息。
privateKey
参数当前使用以下格式硬编码到脚本中:
string privateKey = "-----BEGIN RSA PRIVATE KEY-----XXXXXXXX-----END RSA PRIVATE KEY-----";
所以在我看来,页脚实际上包含在字符串中...我到处调试和搜索,但我似乎无法解决它。我是 RSA 和 Bouncycastle 的新手,所以也许我只是使用了错误的方法。
希望能帮到你,谢谢! - G4A
P.S。这是我的第一个 Whosebug 问题,我刚刚创建了一个帐户,所以如果您也可以就我提出这个问题的方式给我一些反馈;太棒了!
需要在pre/post封装边界文本和Base64数据之间加一行,所以:
string privateKey = "-----BEGIN RSA PRIVATE KEY-----\r\nXXX\r\n-----END RSA PRIVATE KEY-----";
这是因为 pem 规范允许在两者之间存在其他文本headers。
如果这不起作用
"-----BEGIN RSA PRIVATE KEY-----\r\nXXXXXXXX\r\n-----END RSA PRIVATE KEY-----"
请试试这个
"-----BEGIN RSA PRIVATE KEY-----
XXXXXXXX
-----END RSA PRIVATE KEY-----"
我们将 BOX 私钥转换为 Base64 格式并将其存储在 Azure Vault 中。
使用 Base64Encode 方法将密钥转换为 Base64,存储在 Azure Key Vault 中。
在代码中检索编码字符串,使用 Base64Decode 方法解码回来。
public static string Base64Encode(string plainText) { var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText); return System.Convert.ToBase64String(plainTextBytes); } public static string Base64Decode(string base64EncodedData) { var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData); return System.Text.Encoding.UTF8.GetString(base64EncodedBytes); }