我们正在尝试在ios应用程序中添加“使用Apple登录”。当客户端运行良好时,我们的后端是用Java编写的,因此我们无法解码Apple的公钥。当您点击URL https://appleid.apple.com/auth/keys时,它将为您提供公共密钥。但是,当我尝试使PublicKey
对象为Java时,它无法从那里识别n
和e
的值。那些Base64编码了吗?
当我尝试在Base64中解码n
和e
值时,它给了我illegal character 2d
。我怀疑它是base64的原因是在NodeJS包(https://www.npmjs.com/package/node-rsa)中,它们正在通过base64解码n和e值。但是问题是指数值(e)是AQAB
,它永远不能是base64。如何从中创建PublicKey对象?
我使用的代码是:
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
。
如何解决这两个问题?请帮忙。