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

The following examples show how to use java.security.spec.X509EncodedKeySpec#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: X509EncodedKeySpecTest.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();

    X509EncodedKeySpec meks = new X509EncodedKeySpec(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 2
Source File: X509EncodedKeySpecTest.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();

    X509EncodedKeySpec meks = new X509EncodedKeySpec(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 3
Source File: IosRSAKeyFactory.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Override
 protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
     throws InvalidKeySpecException {
  //The KeySpec for Private Key is PKCS8
if (keySpec instanceof PKCS8EncodedKeySpec ) {
  return new IosRSAKey.IosRSAPrivateKey(((PKCS8EncodedKeySpec) keySpec).getEncoded());  
}
   if (keySpec instanceof X509EncodedKeySpec) {
     X509EncodedKeySpec x509Spec = (X509EncodedKeySpec) keySpec;
     return new IosRSAKey.IosRSAPrivateKey(x509Spec.getEncoded());
   } else if (keySpec instanceof RSAPrivateKeySpec) {
     return new IosRSAKey.IosRSAPrivateKey((RSAPrivateKeySpec) keySpec);
   }
   throw new InvalidKeySpecException(
       "Must use PKCS8EncodedKeySpec, X509EncodedKeySpec or RSAPrivateKeySpec; was "
           + keySpec.getClass().getName());
 }
 
Example 4
Source File: IosRSAKeyFactory.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Override
protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException {
  if (keySpec instanceof X509EncodedKeySpec) {
    X509EncodedKeySpec x509Spec = (X509EncodedKeySpec) keySpec;
    return new IosRSAKey.IosRSAPublicKey(x509Spec.getEncoded());
  } else if (keySpec instanceof RSAPublicKeySpec) {
    return new IosRSAKey.IosRSAPublicKey((RSAPublicKeySpec) keySpec);
  }
  throw new InvalidKeySpecException(
      "Must use RSAPublicKeySpec; was " + keySpec.getClass().getName());
}
 
Example 5
Source File: CryptoUtils.java    From crate with Apache License 2.0 4 votes vote down vote up
static byte[] getPublicKeyBytes(PublicKey publicKey) {
    X509EncodedKeySpec encodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
    return encodedKeySpec.getEncoded();
}
 
Example 6
Source File: X509EncodedKeySpecTest.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};

    X509EncodedKeySpec eks = new X509EncodedKeySpec(encodedKey);

    byte[] ek = eks.getEncoded();

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