用于解密/解密的PGP java库

时间:2016-04-14 11:35:17

标签: java encryption cryptography bouncycastle

我需要解密使用PGP加密的文件。 我需要编写每日调度程序并需要Java库支持。我在互联网上搜索,但所有与bouncycastle相关的例子都很老,现在还没有用。有人可以引导我到替代库或指向我最新的代码示例以集成城堡API。

1 个答案:

答案 0 :(得分:0)

如果您不想使用任何库,可以采用以下方法:

在java中使用Runtime类:

static Runtime runtime;
static {
    runtime = Runtime.getRuntime();
}

public int decrypt(String passphrase, String encFileName, String newFileName) {
    int result = 1;
    StringBuffer output = new StringBuffer();
    try {
        String st = "pgp --decrypt --overwrite remove --passphrase " + passphrase
                + " --output " + newFileName + " " + encFileName;
        Process process = runtime.exec(st);
        logger.debug(st);
        result = process.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                process.getInputStream()));

        String line = "";
        while ((line = reader.readLine()) != null) {
            output.append(line + "\n");
        }
        logger.info(output);
    } catch (IOException e) {

        logger.error("IOError during decrypt" + e);
        e.printStackTrace();
    } catch (InterruptedException e) {
        logger.error("InterruptedException during decrypt" + e);
        e.printStackTrace();
    }
    return result;
}

将上述代码放在任何类中,并根据您的要求进行自定义。忽略日志记录及其相关代码。 文件名应该是您的完整路径。例如:/xyz/abc.pgp

相关问题