Java Code Examples for java.security.spec.PKCS8EncodedKeySpec#getEncoded()

The following examples show how to use java.security.spec.PKCS8EncodedKeySpec#getEncoded() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZipUtils.java    From isu with GNU General Public License v3.0 6 votes vote down vote up
/** Read a PKCS#8 format private key. */
private static PrivateKey readPrivateKey(InputStream input)
throws IOException, GeneralSecurityException {
    try {
        byte[] buffer = new byte[4096];
        int size = input.read(buffer);
        byte[] bytes = Arrays.copyOf(buffer, size);
        /* Check to see if this is in an EncryptedPrivateKeyInfo structure. */
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(bytes);
        /*
         * Now it's in a PKCS#8 PrivateKeyInfo structure. Read its Algorithm
         * OID and use that to construct a KeyFactory.
         */
        ASN1InputStream bIn = new ASN1InputStream(new ByteArrayInputStream(spec.getEncoded()));
        PrivateKeyInfo pki = PrivateKeyInfo.getInstance(bIn.readObject());
        String algOid = pki.getPrivateKeyAlgorithm().getAlgorithm().getId();
        return KeyFactory.getInstance(algOid).generatePrivate(spec);
    } finally {
        input.close();
    }
}
 
Example 2
Source File: PKCS8EncodedKeySpecTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that internal state of the object
 * can not be changed by modifying initial
 * array value
 */
public final void testIsStatePreserved1() {
    // Reference array
    byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4};
    // Reference array's copy will be used for test
    byte[] encodedKeyCopy = encodedKey.clone();

    PKCS8EncodedKeySpec meks = new PKCS8EncodedKeySpec(encodedKeyCopy);

    // Modify initial array's value
    encodedKeyCopy[3] = (byte)5;

    // Get encoded key
    byte[] ek = meks.getEncoded();

    // Check  using reference array that
    // byte value has not been changed
    assertTrue(Arrays.equals(encodedKey, ek));
}
 
Example 3
Source File: PKCS8EncodedKeySpecTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that internal state of the object
 * can not be modified using returned value
 * of <code>getEncoded()</code> method
 */
public final void testIsStatePreserved2() {
    // Reference array
    byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4};
    // Reference array's copy will be used for test
    byte[] encodedKeyCopy = encodedKey.clone();

    PKCS8EncodedKeySpec meks = new PKCS8EncodedKeySpec(encodedKeyCopy);

    byte[] ek = meks.getEncoded();

    // Modify returned array
    ek[3] = (byte)5;

    // Get encoded key again
    byte[] ek1 = meks.getEncoded();

    // Check using reference array that
    // byte value has not been changed
    assertTrue(Arrays.equals(encodedKey, ek1));
}
 
Example 4
Source File: CryptoUtils.java    From crate with Apache License 2.0 4 votes vote down vote up
static byte[] getPrivateKeyBytes(PrivateKey privateKey) {
    PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
    return encodedKeySpec.getEncoded();
}
 
Example 5
Source File: ConfigBuilderTest.java    From julongchain with Apache License 2.0 3 votes vote down vote up
@Test
public void iteratorPath() {

   PKCS8EncodedKeySpec pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec("123".getBytes());

    byte[] bytes=pkcs8EncodedKeySpec.getEncoded();

    java.lang.String publiKey=java.lang.String.valueOf(Base64.encode(bytes));
        //    new String(Base64.encode(bytes));
   // Base64.encode(bytes);
    System.out.println(publiKey);

}
 
Example 6
Source File: PKCS8EncodedKeySpecTest.java    From j2objc with Apache License 2.0 3 votes vote down vote up
/**
 * Test for <code>getEncoded()</code> method<br>
 * Assertion: returns encoded key
 */
public final void testGetEncoded() {
    byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4};

    PKCS8EncodedKeySpec meks = new PKCS8EncodedKeySpec(encodedKey);

    byte[] ek = meks.getEncoded();

    assertTrue(Arrays.equals(encodedKey, ek));
}