PGP Encryption and Decryption with Java [closed]

前提是你 提交于 2019-11-27 12:04:06

I've written a complete code in Java with BounceCastle API and OpenPGP. In this source code you will find how to generate the key pair, encrypt and decrypt files. Take a look at: https://github.com/damico/OpenPgp-BounceCastle-Example

You can write a simple wrapper around GNU PGP which basically executes the GPG command from Java.

The advantage of using GNU PGP is that you will not be tied to a specific library. The amount of documentation and support available online for third-party libraries is not as rich as other encryption schemes since most invoke PGP from command-line. The PGP keys will also stay in one common place i.e. the user specific key ring rather than exported in multiple files.

The GPG command for decryption is

echo "password" | gpg --passphrase-fd 0 --output plaintext.txt --decrypt encrypted.gpg

By specifying passphrase-fd as 0 you can provide the password through the standard input stream.

Here is how the Java code looks like -

public static void decryptFile(String privKeyPass) {
    String[] cmd = new String[];
    int i = 7;
    cmd[i++] = "gpg";
    cmd[i++] = "--passphrase-fd";
    cmd[i++] = "0";
    cmd[i++] = "--output";
    cmd[i++] = "plaintext.txt";
    cmd[i++] = "--decrypt";
    cmd[i++] = "encrypted.gpg";
    Process process = Runtime.getRuntime().exec(cmd);

    BufferedWriterout = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
    out.write(privKeyPass);
    try {
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
   // Read process.getInputStream()
   // Read process.getErrorStream()
}
MikhailSP

Try do see this topic. I have just taken a brief look at it, but I think it is what you need. http://sloanseaman.com/wordpress/2012/05/13/revisited-pgp-encryptiondecryption-in-java/

Eugene Mayevski 'Allied Bits

BouncyCastle has certain support for OpenPGP ("certain" because they mention only RFC 2440 and not RFC 4880 which is more recent). Also you can take a look at OpenPGPBlackbox package of our SecureBlackbox (Java edition), which provides complete support for OpenPGP including LDAP access to keys and other advanced functions.

Try to take a look to JCA CryptoSpec. I am not sure about PGP, but I think you can find there a Provider for your purpose.

As far as I remember code should be something like:

// get cipher object for password-based encryption
Cipher cipher1 = Cipher.getInstance("PBEWithMD5AndDES");//You have to pass here algorithm name which PGP uses. May be you have to find and init provider for it.

// initialize cipher for decryption, using one of the 
// init() methods that takes an AlgorithmParameters 
// object, and pass it the algParams object from above
cipher1.init(Cipher.DECRYPT_MODE, myKey, algParams);


FileInputStream fis;
FileOutputStream fos;
CipherInputStream cis;

fis = new FileInputStream("/tmp/a.txt");
cis = new CipherInputStream(fis, cipher1);
fos = new FileOutputStream("/tmp/b.txt");
byte[] b = new byte[8];
int i = cis.read(b);
while (i != -1) {
    fos.write(b, 0, i);
    i = cis.read(b);
}
fos.close();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!