Search in sources :

Example 51 with KeySpec

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);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecureRandom(java.security.SecureRandom) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) SecretKeyFactory(javax.crypto.SecretKeyFactory) OException(com.orientechnologies.common.exception.OException) KeyStoreException(java.security.KeyStoreException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 52 with KeySpec

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);
    }
}
Also used : PemReader(org.bouncycastle.util.io.pem.PemReader) PemObject(org.bouncycastle.util.io.pem.PemObject) KeySpec(java.security.spec.KeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) FileReader(java.io.FileReader) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream)

Example 53 with 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);
}
Also used : RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) RSAMultiPrimePrivateCrtKeySpec(java.security.spec.RSAMultiPrimePrivateCrtKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) KeySpec(java.security.spec.KeySpec) RSAMultiPrimePrivateCrtKeySpec(java.security.spec.RSAMultiPrimePrivateCrtKeySpec)

Example 54 with KeySpec

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);
}
Also used : RSAMultiPrimePrivateCrtKeySpec(java.security.spec.RSAMultiPrimePrivateCrtKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) KeySpec(java.security.spec.KeySpec) RSAMultiPrimePrivateCrtKeySpec(java.security.spec.RSAMultiPrimePrivateCrtKeySpec)

Example 55 with KeySpec

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);
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) KeySpec(java.security.spec.KeySpec) RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec)

Aggregations

KeySpec (java.security.spec.KeySpec)79 PBEKeySpec (javax.crypto.spec.PBEKeySpec)29 SecretKeyFactory (javax.crypto.SecretKeyFactory)27 KeyFactory (java.security.KeyFactory)21 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)21 SecretKeySpec (javax.crypto.spec.SecretKeySpec)21 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)20 SecretKey (javax.crypto.SecretKey)19 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)15 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)12 PrivateKey (java.security.PrivateKey)11 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)11 InvalidKeyException (java.security.InvalidKeyException)9 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)9 DESKeySpec (javax.crypto.spec.DESKeySpec)9 IOException (java.io.IOException)8 BigInteger (java.math.BigInteger)7 NoSuchProviderException (java.security.NoSuchProviderException)7 PublicKey (java.security.PublicKey)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6