【问题标题】:Bouncy Castle CMS public key encryptionBouncy Castle CMS 公钥加密
【发布时间】:2018-10-06 11:48:52
【问题描述】:

我在这里按照示例进行操作:http://www.baeldung.com/java-bouncy-castle

我有几个问题:

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;
}

将此应用到我的真实世界场景中,我只有一个 RSA 公钥用于收件人,而不是整个 X509Certificate。我戳了一下,但我不知道我怎么能做到这一点。有可能吗?

另一件事是我看到 JceCMSEncryptorBuilder 需要一个 ASN1ObjectIdentifier。我们目前正在做这样的事情:

KeyGenerator cryptKeyGenerator = KeyGenerator.getInstance("AES", "BC");
cryptKeyGenerator.init(256);
Key encryptionKey = cryptKeyGenerator.generateKey();
Cipher symmetricCipher = Cipher.getInstance("AES/CTS/NoPadding", "BC");
symmetricCipher.init(Cipher.ENCRYPT_MODE, encryptionKey, new IvParameterSpec(ivBytes));

在 CMSAlgorithm 类中,我看不到任何 CTS 选项。我错过了什么还是有办法仍然使用 CTS?

【问题讨论】:

    标签: java encryption bouncycastle public-key-encryption


    【解决方案1】:

    我只有收件人的 RSA 公钥,而不是整个 X509Certificate

    KeyTransRecipientInfo structure of CMS EnvelopedData 可以使用 SubjectKeyIdentifier 值 sometimes present in the X.509/PKIX certificate as an extensionBouncy has an overloaded ctor for this case。由于您没有证书,如果收件人将使用证书,您将必须找出用于计算证书中的值的方法,或者尝试不同的猜测直到找到一个这行得通,或者如果您控制收件人,只需选择它(他们)将接受的一些值。

    org.bouncycastle.cert.X509ExtensionUtils 及其两个子类提供了计算这两个标准方案的方法,但我发现它们并不比直接做更方便。

    我们目前正在使用 ... AES/CTS/NoPadding ... 在 CMSAlgorithm 类中我没有看到任何 CTS 选项

    这不仅仅是 CMSAlgorithm 中的内容。有两个相关因素:

    • CMS/PKCS7 EnvelopedData 中使用的任何特定密码(在 JCA 术语中为转换)必须由 OID 和条件参数标识

    • 用于给定消息的密码必须得到发件人和收件人或所有收件人的支持。

    org.bouncycastle.cms.CMSAlgorithm 只是一个方便的密码纲要和其他一些东西,比如密钥协议,它们都有标准化的 OID 并由 BC 实现,后者实际上由 org.bouncycastle.cms.jcajce.EnvelopedDataHelperthe bc-native equivalent 控制,您可以看到仅支持支持的分组密码的 CBC 模式。 (两者都支持 RC4,但作为流密码它不使用任何模式。另外,RC4 现在非常不受欢迎。)

    我不记得曾经在 CTS 模式下看到过任何用于密码的标准化 OID。如果这是正确的,您将不得不分配一个,因为没有其他人会实现该 OID,您的消息将无法与任何人互操作。如果您可以找到您的对等方实现的标准 OID(或至少 AlgId),对于 BC,您必须创建自己的符合(接口)OutputEncryptor 的类,如果您鉴于您有底层密码的提供者或 bc-native 实现,请查看上面的来源。

    【讨论】:

      猜你喜欢
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-16
      • 1970-01-01
      • 2016-02-12
      相关资源
      最近更新 更多