use of java.security.spec.KeySpec in project orientdb by orientechnologies.
the class OSymmetricKey method create.
protected void create() {
try {
SecureRandom secureRandom = new SecureRandom();
byte[] salt = secureRandom.generateSeed(saltLength);
KeySpec keySpec = new PBEKeySpec(seedPhrase.toCharArray(), salt, iteration, keySize);
SecretKeyFactory factory = SecretKeyFactory.getInstance(seedAlgorithm);
SecretKey tempKey = factory.generateSecret(keySpec);
secretKey = new SecretKeySpec(tempKey.getEncoded(), secretKeyAlgorithm);
} catch (Exception ex) {
throw new OSecurityException("OSymmetricKey.create() Exception: " + ex);
}
}
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