【问题标题】:How to convert Base64 encoded pkcs12 content to java.security.PrivateKey?如何将 Base64 编码的 pkcs12 内容转换为 java.security.PrivateKey?
【发布时间】:2014-06-08 18:48:30
【问题描述】:

我正在通过服务帐户使用 Google Directory API,并且在创建服务帐户时收到了 pkcs12 密钥。

Google 确实支持两种不同的方式来使用它,将密钥设为 java.io.Filejava.security.PrivateKey,对于 PoC,我使用了第一种方式,即使用 java.io.File 创建 GoogleCredential 对象,

        GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(httpTransport)
                .setJsonFactory(jsonFactory)
                .setServiceAccountId(serviceAccountId)
                .setServiceAccountScopes(Arrays.asList(DirectoryScopes.ADMIN_DIRECTORY_USER))
                .setServiceAccountUser(serviceAccountUser)
                .setServiceAccountPrivateKeyFromP12File(serviceAccountPrivateKeyFile)
                .build();

它按预期工作,但在我的实际用例中,我不能依赖文件系统,所以我不能使用第一种方法。所以我想用第二种方式实现实际用例,即使用java.security.PrivateKey,完成后看起来像下面这样。

    GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(jsonFactory)
            .setServiceAccountId(serviceAccountId)
            .setServiceAccountScopes(Arrays.asList(DirectoryScopes.ADMIN_DIRECTORY_USER))
            .setServiceAccountUser(serviceAccountUser)
            .setServiceAccountPrivateKey(serviceAccountPrivateKey)
            .build();

我是我的用例,我需要上传私钥并使用 base64 编码将其存储在数据库中。现在我需要传递pkcs12 键的内容并创建Googlecredential 对象。要做到这一点,我认为第二个选项是最合适的方式,但找不到任何示例来从上传的密钥的 base64 编码内容创建 java.security.PrivateKey。

是否可以从 pkcs12 键的 base64 编码内容创建 java.security.PrivateKey 对象?

或者还有其他方法可以实现我的用例吗?

提前致谢

达雷

【问题讨论】:

    标签: java google-api-java-client private-key google-admin-sdk pkcs#12


    【解决方案1】:

    java.securty.KeyStore 采用任何 InputStream 作为 load() 方法,因此您可以使用任何获取 .p12 字节的方法来创建它。这是我使用的另一种方法。虽然这仍然使用 .p12 字节的文件,但您可以引入 ByteArrayInputStream 或任何 InputStream 子类:

        private PrivateKey getPrivateKeyFromP12() {
            // Google p12 files all have "notasecret" as the pass and "privatekey" as the PK name
            String p12p = "notasecret";    // not cool!  Change this before exporting your .p12
            try {
                KeyStore keystore = KeyStore.getInstance("PKCS12");
                // This is where you'd adjust to bring in your .p12 bytes or chars
                // as an input stream.  Passwords are a char array!
                keystore.load(
                    this.getClass().getClassLoader()
                            .getResourceAsStream("the.p12"),
                    p12p.toCharArray());
                // This key name is fixed by Google, but could be changed
                PrivateKey key = (PrivateKey) keystore.getKey("privatekey",
                    p12p.toCharArray());
                return key;
            } catch (Exception e) {
                LOG.error("Exception while trying to obtain private key",e);
                return null;
            } 
    
        }
    

    【讨论】:

      猜你喜欢
      • 2011-04-27
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多