简体   繁体   English

PGP Java库进行加密/解密

[英]PGP java library for excryption/decryption

I need to decrypt file which is encrypted using PGP. 我需要解密使用PGP加密的文件。 I need to write a daily scheduler and need java library support for the same. 我需要编写每日调度程序,并且需要相同的Java库支持。 I searched on internet but all the examples related to bouncycastle are very old and not working now. 我在互联网上进行搜索,但所有与bouncycastle相关的示例都非常古老,无法立即使用。 Can someone please guide me to alternative library or point me to latest code samples to integrate castle APIs. 有人可以指导我使用替代库,还是可以指向最新代码示例以集成Castle API。

If you don't want to use any library, you can go for the following approach: 如果您不想使用任何库,则可以采用以下方法:

Using Runtime class in java: 在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;
}

Put the above code in any class and customize it according to your requirement.Ignore the logging and its related code. 将以上代码放在任何类中并根据您的要求对其进行自定义。忽略日志记录及其相关代码。 File name should be your complete path. 文件名应该是您的完整路径。 eg: /xyz/abc.pgp 例如:/xyz/abc.pgp

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

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