我从需要解密的外部服务获取加密字符串,然后使用BouncyCastle API重新加密。
我设法让解密工作正常,但加密似乎不起作用。当我尝试解密由我的加密方法生成的字符串时,我收到一条InvalidCipherTextException
消息“未知块类型”。
这是我的解密代码,它成功地解密了我正在与之交互的服务中的文本:
string Decrypt(string value)
{
string Signature = "My_Signature";
RsaKeyParameters keyParams = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(Signature));
IBufferedCipher cipher = CipherUtilities.GetCipher("RSA/NONE/PKCS1Padding");
cipher.Init(false, keyParams);
byte[] secretBytes = Convert.FromBase64String(value);
byte[] decrypted = cipher.DoFinal(secretBytes);
return Encoding.Default.GetString(decrypted);
}
这是我的加密方法,它似乎不会生成我的解密方法可以处理的加密字符串:
string Encrypt(string value)
{
string Signature = "My_Signature";
RsaKeyParameters keyParams = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(Signature));
IBufferedCipher cipher = CipherUtilities.GetCipher("RSA/NONE/PKCS1Padding");
cipher.Init(true, keyParams);
byte[] secretBytes = Encoding.Default.GetBytes(value);
byte[] encrypted = cipher.DoFinal(secretBytes);
return Convert.ToBase64String(encrypted);
}
我不确定我缺少什么来完成这项工作。有什么明显的我似乎在这里失踪了吗?