Search in sources :

Example 16 with EncodedKeySpec

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

the class Cryptography method publicRSAKeyFromBytes.

public static PublicKey publicRSAKeyFromBytes(byte[] publicBytes) {
    try {
        EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicBytes);
        KeyFactory generator = KeyFactory.getInstance("RSA");
        return generator.generatePublic(publicKeySpec);
    } catch (Exception exception) {
    }
    return null;
}
Also used : 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)

Example 17 with EncodedKeySpec

use of java.security.spec.EncodedKeySpec in project data-transfer-project by google.

the class PublicPrivateKeyPairGenerator method parsePrivateKey.

/**
 * Parses the given {@code encoded} private key.
 */
public static PrivateKey parsePrivateKey(String encoded) {
    byte[] decoded = BaseEncoding.base64Url().decode(encoded);
    EncodedKeySpec spec = new X509EncodedKeySpec(decoded);
    KeyFactory keyFactory;
    try {
        keyFactory = KeyFactory.getInstance(ALGORITHM);
    } catch (NoSuchAlgorithmException e) {
        logger.error("NoSuchAlgorithmException for: {}", ALGORITHM, e);
        throw new RuntimeException("NoSuchAlgorithmException generating private keyFactory", e);
    }
    try {
        return keyFactory.generatePrivate(spec);
    } catch (InvalidKeySpecException e) {
        logger.error("InvalidKeySpecException for: {}", spec.getEncoded().length, e);
        throw new RuntimeException("InvalidKeySpecException generating private key", e);
    }
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) EncodedKeySpec(java.security.spec.EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec)

Example 18 with EncodedKeySpec

use of java.security.spec.EncodedKeySpec in project j2objc by google.

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 19 with EncodedKeySpec

use of java.security.spec.EncodedKeySpec in project j2objc by google.

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 20 with EncodedKeySpec

use of java.security.spec.EncodedKeySpec in project j2objc by google.

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)

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