【发布时间】:2020-06-11 20:15:00
【问题描述】:
有谁知道在 Java 中的充气城堡中什么相当于以下命令 (GnuPG 2.17.2)?
gpg -e -r "recipient" --output output.gpg input.zip
【问题讨论】:
标签: java encryption bouncycastle pgp
有谁知道在 Java 中的充气城堡中什么相当于以下命令 (GnuPG 2.17.2)?
gpg -e -r "recipient" --output output.gpg input.zip
【问题讨论】:
标签: java encryption bouncycastle pgp
创建证书和私钥
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 类创建一个 OutputEncrytor 对象,使用 AES CBC 算法。
加密器稍后用于生成封装加密消息的 CMSEnvelopedData 对象。
最后,信封的编码表示以字节数组的形式返回。
获取写入文件的最终字节数组。
【讨论】: