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