如何通过Java的加密扩展名加密PGP消息? [英] How do I encrypt a PGP message through java's crypto extension?

查看:84
本文介绍了如何通过Java的加密扩展名加密PGP消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我使用的是Bouncy Castle的库进行实际工作,并在sloanseaman.com上找到了一个示例(经过一些调整)可与v1.52一起使用.

Currently I'm using bouncy castle's libraries for the actual work and have found an example at sloanseaman.com that (after a little tweaking) works with v1.52.

developer.com上也提供了一个有效的示例,说明如何使用JCE接口,甚至可以将bcprov放到其中,并使用其中的某些算法.

I've also got a working example from developer.com of how to use the JCE interface and can even drop the bcprov in it and use some of it's algorithms.

public class CryptoUtil {
private static final String ALGORITHM = "IDEA/PGP/NoPadding";

public static void encryptFile(File keyFile, File plainTextFile, File encryptedFile) throws GeneralSecurityException, IOException {
    Cipher desCipher = Cipher.getInstance(ALGORITHM);
    desCipher.init(Cipher.ENCRYPT_MODE, readKeyFromFile(keyFile));
    OutputStream out = new BufferedOutputStream(new FileOutputStream(encryptedFile));
    InputStream in = new BufferedInputStream(new FileInputStream(plainTextFile));
    while (in.available() > 0) {
        // Read the next chunk of bytes...
        byte[] cleartextBytes = new byte[in.available()];
        in.read(cleartextBytes);
        // Now, encrypt them and write them to the encrypted file...
        byte[] encryptedBytes = desCipher.update(cleartextBytes);
        out.write(encryptedBytes, 0, encryptedBytes.length);
    }
    // Take care of any pending padding operations
    out.write(desCipher.doFinal());
    in.close();
    out.flush();
    out.close();

    System.out.println("Encrypted to " + encryptedFile);
}

但是无论我使用什么算法字符串,我都无法让我的JCE实用程序来加密bouncyCastle实用程序的方式.

But no matter what algorithm string I use, I can't get my JCE utility to encrypt the way that the bouncyCastle utility does.

我得到的最大的结果是使用"IDEA/PGP/NoPadding",它允许我在自己内部进行加密和解密,但是BC实用程序不会解密它们,说流中有一个未知对象.

The furthest I've gotten is using "IDEA/PGP/NoPadding" which allows me to encrypt and decrypt within itself, but the BC utility won't decrypt them, saying there's an unknown object in the stream.

此处是我的源代码

你们知道我需要为此使用哪种算法,模式和填充组合吗?我还需要以其他方式应用其他选项吗?我想我需要使用BC的AlgorithmParametersSpi版本,但是我还没有弄清楚如何创建它

Do you guys know what combination of Algorithm, Mode, and Padding I would need to use for this? Are there other options that I need to apply somehow? I guessing I need to use BC's version of AlgorithmParametersSpi but I haven't figured out how to create that yet

推荐答案

您不能.尽管OpenPGP使用常规"公共/专用和对称加密算法,但麻烦始于这些模式.OpenPGP使用其自己的模式(一种经过修改的CFB模式),以及整个OpenPGPJava的默认库不支持数据包语法.

You can't. While OpenPGP uses "normal" public/private and symmetric encryption algorithms, trouble starts with the modes. OpenPGP uses its own mode (a modified CFB mode), and also the whole OpenPGP packet syntax is not supported by Java's default libraries.

您至少需要在Java中重新实现OpenPGP CFB模式,或者以某种方式依赖Bouncy Castle的实现.

You'd at least need to reimplement the OpenPGP CFB mode in Java, or somehow rely on Bouncy Castle's implementation.

OpenPGP CFB模式已经包括初始化向量的替代;无需/无需使用其他填充.

The OpenPGP CFB mode already includes a replacement for the initialization vector; no additional padding is used/required.

这篇关于如何通过Java的加密扩展名加密PGP消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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