This is my code for generating the token.
but I get following error:
Code Block String rsaPrivateKey = "-----BEGIN PRIVATE KEY-----\n" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "-----END PRIVATE KEY-----"; rsaPrivateKey = rsaPrivateKey.replace("-----BEGIN PRIVATE KEY-----", ""); rsaPrivateKey = rsaPrivateKey.replace("-----END PRIVATE KEY-----", ""); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getMimeDecoder().decode(rsaPrivateKey)); KeyFactory kf = KeyFactory.getInstance("RSA"); PrivateKey privKey = kf.generatePrivate(keySpec); String token = Jwts.builder() .setHeaderParam(JwsHeader.ALGORITHM,"ES256") .setHeaderParam(JwsHeader.KEY_ID,"ABC123DEFG" ) // key id I got from Apple .setIssuer("DEF123GHIJ") .setAudience("https://appleid.apple.com") .setSubject("com.mytest.app") // app id com.app.id .setExpiration(new Date(System.currentTimeMillis() + expiration)) .setIssuedAt(new Date(System.currentTimeMillis())) .signWith(SignatureAlgorithm.ES256, privKey) // ECDSA using P-256 and SHA-256 .compact();
but I get following error:
Code Block Exception in thread "main" java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: Invalid RSA private key at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:217) at java.security.KeyFactory.generatePrivate(KeyFactory.java:372) at com.rjil.cloud.drive.utils.AppleCodeGenerator.main(AppleCodeGenerator.java:35) Caused by: java.security.InvalidKeyException: Invalid RSA private key at sun.security.rsa.RSAPrivateCrtKeyImpl.parseKeyBits(RSAPrivateCrtKeyImpl.java:214) at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:343) at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:356) at sun.security.rsa.RSAPrivateCrtKeyImpl.<init>(RSAPrivateCrtKeyImpl.java:91) at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:75) at sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:316) at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:213) ... 2 more Caused by: java.io.IOException: Version must be 0 at sun.security.rsa.RSAPrivateCrtKeyImpl.parseKeyBits(RSAPrivateCrtKeyImpl.java:192) ... 8 more