有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用BouncyCastle lightwigth API生成cms封装数据

经过无数个小时的挣扎,我终于有了当前的工作代码,可以使用JCE/JCA生成带有收件人信息的CMS信封(RSA-OAEP/PKCS#1)数据:

String digest = "SHA-256";
String mgfDigest = "SHA-256";

// Data to encrypt
CMSTypedData msg = new CMSProcessableByteArray(data);

// Generator for my CMS enveloped data 
CMSEnvelopedDataGenerator envelopedDataGen = new CMSEnvelopedDataGenerator();

// Recipient Info Stuff
JcaAlgorithmParametersConverter paramsConverter = new JcaAlgorithmParametersConverter();
OAEPParameterSpec oaepSpec = new OAEPParameterSpec(digest, "MGF1", new MGF1ParameterSpec(mgfDigest), PSource.PSpecified.DEFAULT);
AlgorithmIdentifier oaepAlgId = paramsConverter.getAlgorithmIdentifier(PKCSObjectIdentifiers.id_RSAES_OAEP, oaepSpec);
envelopedDataGen.addRecipientInfoGenerator(
        new JceKeyTransRecipientInfoGenerator(
                getCert(), 
                oaepAlgId).setProvider("BC"));

/*
  * Generate CMS-Data
  * CMSOutputEncryptor is my own Class implementing OutputEncryptor
  */
CMSEnvelopedData ed = envelopedDataGen.generate(
        msg, 
        new CMSOutputEncryptor());

byte[] encoded = ed.getEncoded();

这与预期的一样有效,但因为它使用JCE,我的客户需要安装无限强度api才能使用此代码。我更喜欢一种克服这些需求的方法,因为我的大多数客户都是拇指

也许有人可以给我看一段代码,它使用纯BouncyCastle的方法来做同样的事情,这样就不需要安装无限强度api了


共 (1) 个答案

  1. # 1 楼答案

    请注意,我不确定这在所有国家/所有客户中是否合法

    如果你想取消限制,你可以使用一些反射魔法。这是我在框架中的实现方式(部分摘自:https://github.com/jruby/jruby/blob/0c345e1b186bd457ebd96143c0816abe93b18fdf/core/src/main/java/org/jruby/util/SecurityHelper.java):

    import java.lang.reflect.Field;
    import java.lang.reflect.Modifier;
    import java.security.NoSuchAlgorithmException;
    import javax.crypto.Cipher;
    
    public class UnlimitedStrengthHelper {
    
        public static void removeCryptoStrengthRestriction() {
            try {
                if (Cipher.getMaxAllowedKeyLength("AES") < 256) {
                    Class jceSecurity = Class.forName("javax.crypto.JceSecurity");
                    Field isRestricted = jceSecurity.getDeclaredField("isRestricted");
                    if (Modifier.isFinal(isRestricted.getModifiers())) {
                        Field modifiers = Field.class.getDeclaredField("modifiers");
                        modifiers.setAccessible(true);
                        modifiers.setInt(isRestricted, isRestricted.getModifiers() & ~Modifier.FINAL);
                        modifiers.setAccessible(false);
                    }
                    isRestricted.setAccessible(true);
                    isRestricted.setBoolean(null, false);
                    isRestricted.setAccessible(false);
                }
            } catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException
                    | NoSuchAlgorithmException | NoSuchFieldException | SecurityException ex) {
                System.out.println("It is not possible to use unrestricted policy with this JDK, "
                        + "consider reconfiguration: " + ex.getLocalizedMessage());
            }
        }
    }
    

    代码首先检查限制是否存在(在这种情况下,您无法使用AES256)。然后,它接受JceSecurity类及其受限制的字段。它确保我们可以访问此字段,并最终将其值设置为false

    顺便说一句,谢谢你的CMS的例子,它帮助了我很多