Bouncy城​​堡PGP解密问题 [英] Bouncy Castle PGP Decryption Issue

查看:275
本文介绍了Bouncy城​​堡PGP解密问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



异常消息:错误设置不对称密码。



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

  private static PgpObjectFactory getClearDataStream(PgpPrivateKey privateKey,PgpPublicKeyEncryptedData publicKeyED)
{
//异常扔在这里
Stream clearStream = publicKeyED.GetDataStream(privateKey);

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

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





我没有对应用程序进行任何更改,我没有碰到钥匙,所以我不太明白为什么一个问题出来了蓝色。有任何想法吗?我也可以使用与我在应用程序中加载的相同的密钥手动解密文件。



更新1 - 我下载了免费试用版对于):



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

 私有静态IBufferedCipher GetKeyCipher(
PublicKeyAlgorithmTag算法)
{
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(异常e)
{
抛出新的PgpException(异常创建密码,e);
}
}


I've had a application using Bouncy Castle for PGP decryption which has run without any issues for the past 8 months or so, and the past 2 days all of a sudden an issue has come up where the GetDataStream method is throwing an exception:

Exception Message: "error setting asymmetric cipher".

Inner Exception Message : "Not an RSA key".

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

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

The key hasn't expired, it has no expiration date:

I haven't made any changes to the application, I haven't touched the keys, so I can't quite understand why an issue has come up out of the blue. Any ideas? I can also manually decrypt the files using Kleopatra using the same keys that I load in the application.

Update 1 - I downloaded the free trial for OpenPGP Library for .NET, which looks to use BouncyCastle also, and I have no issues decrypting the files using the same key. For some reason, my implementation of decryption using BouncyCastle that has worked for several months stopped working for some reason that I have not been able to identify yet.

Update 2 - I pulled files from last week that worked, and I've also downloaded the source code of BouncyCastle in order that I can step through and debug to see where the exception is throwing and how the variables differ between a file that works and a file that doesn't work. The exception is being thrown at the beginning of the GetDataStream method of the PgpPublicKeyEncryptedData class:

byte[] plain = fetchSymmetricKeyData(privKey);

When I step into this method, for files that I can decrypt without any problem, I've noticed that the keyData.Algorithm variable is set to "ElGamalEncrypt", whereas for files that the exception throws, the file keyData.Algortithm is set to "RsaGeneral". Why would these differ? Did the company sending me the files change their encryption method? And is this encryption method not properly supported by 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);
    }

Also, not sure if this is related, the certificate type of our key is DSA.

Update 3 - I've been unable to figure out how to resolve the issue as of yet given the current keys. I generated new keys (type DSA) yesterday, and with the new keys the issue has been resolved.

Update 4 - This issue has just come up again, with the new key that worked in my last update. Once again, the keyData.Algorithm within the PgpPublicKeyEncryptedData class is being see to "RsaGeneral" instead of "ElGamalEncrypt" now. Why would the Algorithm property change? Is the person encrypting the file changing something?

解决方案

This could be important (Source: http://www.opensourcejavaphp.net/csharp/itextsharp/PgpPublicKeyEncryptedData.cs.html) :

It explains the value of your keyData.Algorithm being different, but the why I am still unsure of. It is most likely the input file that is the case. It could be different (client using a different key?)

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);
            }
        }

这篇关于Bouncy城​​堡PGP解密问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆