use*_*228 31 java encryption pgp
我想使用PGP密钥解密文件.
我已下载PGP密钥安装程序并已安装.使用它我创建了一个文本文件并使用PGP密钥加密了文本文件.
然后我得到了一个加密的.pgp扩展文件.现在我想使用PGP使用Java代码解密同一个文件.
在Java中,如何解密已使用PGP密钥加密的文本文件?
尝试看看这个主题.我刚刚看了一下它,但我认为这是你需要的. http://sloanseaman.com/wordpress/2012/05/13/revisited-pgp-encryptiondecryption-in-java/
小智 6
我用BounceCastle API和OpenPGP编写了一个完整的Java代码.在此源代码中,您将了解如何生成密钥对,加密和解密文件.看看:https://github.com/damico/OpenPgp-BounceCastle-Example
小智 6
您可以编写一个围绕GNU PGP的简单包装器,它基本上从Java执行GPG命令.
使用GNU PGP的优点是您不会被绑定到特定的库.第三方库在线提供的文档和支持数量不如其他加密方案丰富,因为大多数从命令行调用PGP.PGP密钥也将保留在一个公共位置,即用户特定的密钥环,而不是导出在多个文件中.
用于解密的GPG命令是
echo "password" | gpg --passphrase-fd 0 --output plaintext.txt --decrypt encrypted.gpg
Run Code Online (Sandbox Code Playgroud)
通过将passphrase-fd指定为0,您可以通过标准输入流提供密码.
以下是Java代码的外观 -
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()
}
Run Code Online (Sandbox Code Playgroud)
尝试查看 JCA CryptoSpec。我不确定 PGP,但我认为您可以找到适合您目的的提供商。
据我记得代码应该是这样的:
// 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();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
82718 次 |
| 最近记录: |