Search in sources :

Example 21 with EncodedKeySpec

use of java.security.spec.EncodedKeySpec in project graylog2-server by Graylog2.

the class KeyUtil method loadPrivateKey.

@VisibleForTesting
protected static PrivateKey loadPrivateKey(File file, String password) throws IOException, GeneralSecurityException {
    try (final InputStream is = Files.newInputStream(file.toPath())) {
        final byte[] keyBytes = ByteStreams.toByteArray(is);
        final String keyString = new String(keyBytes, StandardCharsets.US_ASCII);
        final Matcher m = KEY_PATTERN.matcher(keyString);
        byte[] encoded = keyBytes;
        if (m.matches()) {
            if (!Strings.isNullOrEmpty(m.group(1))) {
                throw new IllegalArgumentException("Unsupported key type PKCS#1, please convert to PKCS#8");
            }
            encoded = BaseEncoding.base64().decode(m.group(3).replaceAll("[\\r\\n]", ""));
        }
        final EncodedKeySpec keySpec = createKeySpec(encoded, password);
        if (keySpec == null) {
            throw new IllegalArgumentException("Unsupported key type: " + file);
        }
        final String[] keyAlgorithms = { "RSA", "DSA", "EC" };
        for (String keyAlgorithm : keyAlgorithms) {
            try {
                @SuppressWarnings("InsecureCryptoUsage") final KeyFactory keyFactory = KeyFactory.getInstance(keyAlgorithm);
                return keyFactory.generatePrivate(keySpec);
            } catch (InvalidKeySpecException e) {
                LOG.debug("Loading {} private key from \"{}\" failed", keyAlgorithm, file, e);
            }
        }
        throw new IllegalArgumentException("Unsupported key type: " + file);
    }
}
Also used : Matcher(java.util.regex.Matcher) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) SecretKeyFactory(javax.crypto.SecretKeyFactory) EncodedKeySpec(java.security.spec.EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 22 with EncodedKeySpec

use of java.security.spec.EncodedKeySpec in project robovm by robovm.

the class EncodedKeySpecTest method testIsStatePreserved2.

/**
     * Tests that internal state of the object can not be modified using
     * returned value of <code>getEncoded()</code> method
     */
public final void testIsStatePreserved2() {
    byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
    EncodedKeySpec meks = new MyEncodedKeySpec(encodedKey);
    /* Get encoded key */
    byte[] ek = meks.getEncoded();
    /* Modify returned value */
    ek[3] = (byte) 5;
    /* Get encoded key again */
    byte[] ek1 = meks.getEncoded();
    /* Check that byte value has not been changed */
    assertTrue(ek1[3] == (byte) 4);
}
Also used : MyEncodedKeySpec(org.apache.harmony.security.tests.support.spec.MyEncodedKeySpec) EncodedKeySpec(java.security.spec.EncodedKeySpec) MyEncodedKeySpec(org.apache.harmony.security.tests.support.spec.MyEncodedKeySpec)

Example 23 with EncodedKeySpec

use of java.security.spec.EncodedKeySpec in project robovm by robovm.

the class EncodedKeySpecTest method testIsStatePreserved1.

/**
     * Tests that internal state of the object can not be modified by modifying
     * initial array value
     */
public final void testIsStatePreserved1() {
    /* Create initial byte array */
    byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
    EncodedKeySpec meks = new MyEncodedKeySpec(encodedKey);
    /* Modify initial array's value */
    encodedKey[3] = (byte) 5;
    /* Get encoded key */
    byte[] ek = meks.getEncoded();
    /* Check that byte value has not been changed */
    assertTrue(ek[3] == (byte) 4);
}
Also used : MyEncodedKeySpec(org.apache.harmony.security.tests.support.spec.MyEncodedKeySpec) EncodedKeySpec(java.security.spec.EncodedKeySpec) MyEncodedKeySpec(org.apache.harmony.security.tests.support.spec.MyEncodedKeySpec)

Example 24 with EncodedKeySpec

use of java.security.spec.EncodedKeySpec in project google-cloud-java by GoogleCloudPlatform.

the class StorageImplTest method beforeClass.

@BeforeClass
public static void beforeClass() throws NoSuchAlgorithmException, InvalidKeySpecException {
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(BaseEncoding.base64().decode(PRIVATE_KEY_STRING));
    privateKey = keyFactory.generatePrivate(privateKeySpec);
    EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(BaseEncoding.base64().decode(PUBLIC_KEY_STRING));
    publicKey = keyFactory.generatePublic(publicKeySpec);
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyFactory(java.security.KeyFactory) EncodedKeySpec(java.security.spec.EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) BeforeClass(org.junit.BeforeClass)

Example 25 with EncodedKeySpec

use of java.security.spec.EncodedKeySpec in project smoke by textbrowser.

the class Cryptography method generatePrivatePublicKeyPair.

public static KeyPair generatePrivatePublicKeyPair(String algorithm, byte[] privateBytes, byte[] publicBytes) {
    try {
        if (algorithm.equals("McEliece-Fujisaki")) {
            EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateBytes);
            EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicBytes);
            KeyFactory generator = KeyFactory.getInstance(PQCObjectIdentifiers.mcElieceCca2.getId());
            PrivateKey privateKey = generator.generatePrivate(privateKeySpec);
            PublicKey publicKey = generator.generatePublic(publicKeySpec);
            return new KeyPair(publicKey, privateKey);
        } else {
            EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateBytes);
            EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicBytes);
            KeyFactory generator = KeyFactory.getInstance(algorithm);
            PrivateKey privateKey = generator.generatePrivate(privateKeySpec);
            PublicKey publicKey = generator.generatePublic(publicKeySpec);
            return new KeyPair(publicKey, privateKey);
        }
    } catch (Exception exception) {
        Database.getInstance().writeLog("Cryptography::generatePrivatePublicKeyPair(): " + "exception raised (" + exception.getMessage().toLowerCase().trim() + ").");
    }
    return null;
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) BCMcElieceCCA2PublicKey(org.bouncycastle.pqc.jcajce.provider.mceliece.BCMcElieceCCA2PublicKey) PublicKey(java.security.PublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) EncodedKeySpec(java.security.spec.EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec)

Aggregations

EncodedKeySpec (java.security.spec.EncodedKeySpec)34 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)21 KeyFactory (java.security.KeyFactory)19 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)16 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)15 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)15 MyEncodedKeySpec (org.apache.harmony.security.tests.support.spec.MyEncodedKeySpec)8 PublicKey (java.security.PublicKey)3 RSAPublicKey (java.security.interfaces.RSAPublicKey)3 SecretKeyFactory (javax.crypto.SecretKeyFactory)3 TypedArray (android.content.res.TypedArray)2 IOException (java.io.IOException)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 PKCS1EncodedKeySpec (net.oauth.signature.pem.PKCS1EncodedKeySpec)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 InvalidKeyException (java.security.InvalidKeyException)1 KeyPair (java.security.KeyPair)1