简体   繁体   中英

PGP java library for excryption/decryption

I need to decrypt file which is encrypted using PGP. I need to write a daily scheduler and need java library support for the same. I searched on internet but all the examples related to bouncycastle are very old and not working now. Can someone please guide me to alternative library or point me to latest code samples to integrate castle APIs.

If you don't want to use any library, you can go for the following approach:

Using Runtime class in java:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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