繁体   English   中英

PGP Java库进行加密/解密

[英]PGP java library for excryption/decryption

我需要解密使用PGP加密的文件。 我需要编写每日调度程序,并且需要相同的Java库支持。 我在互联网上进行搜索,但所有与bouncycastle相关的示例都非常古老,无法立即使用。 有人可以指导我使用替代库,还是可以指向最新代码示例以集成Castle API。

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

在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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM