public void testPrivateKeyExportAndImport()
throws Exception {
RSAKey key = new RSAKey(new Base64URL(n), new Base64URL(e), new Base64URL(d),
new Base64URL(p), new Base64URL(q),
new Base64URL(dp), new Base64URL(dq), new Base64URL(qi),
null,
KeyUse.SIGNATURE, null, JWSAlgorithm.RS256, "1",
null, null, null);
// Private key export with CRT (2nd form)
RSAPrivateKey privKey = key.toRSAPrivateKey();
assertEquals(new Base64URL(n).decodeToBigInteger(), privKey.getModulus());
assertEquals(new Base64URL(d).decodeToBigInteger(), privKey.getPrivateExponent());
assertTrue(privKey instanceof RSAPrivateCrtKey);
RSAPrivateCrtKey privCrtKey = (RSAPrivateCrtKey)privKey;
assertEquals(new Base64URL(e).decodeToBigInteger(), privCrtKey.getPublicExponent());
assertEquals(new Base64URL(p).decodeToBigInteger(), privCrtKey.getPrimeP());
assertEquals(new Base64URL(q).decodeToBigInteger(), privCrtKey.getPrimeQ());
assertEquals(new Base64URL(dp).decodeToBigInteger(), privCrtKey.getPrimeExponentP());
assertEquals(new Base64URL(dq).decodeToBigInteger(), privCrtKey.getPrimeExponentQ());
assertEquals(new Base64URL(qi).decodeToBigInteger(), privCrtKey.getCrtCoefficient());
// Key pair export
KeyPair pair = key.toKeyPair();
RSAPublicKey pubKey = (RSAPublicKey)pair.getPublic();
assertEquals(new Base64URL(n).decodeToBigInteger(), pubKey.getModulus());
assertEquals(new Base64URL(e).decodeToBigInteger(), pubKey.getPublicExponent());
assertEquals("RSA", pubKey.getAlgorithm());
privKey = (RSAPrivateKey)pair.getPrivate();
assertEquals(new Base64URL(n).decodeToBigInteger(), privKey.getModulus());
assertEquals(new Base64URL(d).decodeToBigInteger(), privKey.getPrivateExponent());
assertTrue(privKey instanceof RSAPrivateCrtKey);
privCrtKey = (RSAPrivateCrtKey)privKey;
assertEquals(new Base64URL(e).decodeToBigInteger(), privCrtKey.getPublicExponent());
assertEquals(new Base64URL(p).decodeToBigInteger(), privCrtKey.getPrimeP());
assertEquals(new Base64URL(q).decodeToBigInteger(), privCrtKey.getPrimeQ());
assertEquals(new Base64URL(dp).decodeToBigInteger(), privCrtKey.getPrimeExponentP());
assertEquals(new Base64URL(dq).decodeToBigInteger(), privCrtKey.getPrimeExponentQ());
assertEquals(new Base64URL(qi).decodeToBigInteger(), privCrtKey.getCrtCoefficient());
// Key pair import, 1st private form
key = new RSAKey(pubKey, privKey, KeyUse.SIGNATURE, null, JWSAlgorithm.RS256, "1", null, null, null);
assertEquals(KeyUse.SIGNATURE, key.getKeyUse());
assertEquals(JWSAlgorithm.RS256, key.getAlgorithm());
assertEquals("1", key.getKeyID());
assertEquals(new Base64URL(n), key.getModulus());
assertEquals(new Base64URL(e), key.getPublicExponent());
assertEquals(new Base64URL(d), key.getPrivateExponent());
assertNull(key.getFirstPrimeFactor());
assertNull(key.getSecondPrimeFactor());
assertNull(key.getFirstFactorCRTExponent());
assertNull(key.getSecondFactorCRTExponent());
assertNull(key.getFirstCRTCoefficient());
assertTrue(key.getOtherPrimes().isEmpty());
assertTrue(key.isPrivate());
// Key pair import, 2nd private form
key = new RSAKey(pubKey, privCrtKey, KeyUse.SIGNATURE, null, JWSAlgorithm.RS256, "1", null, null, null);
assertEquals(KeyUse.SIGNATURE, key.getKeyUse());
assertEquals(JWSAlgorithm.RS256, key.getAlgorithm());
assertEquals("1", key.getKeyID());
assertEquals(new Base64URL(n), key.getModulus());
assertEquals(new Base64URL(e), key.getPublicExponent());
assertEquals(new Base64URL(d), key.getPrivateExponent());
assertEquals(new Base64URL(p), key.getFirstPrimeFactor());
assertEquals(new Base64URL(q), key.getSecondPrimeFactor());
assertEquals(new Base64URL(dp), key.getFirstFactorCRTExponent());
assertEquals(new Base64URL(dq), key.getSecondFactorCRTExponent());
assertEquals(new Base64URL(qi), key.getFirstCRTCoefficient());
assertTrue(key.getOtherPrimes().isEmpty());
assertTrue(key.isPrivate());
}