有谁知道Java中的充气城堡中的以下命令(GnuPG 2.17.2)相当于什么?
gpg -e -r "recipient" --output output.gpg input.zip
最佳答案
创建证书和私钥
public static byte[] encryptData(byte[] data, X509Certificate encryptionCertificate) throws CertificateEncodingException, CMSException, IOException {
byte[] encryptedData = null;
if (null != data && null != encryptionCertificate) {
CMSEnvelopedDataGenerator cmsEnvelopedDataGenerator
= new CMSEnvelopedDataGenerator();
JceKeyTransRecipientInfoGenerator jceKey
= new JceKeyTransRecipientInfoGenerator(encryptionCertificate);
cmsEnvelopedDataGenerator.addRecipientInfoGenerator(transKeyGen);
CMSTypedData msg = new CMSProcessableByteArray(data);
OutputEncryptor encryptor
= new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES128_CBC)
.setProvider("BC").build();
CMSEnvelopedData cmsEnvelopedData = cmsEnvelopedDataGenerator
.generate(msg,encryptor);
encryptedData = cmsEnvelopedData.getEncoded();
}
return encryptedData;}
使用收件人的证书创建 JceKeyTransRecipientInfoGenerator 对象。
然后创建一个新的 CMSEnvelopedDataGenerator 对象并将收件人信息生成器添加到其中。
然后使用 JceCMSContentEncryptorBuilder 类使用 AES CBC 算法创建 OutputEncrytor 对象。
加密器稍后用于生成封装加密消息的 CMSEnvelopedData 对象。
最后,信封的编码表示形式作为字节数组返回。
获取写入文件的最终字节数组。
关于java - 相当于充气城堡中的 pgp 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60439626/