Search in sources :

Example 6 with EncodedKeySpec

use of java.security.spec.EncodedKeySpec in project intellij-community by JetBrains.

the class Asn1Object method read.

private static PrivateKey read(String fileName) throws IOException {
    KeyFactory factory;
    try {
        factory = KeyFactory.getInstance("RSA");
    } catch (NoSuchAlgorithmException e) {
        throw new IOException("JCE error: " + e.getMessage());
    }
    List<String> lines = FileUtilRt.loadLines(fileName, "UTF-8");
    for (int i = 0; i < lines.size(); i++) {
        String line = lines.get(i);
        if (line.contains(P1_BEGIN_MARKER)) {
            List<String> strings = lines.subList(i + 1, lines.size());
            byte[] keyBytes = readKeyMaterial(P1_END_MARKER, strings);
            RSAPrivateCrtKeySpec keySpec = getRSAKeySpec(keyBytes);
            try {
                return factory.generatePrivate(keySpec);
            } catch (InvalidKeySpecException e) {
                throw new IOException("Invalid PKCS#1 PEM file: " + e.getMessage());
            }
        }
        if (line.contains(P8_BEGIN_MARKER)) {
            List<String> strings = lines.subList(i + 1, lines.size());
            byte[] keyBytes = readKeyMaterial(P8_END_MARKER, strings);
            EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
            try {
                return factory.generatePrivate(keySpec);
            } catch (InvalidKeySpecException e) {
                throw new IOException("Invalid PKCS#8 PEM file: " + e.getMessage());
            }
        }
    }
    throw new IOException("Invalid PEM file: no begin marker");
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) EncodedKeySpec(java.security.spec.EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec)

Example 7 with EncodedKeySpec

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

the class X509EncodedKeySpecTest method testX509EncodedKeySpec.

//
// Test cases
//
/**
     * Test for <code>X509EncodedKeySpec</code> constructor<br>
     * Assertion: constructs new <code>X509EncodedKeySpec</code>
     * object using valid parameter
     */
public final void testX509EncodedKeySpec() {
    byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
    EncodedKeySpec eks = new X509EncodedKeySpec(encodedKey);
    assertTrue(eks instanceof X509EncodedKeySpec);
    try {
        eks = new X509EncodedKeySpec(null);
        fail("expected NullPointerException");
    } catch (NullPointerException e) {
    // ok
    }
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) EncodedKeySpec(java.security.spec.EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec)

Example 8 with EncodedKeySpec

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

the class EncodedKeySpecTest method testGetEncoded.

/**
     * Tests that <code>getEncoded()</code> method returns valid byte array
     */
public final void testGetEncoded() {
    byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
    EncodedKeySpec meks = new MyEncodedKeySpec(encodedKey);
    /* Get encoded key */
    byte[] ek = meks.getEncoded();
    /* Check returned array */
    boolean result = true;
    for (int i = 0; i < encodedKey.length; i++) {
        if (encodedKey[i] != ek[i]) {
            /* indicate failure */
            result = false;
        }
    }
    /* passed */
    assertTrue(result);
}
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 9 with EncodedKeySpec

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

the class EncodedKeySpecTest method testEncodedKeySpec.

/**
     * Tests for constructor <code>EncodedKeySpec(byte[])</code><br>
     */
public final void testEncodedKeySpec() {
    byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
    EncodedKeySpec eks = new MyEncodedKeySpec(encodedKey);
    assertTrue("wrong encoded key was returned", Arrays.equals(encodedKey, eks.getEncoded()));
    assertEquals("wrong name of encoding format", "My", eks.getFormat());
    encodedKey = null;
    try {
        eks = new MyEncodedKeySpec(encodedKey);
        fail("expected NullPointerException");
    } catch (NullPointerException e) {
    //
    }
}
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 10 with EncodedKeySpec

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

the class PKCS8EncodedKeySpecTest method testPKCS8EncodedKeySpec.

//
// Tests
//
/**
     * Test for <code>PKCS8EncodedKeySpec</code> constructor<br>
     * Assertion: constructs new <code>PKCS8EncodedKeySpec</code>
     * object using valid parameter
     */
public final void testPKCS8EncodedKeySpec() {
    byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
    EncodedKeySpec eks = new PKCS8EncodedKeySpec(encodedKey);
    assertTrue(eks instanceof PKCS8EncodedKeySpec);
    try {
        eks = new PKCS8EncodedKeySpec(null);
        fail("expected NullPointerException");
    } catch (NullPointerException e) {
    // ok
    }
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) EncodedKeySpec(java.security.spec.EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec)

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