use of java.security.spec.KeySpec in project robovm by robovm.
the class SubjectPublicKeyInfo method getPublicKey.
/**
* Returns the PublicKey corresponding to this SubjectPublicKeyInfo
* instance.
*/
public PublicKey getPublicKey() {
if (publicKey == null) {
final byte[] encoded = getEncoded();
final KeySpec keySpec = new X509EncodedKeySpec(encoded);
/* Try using the algorithm name first. */
final String algName = algorithmID.getAlgorithmName();
publicKey = generateKeyForAlgorithm(keySpec, algName);
/*
* Fall back to using the algorithm OID if it's not the same as the
* algorithm name.
*/
final String algOid = algorithmID.getAlgorithm();
if (publicKey == null && !algOid.equals(algName)) {
publicKey = generateKeyForAlgorithm(keySpec, algOid);
}
/*
* Encode this as an X.509 public key since we didn't have any
* KeyFactory that could handle this algorithm name or OID. Perhaps
* the thing that's using this can decode it.
*/
if (publicKey == null) {
publicKey = new X509PublicKey(algOid, encoded, subjectPublicKey);
}
}
return publicKey;
}
use of java.security.spec.KeySpec in project neo4j by neo4j.
the class Certificates method loadPrivateKey.
public PrivateKey loadPrivateKey(File privateKeyFile) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
try (PemReader r = new PemReader(new FileReader(privateKeyFile))) {
PemObject pemObject = r.readPemObject();
if (pemObject != null) {
byte[] encodedKey = pemObject.getContent();
KeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey);
try {
return KeyFactory.getInstance("RSA").generatePrivate(keySpec);
} catch (InvalidKeySpecException ignore) {
try {
return KeyFactory.getInstance("DSA").generatePrivate(keySpec);
} catch (InvalidKeySpecException ignore2) {
try {
return KeyFactory.getInstance("EC").generatePrivate(keySpec);
} catch (InvalidKeySpecException e) {
throw new InvalidKeySpecException("Neither RSA, DSA nor EC worked", e);
}
}
}
}
}
// Ok, failed to read as PEM file, try and read it as a raw binary private key
try (DataInputStream in = new DataInputStream(new FileInputStream(privateKeyFile))) {
byte[] keyBytes = new byte[(int) privateKeyFile.length()];
in.readFully(keyBytes);
KeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
return KeyFactory.getInstance(DEFAULT_ENCRYPTION).generatePrivate(keySpec);
}
}
use of java.security.spec.KeySpec in project robovm by robovm.
the class RSAMultiPrimePrivateCrtKeySpecTest method testRSAMultiPrimePrivateCrtKeySpec12.
/**
* Test #12 for
* <code>RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus,
* BigInteger publicExponent,
* BigInteger privateExponent,
* BigInteger primeP,
* BigInteger primeQ,
* BigInteger primeExponentP,
* BigInteger primeExponentQ,
* BigInteger crtCoefficient,
* RSAOtherPrimeInfo[] otherPrimeInfo)
* </code> ctor<br>
* Assertion: constructs <code>RSAMultiPrimePrivateCrtKeySpec</code>
* object using valid parameters. Constructed object must be
* instance of RSAPrivateKeySpec.
*/
public final void testRSAMultiPrimePrivateCrtKeySpec12() {
KeySpec ks = new RSAMultiPrimePrivateCrtKeySpec(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, opi);
assertTrue(ks instanceof RSAPrivateKeySpec);
}
use of java.security.spec.KeySpec in project robovm by robovm.
the class RSAMultiPrimePrivateCrtKeySpecTest method testRSAMultiPrimePrivateCrtKeySpec01.
// Test-cases:
/**
* Test #1 for
* <code>RSAMultiPrimePrivateCrtKeySpec(BigInteger modulus,
* BigInteger publicExponent,
* BigInteger privateExponent,
* BigInteger primeP,
* BigInteger primeQ,
* BigInteger primeExponentP,
* BigInteger primeExponentQ,
* BigInteger crtCoefficient,
* RSAOtherPrimeInfo[] otherPrimeInfo)
* </code> ctor<br>
* Assertion: constructs <code>RSAMultiPrimePrivateCrtKeySpec</code>
* object using valid parameters
*/
public final void testRSAMultiPrimePrivateCrtKeySpec01() {
KeySpec ks = new RSAMultiPrimePrivateCrtKeySpec(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, opi);
assertTrue(ks instanceof RSAMultiPrimePrivateCrtKeySpec);
}
use of java.security.spec.KeySpec in project robovm by robovm.
the class RSAPrivateCrtKeySpecTest method testRSAPrivateCrtKeySpec01.
/**
* Test #1 for <code>RSAPrivateCrtKeySpec</code> constructor
* Assertion: Constructs <code>RSAPrivateCrtKeySpec</code>
* object using valid parameters
*/
public final void testRSAPrivateCrtKeySpec01() {
KeySpec ks = new RSAPrivateCrtKeySpec(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE);
assertTrue(ks instanceof RSAPrivateCrtKeySpec);
}
Aggregations