Error[8]: Undefined offset: 6, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述让我开始说我对这一切非常新鲜.我想要做的是使用 Java中的gpg来解密加密文件. 我成功完成了什么 >如果某位同事使用我的公钥和他的私钥对文件进行加密,并成功解密. >另一方面 >如果另一个同事尝试解密一个不适合他的文件:失败(如预期) 我的钥匙是这样生成的 (gpg –version告诉我我正在使用1.4.5,我正在使用Bouncy Castle 1.47) gpg –gen-ley 选择选项 让我开始说我对这一切非常新鲜.我想要做的是使用 Java中的gpg来解密加密文件.

我成功完成了什么

>如果某位同事使用我的公钥和他的私钥对文件进行加密,并成功解密.
>另一方面
>如果另一个同事尝试解密一个不适合他的文件:失败(如预期)

我的钥匙是这样生成的

(gpg –version告诉我我正在使用1.4.5,我正在使用Bouncy Castle 1.47)

gpg –gen-ley

选择选项“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 cipherat org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder.decryptSessionData(UnkNown Source)at org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder.access
public static PGPSecretKey readSecretKeyFromCol(inputStream in,long keyID) throws IOException,PGPException {    in = PGPUtil.getDecoderStream(in);    PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(in,new BcKeyFingerprintCalculator());    PGPSecretKey key = pgpSec.getSecretKey(keyID);    if (key == null) {        throw new IllegalArgumentException("Can't find encryption key in key ring.");    }    return key;}
0(UnkNown Source)at org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder.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.bouncycastle.jcajce.provIDer.asymmetric.elgamal.CipherSpi.engineInit(未知来源)
在org.bouncycastle.jcajce.provIDer.asymmetric.elgamal.CipherSpi.engineInit(未知来源)
在javax.crypto.Cipher.init(DashoA13 * ..)
在javax.crypto.Cipher.init(DashoA13 * ..)
… 8更多

我在这里开放了很多建议,从“不要使用gpg,使用x”来“不要使用d性城堡,使用x”来代替它之间的任何东西.谢谢!

解决方法 如果有兴趣知道如何使用bouncy castle openPGP库对gpg文件进行加密和解密,请查看以下java代码:

以下是您需要的4种方法:

以下方法将从.asc文件中读取并导入您的密钥:

@SuppressWarnings("rawtypes")    public static PGPPublicKey readPublicKeyFromCol(inputStream in) throws IOException,PGPException {        in = PGPUtil.getDecoderStream(in);        PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in,new BcKeyFingerprintCalculator());        PGPPublicKey key = null;        Iterator rIt = pgpPub.getKeyRings();        while (key == null && rIt.hasNext()) {            PGPPublicKeyRing kRing = (PGPPublicKeyRing) rIt.next();            Iterator kIt = kRing.getPublicKeys();            while (key == null && kIt.hasNext()) {                PGPPublicKey k = (PGPPublicKey) kIt.next();                if (k.isEncryptionKey()) {                    key = k;                }            }        }        if (key == null) {            throw new IllegalArgumentException("Can't find encryption key in key ring.");        }        return key;    }

以下方法将从.asc文件读取和导入您的公钥:

public voID decryptfile(inputStream in,inputStream secKeyIn,inputStream pubKeyIn,char[] pass) throws IOException,PGPException,InvalIDCipherTextException {        Security.addProvIDer(new BouncyCastleProvIDer());        PGPPublicKey pubKey = readPublicKeyFromCol(pubKeyIn);        PGPSecretKey secKey = readSecretKeyFromCol(secKeyIn,pubKey.getKeyID());        in = PGPUtil.getDecoderStream(in);        JcaPGPObjectFactory pgpFact;        PGPObjectFactory pgpF = new PGPObjectFactory(in,new BcKeyFingerprintCalculator());        Object o = pgpF.nextObject();        PGPEncryptedDataList encList;        if (o instanceof PGPEncryptedDataList) {            encList = (PGPEncryptedDataList) o;        } else {            encList = (PGPEncryptedDataList) pgpF.nextObject();        }        Iterator<PGPPublicKeyEncryptedData> itt = encList.getEncryptedDataObjects();        PGPPrivateKey sKey = null;        PGPPublicKeyEncryptedData encP = null;        while (sKey == null && itt.hasNext()) {            encP = itt.next();            secKey = readSecretKeyFromCol(new fileinputStream("PrivateKey.asc"),encP.getKeyID());            sKey = secKey.extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvIDer()).build(pass));        }        if (sKey == null) {            throw new IllegalArgumentException("Secret key for message not found.");        }        inputStream clear = encP.getDataStream(new BcpublicKeyDataDecryptorFactory(sKey));        pgpFact = new JcaPGPObjectFactory(clear);        PGPCompressedData c1 = (PGPCompressedData) pgpFact.nextObject();        pgpFact = new JcaPGPObjectFactory(c1.getDataStream());        PGPliteralData ld = (PGPliteralData) pgpFact.nextObject();        ByteArrayOutputStream bOut = new ByteArrayOutputStream();        inputStream inLd = ld.getDataStream();        int ch;        while ((ch = inLd.read()) >= 0) {            bOut.write(ch);        }        //System.out.println(bOut.toString());        bOut.writeto(new fileOutputStream(ld.getfilename()));        //return bOut;    }    public static voID encryptfile(OutputStream out,String filename,PGPPublicKey encKey) throws IOException,NoSuchProvIDerException,PGPException {        Security.addProvIDer(new BouncyCastleProvIDer());        ByteArrayOutputStream bOut = new ByteArrayOutputStream();        PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);        PGPUtil.writefileToliteralData(comData.open(bOut),PGPliteralData.BINARY,new file(filename));        comData.close();        PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(new BcPGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.TRIPLE_DES).setSecureRandom(new SecureRandom()));        cPk.addMethod(new BcpublicKeyKeyEncryptionMethodGenerator(encKey));        byte[] bytes = bOut.toByteArray();        OutputStream cOut = cPk.open(out,bytes.length);        cOut.write(bytes);        cOut.close();        out.close();    }

以下2种解密和加密gpg文件的方法:

try {             decryptfile(new fileinputStream("encryptedfile.gpg"),new fileinputStream("PrivateKey.asc"),new fileinputStream("PublicKey.asc"),"yourKeyPassword".tochararray());            PGPPublicKey pubKey = readPublicKeyFromCol(new fileinputStream("PublicKey.asc"));            encryptfile(new fileOutputStream("encryptedfileOutput.gpg"),"fileToEncrypt.txt",pubKey);        } catch (PGPException e) {            fail("exception: " + e.getMessage(),e.getUnderlyingException());        }

现在这里是如何调用/运行上面的:

[+++] 总结

以上是内存溢出为你收集整理的获取GPG解密在Java(Bouncy Castle)中工作全部内容,希望文章能够帮你解决获取GPG解密在Java(Bouncy Castle)中工作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
获取GPG解密在Java(Bouncy Castle)中工作_系统运维_内存溢出

获取GPG解密在Java(Bouncy Castle)中工作

获取GPG解密在Java(Bouncy Castle)中工作,第1张

概述让我开始说我对这一切非常新鲜.我想要做的是使用 Java中的gpg来解密加密文件. 我成功完成了什么 >如果某位同事使用我的公钥和他的私钥对文件进行加密,并成功解密. >另一方面 >如果另一个同事尝试解密一个不适合他的文件:失败(如预期) 我的钥匙是这样生成的 (gpg –version告诉我我正在使用1.4.5,我正在使用Bouncy Castle 1.47) gpg –gen-ley 选择选项 让我开始说我对这一切非常新鲜.我想要做的是使用 Java中的gpg来解密加密文件.

我成功完成了什么

>如果某位同事使用我的公钥和他的私钥对文件进行加密,并成功解密.
>另一方面
>如果另一个同事尝试解密一个不适合他的文件:失败(如预期)

我的钥匙是这样生成的

(gpg –version告诉我我正在使用1.4.5,我正在使用Bouncy Castle 1.47)

gpg –gen-ley

选择选项“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 cipherat org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder.decryptSessionData(UnkNown Source)at org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder.access
public static PGPSecretKey readSecretKeyFromCol(inputStream in,long keyID) throws IOException,PGPException {    in = PGPUtil.getDecoderStream(in);    PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(in,new BcKeyFingerprintCalculator());    PGPSecretKey key = pgpSec.getSecretKey(keyID);    if (key == null) {        throw new IllegalArgumentException("Can't find encryption key in key ring.");    }    return key;}
0(UnkNown Source)at org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder.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.bouncycastle.jcajce.provIDer.asymmetric.elgamal.CipherSpi.engineInit(未知来源)
在org.bouncycastle.jcajce.provIDer.asymmetric.elgamal.CipherSpi.engineInit(未知来源)
在javax.crypto.Cipher.init(DashoA13 * ..)
在javax.crypto.Cipher.init(DashoA13 * ..)
… 8更多

我在这里开放了很多建议,从“不要使用gpg,使用x”来“不要使用d性城堡,使用x”来代替它之间的任何东西.谢谢!

解决方法 如果有兴趣知道如何使用bouncy castle openPGP库对gpg文件进行加密和解密,请查看以下java代码:

以下是您需要的4种方法:

以下方法将从.asc文件中读取并导入您的密钥:

@SuppressWarnings("rawtypes")    public static PGPPublicKey readPublicKeyFromCol(inputStream in) throws IOException,PGPException {        in = PGPUtil.getDecoderStream(in);        PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in,new BcKeyFingerprintCalculator());        PGPPublicKey key = null;        Iterator rIt = pgpPub.getKeyRings();        while (key == null && rIt.hasNext()) {            PGPPublicKeyRing kRing = (PGPPublicKeyRing) rIt.next();            Iterator kIt = kRing.getPublicKeys();            while (key == null && kIt.hasNext()) {                PGPPublicKey k = (PGPPublicKey) kIt.next();                if (k.isEncryptionKey()) {                    key = k;                }            }        }        if (key == null) {            throw new IllegalArgumentException("Can't find encryption key in key ring.");        }        return key;    }

以下方法将从.asc文件读取和导入您的公钥:

public voID decryptfile(inputStream in,inputStream secKeyIn,inputStream pubKeyIn,char[] pass) throws IOException,PGPException,InvalIDCipherTextException {        Security.addProvIDer(new BouncyCastleProvIDer());        PGPPublicKey pubKey = readPublicKeyFromCol(pubKeyIn);        PGPSecretKey secKey = readSecretKeyFromCol(secKeyIn,pubKey.getKeyID());        in = PGPUtil.getDecoderStream(in);        JcaPGPObjectFactory pgpFact;        PGPObjectFactory pgpF = new PGPObjectFactory(in,new BcKeyFingerprintCalculator());        Object o = pgpF.nextObject();        PGPEncryptedDataList encList;        if (o instanceof PGPEncryptedDataList) {            encList = (PGPEncryptedDataList) o;        } else {            encList = (PGPEncryptedDataList) pgpF.nextObject();        }        Iterator<PGPPublicKeyEncryptedData> itt = encList.getEncryptedDataObjects();        PGPPrivateKey sKey = null;        PGPPublicKeyEncryptedData encP = null;        while (sKey == null && itt.hasNext()) {            encP = itt.next();            secKey = readSecretKeyFromCol(new fileinputStream("PrivateKey.asc"),encP.getKeyID());            sKey = secKey.extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvIDer()).build(pass));        }        if (sKey == null) {            throw new IllegalArgumentException("Secret key for message not found.");        }        inputStream clear = encP.getDataStream(new BcpublicKeyDataDecryptorFactory(sKey));        pgpFact = new JcaPGPObjectFactory(clear);        PGPCompressedData c1 = (PGPCompressedData) pgpFact.nextObject();        pgpFact = new JcaPGPObjectFactory(c1.getDataStream());        PGPliteralData ld = (PGPliteralData) pgpFact.nextObject();        ByteArrayOutputStream bOut = new ByteArrayOutputStream();        inputStream inLd = ld.getDataStream();        int ch;        while ((ch = inLd.read()) >= 0) {            bOut.write(ch);        }        //System.out.println(bOut.toString());        bOut.writeto(new fileOutputStream(ld.getfilename()));        //return bOut;    }    public static voID encryptfile(OutputStream out,String filename,PGPPublicKey encKey) throws IOException,NoSuchProvIDerException,PGPException {        Security.addProvIDer(new BouncyCastleProvIDer());        ByteArrayOutputStream bOut = new ByteArrayOutputStream();        PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);        PGPUtil.writefileToliteralData(comData.open(bOut),PGPliteralData.BINARY,new file(filename));        comData.close();        PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(new BcPGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.TRIPLE_DES).setSecureRandom(new SecureRandom()));        cPk.addMethod(new BcpublicKeyKeyEncryptionMethodGenerator(encKey));        byte[] bytes = bOut.toByteArray();        OutputStream cOut = cPk.open(out,bytes.length);        cOut.write(bytes);        cOut.close();        out.close();    }

以下2种解密和加密gpg文件的方法:

try {             decryptfile(new fileinputStream("encryptedfile.gpg"),new fileinputStream("PrivateKey.asc"),new fileinputStream("PublicKey.asc"),"yourKeyPassword".tochararray());            PGPPublicKey pubKey = readPublicKeyFromCol(new fileinputStream("PublicKey.asc"));            encryptfile(new fileOutputStream("encryptedfileOutput.gpg"),"fileToEncrypt.txt",pubKey);        } catch (PGPException e) {            fail("exception: " + e.getMessage(),e.getUnderlyingException());        }

现在这里是如何调用/运行上面的:

总结

以上是内存溢出为你收集整理的获取GPG解密在Java(Bouncy Castle)中工作全部内容,希望文章能够帮你解决获取GPG解密在Java(Bouncy Castle)中工作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1048471.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存