java - 相当于充气城堡中的 pgp 命令

标签 java encryption bouncycastle pgp

有谁知道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/

相关文章:

java - 在 JFileChooser 中设置滚动条的 RTL 方向?

java - 如何在 Java 中构建读取 xml 文件的构造函数?

C#为DSA加密算法生成公钥和私钥

java - 在android中使用海绵城堡

c# - BouncycaSTLe 不能在 C# 中正确解密

java - 将 openssl 迁移到 java

java swing - 向我的 JTree 节点添加颜色

Java线程启动其他线程,每个线程写入数组

php - 将解密算法传递到MySql select语句中

bash - OpenSSL 解密 AES 256 位(base64)加密密码 - 错误的最终 block 长度