讓我開始說我對這一切都非常陌生。我想要做的是從Java內部使用gpg來解密加密文件。讓GPG解密在Java中工作(Bouncy Castle)
我已經成功地完成:
有一個同事使用我的公鑰和自己的私鑰加密文件,併成功解密它。
走到另一條路
有另一個同事嘗試解密,這不是他一個文件:失敗(如預期)這樣產生
我的鑰匙......
(GPG --version告訴我,我使用的是1.4.5,我用充氣城堡1.47)
GPG --gen-萊伊
選擇選項「DSA和Elgamal(默認)」
填寫其他字段並生成密鑰。
該文件已使用我的公鑰和另一個密鑰加密。我想解密它。我已經編寫了下面的Java代碼來完成這個任務。我使用了幾個不推薦使用的方法,但我無法弄清楚如何正確實現使用非棄用版本所需的工廠方法,所以如果有人對我應該使用的那些實現有一個想法,那將是一個不錯的獎金。
Security.addProvider(new BouncyCastleProvider());
PGPSecretKeyRingCollection secretKeyRing = new PGPSecretKeyRingCollection(new FileInputStream(new File("test-files/secring.gpg")));
PGPSecretKeyRing pgpSecretKeyRing = (PGPSecretKeyRing) secretKeyRing.getKeyRings().next();
PGPSecretKey secretKey = pgpSecretKeyRing.getSecretKey();
PGPPrivateKey privateKey = secretKey.extractPrivateKey("mypassword".toCharArray(), "BC");
System.out.println(privateKey.getKey().getAlgorithm());
System.out.println(privateKey.getKey().getFormat());
PGPObjectFactory pgpF = new PGPObjectFactory(
new FileInputStream(new File("test-files/test-file.txt.gpg")));
Object pgpObj = pgpF.nextObject();
PGPEncryptedDataList encryptedDataList = (PGPEncryptedDataList) pgpObj;
Iterator objectsIterator = encryptedDataList.getEncryptedDataObjects();
PGPPublicKeyEncryptedData publicKeyEncryptedData = (PGPPublicKeyEncryptedData) objectsIterator.next();
InputStream inputStream = publicKeyEncryptedData.getDataStream(privateKey, "BC");
所以,當我運行此代碼,我得知我的算法和格式是我的祕密密鑰如下:
算法:DSA 格式:PKCS#8
然後它打破上最後一行:
Exception in thread "main" org.bouncycastle.openpgp.PGPException: error setting asymmetric cipher
at org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder.decryptSessionData(Unknown Source)
at org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder.access$000(Unknown Source)
at org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder$2.recoverSessionData(Unknown Source)
at org.bouncycastle.openpgp.PGPPublicKeyEncryptedData.getDataStream(Unknown Source)
at org.bouncycastle.openpgp.PGPPublicKeyEncryptedData.getDataStream(Unknown Source)
at org.bouncycastle.openpgp.PGPPublicKeyEncryptedData.getDataStream(Unknown Source)
at org.bouncycastle.openpgp.PGPPublicKeyEncryptedData.getDataStream(Unknown Source)
at TestBouncyCastle.main(TestBouncyCastle.java:74)
產生的原因:java.security.InvalidKeyException:未知的密鑰類型傳遞到的ElGamal 在org.bouncyc astle.jcajce.provider.asymmetric.elgamal.CipherSpi.engineInit(Unknown Source) at org.bouncycastle.jcajce.provider.asymmetric.elgamal.CipherSpi.engineInit(Unknown Source) at javax.crypto.Cipher.init(DashoA13 * ..) 在javax.crypto.Cipher.init(DashoA13 * ..) ... 8個
我接受了很多建議在這裏,從「不使用GPG,用x代替「到」不要使用充氣城堡,用x來代替「之間的任何事物。謝謝!
+1 Ewwwwwwwwwwww;) – 2013-02-21 19:55:20
-1原因很明顯。此外,在通話過程中,任何查看活動進程列表的人都可以看到您的密碼,也可能會在系統日誌中顯示。 – user359996 2013-05-21 18:00:54