use of java.security.spec.RSAPublicKeySpec in project jdk8u_jdk by JetBrains.
the class RSANoLimit method main.
public static void main(String[] args) throws Exception {
boolean result = true;
Provider p = Security.getProvider("SunJCE");
System.out.println("Testing provider " + p.getName() + "...");
// correct value
if (Cipher.getMaxAllowedKeyLength("RSA") != Integer.MAX_VALUE) {
result = false;
System.out.println("Test#1 failed");
}
// Test#2: try initializing RSA cipher with 4096 key
String algo = "RSA";
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(MODULUS4096), new BigInteger(PUB4096));
KeyFactory kf = KeyFactory.getInstance(algo);
RSAPublicKey pubKey = (RSAPublicKey) kf.generatePublic(pubKeySpec);
Cipher c = Cipher.getInstance(algo + "/ECB/NoPadding", p);
try {
c.init(Cipher.ENCRYPT_MODE, pubKey);
} catch (InvalidKeyException ike) {
result = false;
System.out.println("Test#2 failed");
ike.printStackTrace();
}
if (result) {
System.out.println("All tests passed!");
} else {
throw new Exception("One or more test failed!");
}
}
use of java.security.spec.RSAPublicKeySpec in project JustAndroid by chinaltz.
the class AbRsa method getPrivateKey.
/**
* 使用N、d值还原私钥
*
* @param modulus
* @param privateExponent
* @return
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
*/
public static PrivateKey getPrivateKey(String modulus, String privateExponent) throws NoSuchAlgorithmException, InvalidKeySpecException {
BigInteger bigIntModulus = new BigInteger(modulus);
BigInteger bigIntPrivateExponent = new BigInteger(privateExponent);
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(bigIntModulus, bigIntPrivateExponent);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
use of java.security.spec.RSAPublicKeySpec in project oxTrust by GluuFederation.
the class ManageCertificateAction method getKeyPair.
private KeyPair getKeyPair(String fileName) {
KeyPair pair = null;
JCERSAPrivateCrtKey privateKey = null;
PEMParser r = null;
FileReader fileReader = null;
File keyFile = new File(getTempCertDir() + fileName.replace("crt", "key"));
if (keyFile.isFile()) {
try {
fileReader = new FileReader(keyFile);
r = new PEMParser(fileReader);
Object keys = r.readObject();
if (keys == null) {
log.error(" Unable to read keys from: " + keyFile.getAbsolutePath());
return null;
}
if (keys instanceof KeyPair) {
pair = (KeyPair) keys;
log.debug(keyFile.getAbsolutePath() + "contains KeyPair");
} else if (keys instanceof JCERSAPrivateCrtKey) {
privateKey = (JCERSAPrivateCrtKey) keys;
log.debug(keyFile.getAbsolutePath() + "contains JCERSAPrivateCrtKey");
BigInteger exponent = privateKey.getPublicExponent();
BigInteger modulus = privateKey.getModulus();
RSAPublicKeySpec publicKeySpec = new java.security.spec.RSAPublicKeySpec(modulus, exponent);
PublicKey publicKey = null;
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
publicKey = keyFactory.generatePublic(publicKeySpec);
} catch (Exception e) {
e.printStackTrace();
}
pair = new KeyPair(publicKey, privateKey);
} else {
log.error(keyFile.getAbsolutePath() + " Contains unsupported key type: " + keys.getClass().getName());
return null;
}
} catch (IOException e) {
log.error(e.getMessage(), e);
return null;
} finally {
try {
r.close();
fileReader.close();
} catch (Exception e) {
log.error(e.getMessage(), e);
return null;
}
}
} else {
log.error("Key file does not exist : " + keyFile.getAbsolutePath());
}
log.debug("KeyPair successfully extracted from: " + keyFile.getAbsolutePath());
return pair;
}
use of java.security.spec.RSAPublicKeySpec in project gocd by gocd.
the class HttpTestUtil method generateKeyPair.
private KeyPair generateKeyPair() {
try {
KeyPair seed = KeyPairGenerator.getInstance("RSA", "BC").generateKeyPair();
RSAPrivateKey privateSeed = (RSAPrivateKey) seed.getPrivate();
RSAPublicKey publicSeed = (RSAPublicKey) seed.getPublic();
KeyFactory fact = KeyFactory.getInstance("RSA", "BC");
RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(privateSeed.getModulus(), privateSeed.getPrivateExponent());
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(publicSeed.getModulus(), publicSeed.getPublicExponent());
return new KeyPair(fact.generatePublic(publicKeySpec), fact.generatePrivate(privateKeySpec));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of java.security.spec.RSAPublicKeySpec in project athenz by yahoo.
the class Crypto method extractPublicKey.
public static PublicKey extractPublicKey(PrivateKey privateKey) throws CryptoException {
// we only support RSA and ECDSA private keys
PublicKey publicKey = null;
switch(privateKey.getAlgorithm()) {
case RSA:
try {
KeyFactory kf = KeyFactory.getInstance(RSA, BC_PROVIDER);
RSAPrivateCrtKey rsaCrtKey = (RSAPrivateCrtKey) privateKey;
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(rsaCrtKey.getModulus(), rsaCrtKey.getPublicExponent());
publicKey = kf.generatePublic(keySpec);
} catch (NoSuchProviderException ex) {
LOG.error("extractPublicKey: RSA - Caught NoSuchProviderException exception: " + ex.getMessage());
throw new CryptoException(ex);
} catch (NoSuchAlgorithmException ex) {
LOG.error("extractPublicKey: RSA - Caught NoSuchAlgorithmException exception: " + ex.getMessage());
throw new CryptoException(ex);
} catch (InvalidKeySpecException ex) {
LOG.error("extractPublicKey: RSA - Caught InvalidKeySpecException exception: " + ex.getMessage());
throw new CryptoException(ex);
}
break;
case ECDSA:
try {
KeyFactory kf = KeyFactory.getInstance(ECDSA, BC_PROVIDER);
BCECPrivateKey ecPrivKey = (BCECPrivateKey) privateKey;
ECMultiplier ecMultiplier = new FixedPointCombMultiplier();
ECParameterSpec ecParamSpec = (ECParameterSpec) ecPrivKey.getParameters();
ECPoint ecPointQ = ecMultiplier.multiply(ecParamSpec.getG(), ecPrivKey.getD());
ECPublicKeySpec keySpec = new ECPublicKeySpec(ecPointQ, ecParamSpec);
publicKey = kf.generatePublic(keySpec);
} catch (NoSuchProviderException ex) {
LOG.error("extractPublicKey: ECDSA - Caught NoSuchProviderException exception: " + ex.getMessage());
throw new CryptoException(ex);
} catch (NoSuchAlgorithmException ex) {
LOG.error("extractPublicKey: ECDSA - Caught NoSuchAlgorithmException exception: " + ex.getMessage());
throw new CryptoException(ex);
} catch (InvalidKeySpecException ex) {
LOG.error("extractPublicKey: ECDSA - Caught InvalidKeySpecException exception: " + ex.getMessage());
throw new CryptoException(ex);
}
break;
default:
String msg = "Unsupported Key Algorithm: " + privateKey.getAlgorithm();
LOG.error("extractPublicKey: " + msg);
throw new CryptoException(msg);
}
return publicKey;
}
Aggregations