use of java.security.spec.EncodedKeySpec in project graylog2-server by Graylog2.
the class KeyUtil method loadPrivateKey.
@VisibleForTesting
protected static PrivateKey loadPrivateKey(File file, String password) throws IOException, GeneralSecurityException {
try (final InputStream is = Files.newInputStream(file.toPath())) {
final byte[] keyBytes = ByteStreams.toByteArray(is);
final String keyString = new String(keyBytes, StandardCharsets.US_ASCII);
final Matcher m = KEY_PATTERN.matcher(keyString);
byte[] encoded = keyBytes;
if (m.matches()) {
if (!Strings.isNullOrEmpty(m.group(1))) {
throw new IllegalArgumentException("Unsupported key type PKCS#1, please convert to PKCS#8");
}
encoded = BaseEncoding.base64().decode(m.group(3).replaceAll("[\\r\\n]", ""));
}
final EncodedKeySpec keySpec = createKeySpec(encoded, password);
if (keySpec == null) {
throw new IllegalArgumentException("Unsupported key type: " + file);
}
final String[] keyAlgorithms = { "RSA", "DSA", "EC" };
for (String keyAlgorithm : keyAlgorithms) {
try {
@SuppressWarnings("InsecureCryptoUsage") final KeyFactory keyFactory = KeyFactory.getInstance(keyAlgorithm);
return keyFactory.generatePrivate(keySpec);
} catch (InvalidKeySpecException e) {
LOG.debug("Loading {} private key from \"{}\" failed", keyAlgorithm, file, e);
}
}
throw new IllegalArgumentException("Unsupported key type: " + file);
}
}
use of java.security.spec.EncodedKeySpec in project robovm by robovm.
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);
}
use of java.security.spec.EncodedKeySpec in project robovm by robovm.
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 google-cloud-java by GoogleCloudPlatform.
the class StorageImplTest method beforeClass.
@BeforeClass
public static void beforeClass() throws NoSuchAlgorithmException, InvalidKeySpecException {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(BaseEncoding.base64().decode(PRIVATE_KEY_STRING));
privateKey = keyFactory.generatePrivate(privateKeySpec);
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(BaseEncoding.base64().decode(PUBLIC_KEY_STRING));
publicKey = keyFactory.generatePublic(publicKeySpec);
}
use of java.security.spec.EncodedKeySpec in project smoke by textbrowser.
the class Cryptography method generatePrivatePublicKeyPair.
public static KeyPair generatePrivatePublicKeyPair(String algorithm, byte[] privateBytes, byte[] publicBytes) {
try {
if (algorithm.equals("McEliece-Fujisaki")) {
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateBytes);
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicBytes);
KeyFactory generator = KeyFactory.getInstance(PQCObjectIdentifiers.mcElieceCca2.getId());
PrivateKey privateKey = generator.generatePrivate(privateKeySpec);
PublicKey publicKey = generator.generatePublic(publicKeySpec);
return new KeyPair(publicKey, privateKey);
} else {
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateBytes);
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicBytes);
KeyFactory generator = KeyFactory.getInstance(algorithm);
PrivateKey privateKey = generator.generatePrivate(privateKeySpec);
PublicKey publicKey = generator.generatePublic(publicKeySpec);
return new KeyPair(publicKey, privateKey);
}
} catch (Exception exception) {
Database.getInstance().writeLog("Cryptography::generatePrivatePublicKeyPair(): " + "exception raised (" + exception.getMessage().toLowerCase().trim() + ").");
}
return null;
}
Aggregations