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;
}
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);
}
}
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);
}
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) {
//
}
}
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);
}
Aggregations