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

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

问题描述

我已经用充气城堡已经没有任何问题,运行在过去的8个月左右的PGP解密应用程序,并在最近两天突然之间的问题已经拿出其中GetDataStream方法是抛出例外:



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



内部异常的消息:没有一个RSA关键的。

 私有静态PgpObjectFactory getClearDataStream(PgpPrivateKey privateKey,PgpPublicKeyEncryptedData publicKeyED)
{
//异常抛出在这里。
流清流= publicKeyED.GetDataStream(privateKey);

PgpObjectFactory clearFactory =新PgpObjectFactory(清流);
返回clearFactory;
}



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



我还没有对应用程序进行任何更改,我没碰过的钥匙,所以我不明白为什么一个问题又拿出了蓝色的。有任何想法吗?我也可以手动使用克列奥帕特拉利用我在应用程序中加载相同的密钥解密文件



更新1 - 我下载了免费试用对于 OpenPGP的图书馆.NET ,看起来使用BouncyCastle的同时,我也没有问题解密使用相同的文件键。出于某种原因,我的实现使用BouncyCastle的解密已经连续数月工作停止某些原因,我一直无法找出尚未工作。



更新2 - 我从上周曾拉文件,我还下载了BouncyCastle的源代码,以便我可以逐步调试,看看那里的异常被抛出和变量的文件之间的区别,工程和文件不起作用。唯一的例外是在PgpPublicKeyEncryptedData类的GetDataStream方法开始被抛出:

 字节[]纯= fetchSymmetricKeyData(的privKey) ; 

当我踏进这个方法,我可以没有任何问题解密文件,我注意到该keyData.Algorithm变量被设置为ElGamalEncrypt,而对于该异常引发的文件,该文件keyData.Algortithm设为RsaGeneral。为什么这些会有所不同?难道该公司寄来的文件,改变他们的加密方法?并且不恰当地BouncyCastle的支持这种加密方式?



 专用字节[] fetchSymmetricKeyData(PgpPrivateKey的privKey)
{
IBufferedCipher C1 = GetKeyCipher(keyData.Algorithm);


{
c1.Init(假,privKey.Key);
}
赶上(InvalidKeyException将E)
{
抛出新PgpException(错误设置非对称密码,E);
}



另外,不知道这是相关的,我们的主要的证书类型DSA。





更新3 - 我一直无法弄清楚如何解决这个问题尚未考虑到当前的密钥。昨天我产生了新的密钥(Key类型DSA),并用新的密钥问题已得到解决。



更新4 - 这个问题刚再次拿出,用在我的最后一次更新工作的新重点。再次,PgpPublicKeyEncryptedData类中的keyData.Algorithm正在看到RsaGeneral而不是ElGamalEncrypt了。为什么会产生算法的性能变化? ?是加密文件改变的东西的人。


解决方案

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



这说明你的keyData.Algorithm是不同的价值,但为什么我仍然不确定。它是最有可能的输入文件是这样的话。这可能是不同的(使用不同的密钥客户端?)

 私有静态IBufferedCipher GetKeyCipher(
PublicKeyAlgorithmTag算法)
{

{
开关(算法)
{
情况下PublicKeyAlgorithmTag.RsaEncrypt:
情况下PublicKeyAlgorithmTag.RsaGeneral:
返回CipherUtilities .GetCipher(RSA // PKCS1Padding);
情况下PublicKeyAlgorithmTag.ElGamalEncrypt:
情况下PublicKeyAlgorithmTag.ElGamalGeneral:
返回CipherUtilities.GetCipher(的ElGamal / ECB / PKCS1Padding);
默认:
抛出新PgpException(未知的非对称算法:+算法);
}
}
赶上(PgpException 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);
            }
        }

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

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