【发布时间】:2010-09-10 13:22:52
【问题描述】:
我从外部服务获取了一个加密字符串,我需要对其进行解密,然后使用 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);
}
我不确定我缺少什么来完成这项工作。这里有什么明显的我似乎遗漏的东西吗?
【问题讨论】:
标签: c# encryption cryptography bouncycastle