Bouncy Castle PGP解密问题

时间:2012-06-19 18:17:02

标签: c# encryption bouncycastle pgp encryption-asymmetric

我有一个使用Bouncy Castle进行PGP解密的应用程序,在过去的8个月左右没有任何问题,过去2天突然出现了一个问题,GetDataStream方法抛出了一个问题例外:

异常消息:“错误设置非对称密码”。

内部异常消息:“不是RSA密钥”。

private static PgpObjectFactory getClearDataStream(PgpPrivateKey privateKey, PgpPublicKeyEncryptedData publicKeyED)
{
    // Exception throws here.
    Stream clearStream = publicKeyED.GetDataStream(privateKey);

    PgpObjectFactory clearFactory = new PgpObjectFactory(clearStream);
    return clearFactory;
}

密钥尚未过期,没有到期日期:

enter image description here

我没有对应用程序进行任何更改,我没有触及按键,所以我不太明白为什么一个问题突然出现了。有任何想法吗?我也可以使用我在应用程序中加载的相同密钥使用Kleopatra手动解密文件。

更新1 - 我下载了OpenPGP Library for .NET的免费试用版,它也希望使用BouncyCastle,我也没有问题使用相同的密钥解密文件。出于某种原因,我使用已经工作了几个月的BouncyCastle实现的解密由于某种原因而停止工作,我还无法识别。

更新2 - 我从上周开始提取文件,并且我还下载了BouncyCastle的源代码,以便我可以单步调试以查看异常抛出的位置和有效的文件和不起作用的文件之间的变量如何不同。在PgpPublicKeyEncryptedData类的GetDataStream方法的开头抛出异常:

byte[] plain = fetchSymmetricKeyData(privKey);

当我进入这个方法时,对于我可以解密而没有任何问题的文件,我注意到keyData.Algorithm变量设置为“ElGamalEncrypt”,而对于异常抛出的文件,文件keyData.Algortithm设置为“RsaGeneral”。为什么这些不同?公司向我发送文件是否更改了加密方法? BouncyCastle没有正确支持这种加密方法吗?

private byte[] fetchSymmetricKeyData(PgpPrivateKey privKey)
{
    IBufferedCipher c1 = GetKeyCipher(keyData.Algorithm);

    try
    {
        c1.Init(false, privKey.Key);
    }
    catch (InvalidKeyException e)
    {
        throw new PgpException("error setting asymmetric cipher", e);
    }

另外,不确定这是否相关,我们的密钥的证书类型是DSA。

enter image description here

更新3 - 鉴于目前的密钥,我一直无法弄清楚如何解决问题。我昨天生成了新密钥(类型为DSA),并且使用新密钥解决了问题。

更新4 - 这个问题刚刚出现,我的上一次更新中使用了新密钥。现在,PgpPublicKeyEncryptedData类中的keyData.Algorithm再次被看作“RsaGeneral”而不是“ElGamalEncrypt”。为什么Algorithm属性会改变?加密文件的人是在改变什么吗?

2 个答案:

答案 0 :(得分:1)

这可能很重要(来源:http://www.opensourcejavaphp.net/csharp/itextsharp/PgpPublicKeyEncryptedData.cs.html):

它解释了你的keyData.Algorithm的价值不同,但我仍然不确定。这很可能是输入文件。它可能不同(客户端使用不同的密钥?)

private static IBufferedCipher GetKeyCipher(
            PublicKeyAlgorithmTag algorithm)
        {
            try
            {
                switch (algorithm)
                {
                    case PublicKeyAlgorithmTag.RsaEncrypt:
                    case PublicKeyAlgorithmTag.RsaGeneral:
                        return CipherUtilities.GetCipher("RSA//PKCS1Padding");
                    case PublicKeyAlgorithmTag.ElGamalEncrypt:
                    case PublicKeyAlgorithmTag.ElGamalGeneral:
                        return CipherUtilities.GetCipher("ElGamal/ECB/PKCS1Padding");
                    default:
                        throw new PgpException("unknown asymmetric algorithm: " + algorithm);
                }
            }
            catch (PgpException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw new PgpException("Exception creating cipher", e);
            }
        }

答案 1 :(得分:0)

看起来另一方正在加密其他/不同的密钥。 可能你的密钥环也包含RSA密钥,但BouncyCastle只使用第一个(???)。 使用gpg,您可以通过发出检查加密文件的内容 gpg --list-packets YourEncryptedFile.pgp

之后,将相同的命令应用于“good”文件和密钥环,并比较加密文件的密钥标识符。 由于您使用的是DSA密钥,因此文件应加密为ElGamal子密钥。

相关问题