当前位置: 首页 > 知识库问答 >
问题:

用Java加载加密的PCKS8 PEM私钥

范云
2023-03-14

我希望加载/使用Java应用程序中提供给我的加密私钥。请找到密钥(加密的私有密钥、解密的私有密钥和公钥如下所示)。

加密的私钥密码:“AWCTJPET9FL7UBTP97HX99GDOFEWKUF5TUXSUJEST2SEKYVKYINRFRJ6EISUTERF”

密钥是在NodeJS中使用加密生成的,使用:

generateKeyPairSync( "rsa", {
        modulusLength: 4096,
        publicKeyEncoding: {
          type: "spki",
          format: "pem"
        },
        privateKeyEncoding: {
          type: "pkcs8",
          format: "pem",
          cipher: "aes-256-cbc",
          passphrase: "aWCTJPET9fL7UBTp97hX99gdofeWKUf5tuxSuJeST2sEkyvkyinrfrj6EiSUTErF"
        }
      } )

Note: enforcing padding in encrypt and decrypt padding: RSA_PKCS1_PADDING

我正在使用:运行时版本:11.0.7+10-B765.64amd64,OpenJDK 64位

我找到的最好/最接近的参考资料是:如何用Java读取密码加密密钥?

java.security.NoSuchAlgorithmException: Cannot find any provider supporting 1.2.840.113549.1.5.13

我在2020-10-30 09:27找到一个错误报告,上面说它已经解决了:https://bugs.openjdk.java.net/browse/jdk-8076999

那么问题是,如何将给定的加密私钥(PEM)加载到Java应用程序中,使其能够被使用呢?附近有工作吗?

网上有很多关于使用Bouncy Castle库的东西,但我也找不到一个工作的例子。我没有限制代码明智。如果可能,请提供一个例子。

String privKeyStrBase64Encoded = "THE ENCRYPTED PRIVATE KEY SHOWN FURTHER DOWN"
        privKeyStrBase64Encoded = privKeyStrBase64Encoded.replace("-----BEGIN ENCRYPTED PRIVATE KEY-----", "");
        privKeyStrBase64Encoded = privKeyStrBase64Encoded.replaceAll(System.lineSeparator(), "");
        privKeyStrBase64Encoded = privKeyStrBase64Encoded.replace("-----END ENCRYPTED PRIVATE KEY-----", "");

        byte[] encryptedPKInfo =  Base64.getDecoder().decode(privKeyStrBase64Encoded);
        EncryptedPrivateKeyInfo ePKInfo = new EncryptedPrivateKeyInfo(encryptedPKInfo);
        char[] password = "aWCTJPET9fL7UBTp97hX99gdofeWKUf5tuxSuJeST2sEkyvkyinrfrj6EiSUTErF".toCharArray();
        
        Cipher cipher = Cipher.getInstance(ePKInfo.getAlgName()); //Cannot find any provider supporting 1.2.840.113549.1.5.13
        PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
        // Now create the Key from the PBEKeySpec
        SecretKeyFactory skFac = SecretKeyFactory.getInstance(ePKInfo.getAlgName());
        Key pbeKey = skFac.generateSecret(pbeKeySpec);
        // Extract the iteration count and the salt
        AlgorithmParameters algParams = ePKInfo.getAlgParameters();
        cipher.init(Cipher.DECRYPT_MODE, pbeKey, algParams);
        // Decrypt the encryped private key into a PKCS8EncodedKeySpec
        KeySpec pkcs8KeySpec = ePKInfo.getKeySpec(cipher);
        // Now retrieve the RSA Public and private keys by using an
        // RSA keyfactory.
        KeyFactory rsaKeyFac = KeyFactory.getInstance("RSA");
        // First get the private key
        RSAPrivateCrtKey rsaPriv = (RSAPrivateCrtKey) rsaKeyFac.generatePrivate(pkcs8KeySpec);
        // Now derive the RSA public key from the private key
        RSAPublicKeySpec rsaPubKeySpec = new RSAPublicKeySpec(rsaPriv.getModulus(), rsaPriv.getPublicExponent());
        RSAPublicKey rsaPubKey = (RSAPublicKey) rsaKeyFac.generatePublic(rsaPubKeySpec);
    }
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIJrTBXBgkqhkiG9w0BBQ0wSjApBgkqhkiG9w0BBQwwHAQIRZpyCAQQR8oCAggA
MAwGCCqGSIb3DQIJBQAwHQYJYIZIAWUDBAEqBBC8h4heO8aR5NaJq5o89myBBIIJ
UDo2+MIuXH+y+hB9fx1kVe7yIogh0DEeYTnTLAraeg2yudSoNLq4xF2Ftid7Ax9Q
6BwdrY6YB8/Pk9vn7gWBoTdrfUpJZZu6DtLu7URXPTN5mgRNmJzC4uAv9y+T8CRv
sLXjwYOSbF4Vsbm8JlgJPuKPKkH1lTi3t1jxik+nNrDyAXRJOYfowkW9xnGqNSx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P5rcmdI5vnBI
7MYEhBqo+QWaF1fy4I4L8UMl1Kzqh4bc4s4w9r2d+JDHS7uwZBY9iwRSD4ChJyzH
GYBhnCNBmHz78F+hTlGrxLgGGouQXTxsGu1XUTUPqUgDXPRuD+plKtiJ10VADBsL
/WQaIbJF7CoWWvZNDqd1kiBNDdUmTfSw/VIIUjLuJpJIsZlnK3PY0/nA0FWBtK71
YsKNM/QFSQ6VQ78tMd0BCPEaMxCHaabsas8QZE4zteSKAJ5EigmggcjtiP63cZnp
iijX6av6myULqz94+yD4qyTGWyiA1JtqQACn6yBHhyUbs4y5/xTwGy9nFi0LI4IA
mmFPquGOLG1Vsy6b9KCoq9HiCjPPx+S28g76JDTC5cq2sU0lPJRGQfhIzUsDW7Pf
U0+rnUQRxi+rznMEsllj1uDQa1T608IjVRyF93vl4ni0QOY7ZYLH1qiK2YSAbhQ3
4krHOc7bUbWy7MyNRF1l+jcmgbxceC9Y/FhKyoZT2FSYHlOG/P95osLGXKjkAQYl
jVTPIlD4+5ZNhWLyfzDwlAMCAwEAAQ==
-----END PUBLIC KEY-----

共有1个答案

狄令
2023-03-14

https://bugs.openjdk.java.net/browse/jdk-8076999不适用于您的问题;它是关于解析PBES2参数的,而不是关于实现PBES2算法的。根据https://bugs.openjdk.java.net/browse/jdk-8202837底部的链接,该问题实际上在2018年11.0.1 up中得到了解决(至少对于使用AES的PBES2,这是您所需要的),其中包括您的14.0.2,您的新的EncryptedPrivateKeyInfo没有失败就证明了这一点。

但是Oracle/OpenJDK(从14开始)不通过名称或OID实现通用的PBES2(keyfactory和/或Cipher),BouncyCastle也不实现(从1.66开始)。前者确实通过名称实现了一些特定的PBES2 AES方案,包括您需要的方案,但您必须在parameters对象内部挖掘才能找到该名称,如下所示:

public static void main (String[] args) throws Exception {
    //String file = args[0], pw = args[1];
    String file = "n:66286457.pem", pw = "aWCTJPET9fL7UBTp97hX99gdofeWKUf5tuxSuJeST2sEkyvkyinrfrj6EiSUTErF";
    String pem = new String(Files.readAllBytes(Paths.get(file)));
    String b64 = pem.replaceAll("-----(BEGIN|END) ENCRYPTED PRIVATE KEY-----|\r?\n","");
    byte[] der = Base64.getDecoder().decode(b64);
    
    // Oracle/OpenJDK param parsing for PBES2 only works in 11.0.1 up 
    // and currently only for AES-128,256 see JDK-8076999,JDK-8202837
    EncryptedPrivateKeyInfo eki = new EncryptedPrivateKeyInfo(der);
    if( !eki.getAlgName().equals("1.2.840.113549.1.5.13") ) 
        throw new Exception ("not PBES2"); // or other handling 
    AlgorithmParameters top = eki.getAlgParameters();
    // hack to get nonpublic field and class, may break if Java keeps getting stricter
    Class<?> clazz = top.getClass();
    Object spi = access(clazz,"paramSpi").get(top);
    clazz = Class.forName("com.sun.crypto.provider.PBES2Parameters");
    String spiname = (String) access(clazz,"pbes2AlgorithmName").get(spi);

    SecretKeyFactory fact = SecretKeyFactory.getInstance(spiname);
    SecretKey skey = fact.generateSecret(new PBEKeySpec(pw.toCharArray()));
    Cipher ciph = Cipher.getInstance(spiname); 
    ciph.init(Cipher.DECRYPT_MODE, skey, eki.getAlgParameters());

    KeyFactory fac2 = KeyFactory.getInstance("RSA");
    PrivateKey pkey = fac2.generatePrivate(eki.getKeySpec(ciph));

    System.out.println ( ((RSAPrivateKey)pkey).getModulus() ); // for test
}
static Field access (Class<?> clazz, String name) throws Exception {
    Field f = clazz.getDeclaredField(name); 
    f.setAccessible(true);
    return f;
}

顺便说一句:IV不是问题;这种情况下的AlgorithmParameters包装com.sun.crypto.provider.pbes2Parameters,它包括PBKDF2参数(salt和itercount)和加密参数(IV)。

BouncyCastle(bcpkix加bcprov)确实是一种简单且完全支持的方式;您可以直接解析PEM(不对其进行调整和base64解码),也可以解析(dePEMified或其他)DER,然后使用PKCS8EncryptedPrivateKeyInfo.DecryptPrivateKeyInfoJCEOpenSSLPKCS8DecryptorProviderBuilder以及JCAPEMKeyConverter(它只是通过适当的KeyFactor运行它,如果您愿意,可以手动执行)。对于PEM,请参见读取PEM格式的PKCS8:无法找到provider或对于DER,如何读取PKCS8加密的私钥,该私钥也是用BouncyCastle在DER中编码的?(披露:都是我的)。

 类似资料:
  • 问题内容: 我正在尝试使用RSA私钥加密某些内容。 我遵循以下示例:http : //www.junkheap.net/content/public_key_encryption_java, 但将其转换为使用私钥而不是公共密钥。遵循该示例,我认为我需要做的是: 读取DER格式的私钥 生成PCKS8EncodedKeySpec 从KeyFactory调用generatePrivate()获得一个私钥

  • 我已经用java代码生成了一个私钥,并将其保存为: 无法加载私钥9964:error:0d0680a8:asn1编码例程:asn1_check_tlen:错误标记:.\crypto\asn1\tasn_dec。c:1319:9964:error:0d06c03a:asn1编码例程:asn1_d2i_ex_primitial:nested asn1错误或:.\crypto\asn1\tasn_dec

  • 我试图通过RSA加密字符串。我也有java样本,但我不能正确地将其转换为c#。 RSA示例: 这是我在c#中生成的代码,但我的c#代码结果与javaCode结果不同。我的代码怎么了?

  • 这段代码的目的是在最终用户的机器上生成一个CSR,然后我对它进行签名,并用机器的MAC地址(和salt)对它进行加密,这样程序就只能在授权的机器上运行,而且只有授权的机器才能访问我的PostgreSql数据库。 建议?

  • 我在Java写一个安全的文件共享应用程序。一般的架构是这样的: 用户希望加密文件以在多个用户之间安全共享 应用程序在客户端上生成一个随机UUID,并将其用作AES 256密码,然后使用UUID对数据进行加密 然后使用每个人的公钥对UUID进行RSA加密。每个共享用户一次 每个加密的UUID数据包作为文件的一部分存储在自定义文件头中 然后将文件上载到其他人可以访问的服务器 用户可以使用各自的私钥读取

  • 并且我将这个函数称为用RSA公钥加密DSA密钥的函数: