【问题标题】:BouncyCastle Decryption works, but not Encryption?BouncyCastle 解密有效,但加密无效?
【发布时间】: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


    【解决方案1】:

    我假设您的 Signature-string 实际上包含一个公钥的 base64 编码?

    我不会给你一个关于Public-key cryptography的完整课程,但请记住你必须使用公钥加密和私钥解密。看起来您正在尝试使用相同的密钥来执行这两项操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-04
      • 2018-06-18
      • 2021-05-26
      • 2012-06-02
      • 1970-01-01
      • 2018-07-04
      • 1970-01-01
      相关资源
      最近更新 更多