我正在尝试在 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/