5

我正在为 IOS 应用程序中使用的 Apple 登录功能实现服务器端部分。

为了验证 JWT,我需要使用公钥。我现在坚持如何从我从 Apple 获得的模数和指数创建公钥。

4

1 回答 1

6

要从指数和模数生成公钥,需要将它们转换为 BigInteger,然后可以使用 Java 安全性的 KeyFactory。

例如:


  String modulus = "modulus from Apple";
  String exponent = "exponent from Apple";
  byte[] modulusByte = Base64.getUrlDecoder().decode(modulus);

  BigInteger modulusAsBigInt = new BigInteger(1, modulusByte);
  byte[] exponentByte = Base64.getUrlDecoder().decode(exponent);
  BigInteger exponentAsBigInt = new BigInteger(1, exponentByte);

  RSAPublicKeySpec spec = new RSAPublicKeySpec(modulusAsBigInt, exponentAsBigInt);
  KeyFactory factory = KeyFactory.getInstance("RSA");
  PublicKey pub = factory.generatePublic(spec);

于 2019-08-02T15:44:32.867 回答