我们正在尝试在我们的 ios 应用程序中添加 Sign In with Apple。当客户端工作正常时,我们的后端是用 Java 编写的,我们无法解码 Apple 的公钥。当您点击 URL https://appleid.apple.com/auth/keys时,它会为您提供公钥。但是,当我尝试将PublicKey
对象设为 Java 时,它无法从那里识别n
ande
值。那些是 Base64 编码的吗?
当我尝试解码Base64 中的n
and值时,它给了我. 我怀疑它是 base64 的原因是在 NodeJS 包(https://www.npmjs.com/package/node-rsa)中,它们正在通过 base64 解码 n 和 e 值。但问题是指数值(e)永远不能是base64。如何从中创建 PublicKey 对象?e
illegal character 2d
AQAB
我正在使用的代码是:
HttpResponse<String> responsePublicKeyApple = Unirest.get("https://appleid.apple.com/auth/keys").asString();
ApplePublicKeyResponse applePublicKeyResponse = new Gson().fromJson(responsePublicKeyApple.getBody(),ApplePublicKeyResponse.class);
System.out.println("N: "+applePublicKeyResponse.getKeys().get(0).getN());
System.out.println("E: "+applePublicKeyResponse.getKeys().get(0).getE());
byte[] decodedBytesE = Base64.getDecoder().decode(applePublicKeyResponse.getKeys().get(0).getE());
String decodedE = new String(decodedBytesE);
System.out.println("decode E: "+decodedE);
byte[] decodedBytesN = Base64.getDecoder().decode(applePublicKeyResponse.getKeys().get(0).getN());
String decodedN = new String(decodedBytesN);
System.out.println("decode N: "+decodedN);
BigInteger bigIntegerN = new BigInteger(decodedN,16);
BigInteger bigIntegerE = new BigInteger(decodedE,16);
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(bigIntegerN,bigIntegerE);
KeyFactory keyFactory = KeyFactory.getInstance(SignatureAlgorithm.RS256.getValue());
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
它无法解码部分n
和e
值。另一件事是苹果的回应是说他们使用RS256
算法来签署令牌但是当我尝试这样做时
KeyFactory keyFactory = KeyFactory.getInstance(SignatureAlgorithm.RS256.getValue());
它说RS256 keyfactory is not available
。
我该如何解决这两个问题?请帮忙。