use of java.security.interfaces.RSAPublicKey in project android_frameworks_base by DirtyUnicorns.
the class AndroidKeyPairGeneratorTest method assertKeyPairCorrect.
private void assertKeyPairCorrect(KeyPair pair, String alias, String keyType, int keySize, AlgorithmParameterSpec spec, X500Principal dn, BigInteger serial, Date start, Date end) throws Exception {
final PublicKey pubKey = pair.getPublic();
assertNotNull("The PublicKey for the KeyPair should be not null", pubKey);
assertEquals(keyType, pubKey.getAlgorithm());
if ("EC".equalsIgnoreCase(keyType)) {
assertEquals("Curve should be what was specified during initialization", keySize, ((ECPublicKey) pubKey).getParams().getCurve().getField().getFieldSize());
} else if ("RSA".equalsIgnoreCase(keyType)) {
RSAPublicKey rsaPubKey = (RSAPublicKey) pubKey;
assertEquals("Modulus size should be what is specified during initialization", (keySize + 7) & ~7, (rsaPubKey.getModulus().bitLength() + 7) & ~7);
if (spec != null) {
RSAKeyGenParameterSpec params = (RSAKeyGenParameterSpec) spec;
assertEquals((keySize + 7) & ~7, (params.getKeysize() + 7) & ~7);
assertEquals(params.getPublicExponent(), rsaPubKey.getPublicExponent());
}
}
final PrivateKey privKey = pair.getPrivate();
assertNotNull("The PrivateKey for the KeyPair should be not null", privKey);
assertEquals(keyType, privKey.getAlgorithm());
if ("EC".equalsIgnoreCase(keyType)) {
assertTrue("EC private key must be instanceof ECKey: " + privKey.getClass().getName(), privKey instanceof ECKey);
assertEquals("Private and public key must have the same EC parameters", ((ECKey) pubKey).getParams(), ((ECKey) privKey).getParams());
} else if ("RSA".equalsIgnoreCase(keyType)) {
assertTrue("RSA private key must be instance of RSAKey: " + privKey.getClass().getName(), privKey instanceof RSAKey);
assertEquals("Private and public key must have the same RSA modulus", ((RSAKey) pubKey).getModulus(), ((RSAKey) privKey).getModulus());
}
final byte[] userCertBytes = mAndroidKeyStore.get(Credentials.USER_CERTIFICATE + alias);
assertNotNull("The user certificate should exist for the generated entry", userCertBytes);
final CertificateFactory cf = CertificateFactory.getInstance("X.509");
final Certificate userCert = cf.generateCertificate(new ByteArrayInputStream(userCertBytes));
assertTrue("Certificate should be in X.509 format", userCert instanceof X509Certificate);
final X509Certificate x509userCert = (X509Certificate) userCert;
assertEquals("Public key used to sign certificate should have the same algorithm as in KeyPair", pubKey.getAlgorithm(), x509userCert.getPublicKey().getAlgorithm());
assertEquals("PublicKey used to sign certificate should match one returned in KeyPair", pubKey, AndroidKeyStoreProvider.getAndroidKeyStorePublicKey(Credentials.USER_PRIVATE_KEY + alias, KeyStore.UID_SELF, x509userCert.getPublicKey().getAlgorithm(), x509userCert.getPublicKey().getEncoded()));
assertEquals("The Subject DN should be the one passed into the params", dn, x509userCert.getSubjectDN());
assertEquals("The Issuer DN should be the same as the Subject DN", dn, x509userCert.getIssuerDN());
assertEquals("The Serial should be the one passed into the params", serial, x509userCert.getSerialNumber());
assertDateEquals("The notBefore date should be the one passed into the params", start, x509userCert.getNotBefore());
assertDateEquals("The notAfter date should be the one passed into the params", end, x509userCert.getNotAfter());
// Assert that the cert's signature verifies using the public key from generated KeyPair
x509userCert.verify(pubKey);
// Assert that the cert's signature verifies using the public key from the cert itself.
x509userCert.verify(x509userCert.getPublicKey());
final byte[] caCerts = mAndroidKeyStore.get(Credentials.CA_CERTIFICATE + alias);
assertNull("A list of CA certificates should not exist for the generated entry", caCerts);
ExportResult exportResult = mAndroidKeyStore.exportKey(Credentials.USER_PRIVATE_KEY + alias, KeymasterDefs.KM_KEY_FORMAT_X509, null, null);
assertEquals(KeyStore.NO_ERROR, exportResult.resultCode);
final byte[] pubKeyBytes = exportResult.exportData;
assertNotNull("The keystore should return the public key for the generated key", pubKeyBytes);
assertTrue("Public key X.509 format should be as expected", Arrays.equals(pubKey.getEncoded(), pubKeyBytes));
}
use of java.security.interfaces.RSAPublicKey in project tech by ffyyhh995511.
the class RASUtil method loadPublicKeyByStr.
/**
* 公钥字符串转换为公钥对象
* @param publicKeyStr
* @return
* @throws Exception
*/
public static RSAPublicKey loadPublicKeyByStr(String publicKeyStr) throws Exception {
try {
byte[] buffer = decryptBASE64(publicKeyStr);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
return (RSAPublicKey) keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
throw new Exception("无此算法");
} catch (InvalidKeySpecException e) {
throw new Exception("公钥非法");
} catch (NullPointerException e) {
throw new Exception("公钥数据为空");
}
}
use of java.security.interfaces.RSAPublicKey in project tech by ffyyhh995511.
the class RASUtil method initKey.
/**
* 初始化
* @return
* @throws Exception
*/
public static Map<String, Object> initKey() throws Exception {
// 获得对象 KeyPairGenerator 参数 RSA 1024个字节
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGen.initialize(1024);
// 通过对象 KeyPairGenerator 获取对象KeyPair
KeyPair keyPair = keyPairGen.generateKeyPair();
// 通过对象 KeyPair 获取RSA公私钥对象RSAPublicKey RSAPrivateKey
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
// 公私钥对象存入map中
Map<String, Object> keyMap = new HashMap<String, Object>(2);
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
}
use of java.security.interfaces.RSAPublicKey in project cxf by apache.
the class JwkUtilsTest method testFromToPublicRsaKey2.
@Test
public void testFromToPublicRsaKey2() throws Exception {
BigInteger n = new BigInteger("525569531153621228164069013206963023039121751335221395180741421479892725873020691336158448746650762107595" + "8352148531548486906896903886764928450353366890712125983926472500064566992690642117517954169974907061547" + "3353190040609042090075291281955112293781438730376121249764205272939686534594208819023639183157456093565" + "4148815673814517535941780340023556224072529306118783149589148262622268860151306096159642808944513667279" + "4704664637866917427597486905443676772669967766269923280637049233876979061993814679654208850149406432368" + "2161337544093644200063709176660451323844399667162451308704624790051211834667782115390754507376506824717" + "9938484919159962066058375588059543574624283546151162925649987580839763809787286157381728046746195701379" + "0902293850442561995774628930418082115864728330723111110174368232384797709242627319756376556142528218939" + "7783875183123336240582938265783686836202210705597100765098627429017295706176890505466946207401105614189" + "2784165813507235148683348014201150784998715061575093867666453332433607035581378251824779499939486011300" + "7245546797308586043310145338620953330797301627631794650975659295961069452157705404946866414340860434286" + "65874725802069389719375237126155948350679342167596471110676954951640992376889874630989205394080379", 10);
BigInteger e = new BigInteger("65537", 10);
RSAPublicKey publicKey = CryptoUtils.getRSAPublicKey(n, e);
JsonWebKey jwk1 = JwkUtils.fromRSAPublicKey(publicKey, KeyAlgorithm.RSA_OAEP_256.getJwaName());
assertNotNull(jwk1.getProperty(JsonWebKey.RSA_PUBLIC_EXP));
assertNull(jwk1.getProperty(JsonWebKey.RSA_PRIVATE_EXP));
RSAPublicKey privateKey2 = JwkUtils.toRSAPublicKey(jwk1);
assertEquals(privateKey2, publicKey);
}
use of java.security.interfaces.RSAPublicKey in project cxf by apache.
the class JwkUtilsTest method testRsaKeyModulus.
@Test
public void testRsaKeyModulus() throws Exception {
JsonWebKey jwk = JwkUtils.readJwkKey(RSA_KEY);
String modulus = jwk.getStringProperty(JsonWebKey.RSA_MODULUS);
assertEquals(256, JoseUtils.decode(modulus).length);
RSAPublicKey pk = JwkUtils.toRSAPublicKey(jwk);
JsonWebKey jwk2 = JwkUtils.fromRSAPublicKey(pk, jwk.getAlgorithm());
String modulus2 = jwk2.getStringProperty(JsonWebKey.RSA_MODULUS);
assertEquals(256, JoseUtils.decode(modulus2).length);
assertEquals(modulus2, modulus);
}
Aggregations