java - 使用 Scala 或 Java 将 .p12 转换为 .pem 以在 AWS SNS 中注册

标签 java scala amazon-web-services amazon-sns

我正在尝试在 SNS 中注册我的移动应用程序。 Amazon API 需要 .pem 格式的 APNS 证书及其密码。我收到 p12 格式的证书二进制文件。我怎样才能以正确的方式转换它?或者我真的需要转换它吗?

这就是我到目前为止所得到的:

/**
 * @param principal For APNS, is certificate in .pem format.
 * @param credential For APNS, is private key.
 * @param platform APNS of FCM
 * @param appName the application name  
 */
def registerApp(principal: String, credential: String, platform: String, appName: String): String = {

val attributes = Map("PlatformPrincipal" -> principal, "PlatformCredential" -> credential)
val map = mapAsJavaMap(attributes.asInstanceOf[util.Map[String, String]])

val request = new CreatePlatformApplicationRequest()
  .withPlatform(platform)
  .withName(appName)
  .withAttributes(map)

amazonSNSClient.createPlatformApplicationAsync(request).get().getPlatformApplicationArn 

我做了一些研究,但没能找到一些非常简单的东西。我也无法使用控制台 keytool

我基本上需要的是实现这个的东西

openssl pkcs12 -in myFile.p12 -out myFile.pem -nodes

在 Java 的 Scala 中,但使用输入二进制。

最佳答案

(可能的替代答案)

您不需要 BouncycaSTLe 来读取 PKCS12,基本的 Java 加密就可以做到这一点。 BouncycaSTLe 确实提供了完整的、(大部分)方便的 PEM 功能,而基本的 Java 则没有,但这里您需要的具体功能并不难综合:

static void SO57695413PKCS12ToPEM (String[] args) throws Exception {
    KeyStore ks = KeyStore.getInstance("PKCS12"); 
    ks.load(new FileInputStream(args[0]), args[1].toCharArray());
    // should close but I'm being lazy here
    PrivateKey pkey = (PrivateKey) ks.getKey(args[2], args[1].toCharArray());
    Certificate cert = ks.getCertificate(args[2]);
    // or loop over/look at aliases if desired
    System.out.print("-----BEGIN PRIVATE KEY-----\r\n"
            + Base64.getMimeEncoder().encodeToString(pkey.getEncoded())
            + "\r\n-----END PRIVATE KEY-----\r\n");
    System.out.print("-----BEGIN CERTIFICATE-----\r\n"
            + Base64.getMimeEncoder().encodeToString(cert.getEncoded())
            + "\r\n-----END CERTIFICATE-----\r\n");
    // or put in a String etc as desired
}

关于java - 使用 Scala 或 Java 将 .p12 转换为 .pem 以在 AWS SNS 中注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57695413/

相关文章:

输入字符串的 java.lang.NumberFormatException

scala - 从 Scala 中的 yield 返回特定类型

scala - 无法在线安装 sbt(按照官方指南)

mysql - 如何在 scala/python 中将计算列添加到数据框中?

python - OSX 上的 'arch' 意外输出(使用 Mac M1 安装弹性 bean )

mysql - 如何设置15M+行的表键以获得高性能和低成本?

java调度程序不工作

java - 在 IntelliJ 中添加 Spring 框架支持 - 没有这样的选项

java - 如何将字节数组转换为其数值(Java)?

amazon-web-services - 使用 CDK diff 来区分管道中包含的资源