use of java.security.spec.PKCS8EncodedKeySpec in project platformlayer by platformlayer.
the class KeyParser method parse.
public Object parse(String s) {
Object key = null;
if (key == null) {
if (s.contains(BEGIN_PRIVATE_KEY)) {
String payload = s.substring(s.indexOf(BEGIN_PRIVATE_KEY) + BEGIN_PRIVATE_KEY.length());
if (payload.contains(END_PRIVATE_KEY)) {
payload = payload.substring(0, payload.indexOf(END_PRIVATE_KEY));
key = tryParsePemFormat(payload);
}
}
}
if (key == null) {
try {
PemReader reader = new PemReader(new StringReader(s));
PemObject pemObject = reader.readPemObject();
reader.close();
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pemObject.getContent());
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey privateKey = kf.generatePrivate(keySpec);
if (privateKey instanceof RSAPrivateCrtKey) {
RSAPrivateCrtKey rsaPrivateCrtKey = (RSAPrivateCrtKey) privateKey;
RSAPublicKeySpec publicKeySpec = new java.security.spec.RSAPublicKeySpec(rsaPrivateCrtKey.getModulus(), rsaPrivateCrtKey.getPublicExponent());
PublicKey publicKey = kf.generatePublic(publicKeySpec);
key = new KeyPair(publicKey, privateKey);
} else {
key = privateKey;
}
} catch (Exception e) {
log.debug("Error reading pem data", e);
return null;
}
}
if (key == null) {
try {
// TODO: Check if looks like base64??
byte[] fromBase64 = Base64.decode(s);
key = parse(fromBase64);
} catch (Exception e) {
log.debug("Cannot decode as base64", e);
}
}
return key;
}
use of java.security.spec.PKCS8EncodedKeySpec in project xabber-android by redsolution.
the class AccountTable method getKeyPair.
static KeyPair getKeyPair(Cursor cursor) {
byte[] publicKeyBytes = cursor.getBlob(cursor.getColumnIndex(Fields.PUBLIC_KEY));
byte[] privateKeyBytes = cursor.getBlob(cursor.getColumnIndex(Fields.PRIVATE_KEY));
if (privateKeyBytes == null || publicKeyBytes == null) {
return null;
}
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
PublicKey publicKey;
PrivateKey privateKey;
KeyFactory keyFactory;
try {
keyFactory = KeyFactory.getInstance("DSA");
publicKey = keyFactory.generatePublic(publicKeySpec);
privateKey = keyFactory.generatePrivate(privateKeySpec);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new RuntimeException(e);
}
return new KeyPair(publicKey, privateKey);
}
use of java.security.spec.PKCS8EncodedKeySpec in project robovm by robovm.
the class OpenSSLECKeyFactory method engineTranslateKey.
@Override
protected Key engineTranslateKey(Key key) throws InvalidKeyException {
if (key == null) {
throw new InvalidKeyException("key == null");
}
if ((key instanceof OpenSSLECPublicKey) || (key instanceof OpenSSLECPrivateKey)) {
return key;
} else if (key instanceof ECPublicKey) {
ECPublicKey ecKey = (ECPublicKey) key;
ECPoint w = ecKey.getW();
ECParameterSpec params = ecKey.getParams();
try {
return engineGeneratePublic(new ECPublicKeySpec(w, params));
} catch (InvalidKeySpecException e) {
throw new InvalidKeyException(e);
}
} else if (key instanceof ECPrivateKey) {
ECPrivateKey ecKey = (ECPrivateKey) key;
BigInteger s = ecKey.getS();
ECParameterSpec params = ecKey.getParams();
try {
return engineGeneratePrivate(new ECPrivateKeySpec(s, params));
} catch (InvalidKeySpecException e) {
throw new InvalidKeyException(e);
}
} else if ((key instanceof PrivateKey) && ("PKCS#8".equals(key.getFormat()))) {
byte[] encoded = key.getEncoded();
if (encoded == null) {
throw new InvalidKeyException("Key does not support encoding");
}
try {
return engineGeneratePrivate(new PKCS8EncodedKeySpec(encoded));
} catch (InvalidKeySpecException e) {
throw new InvalidKeyException(e);
}
} else if ((key instanceof PublicKey) && ("X.509".equals(key.getFormat()))) {
byte[] encoded = key.getEncoded();
if (encoded == null) {
throw new InvalidKeyException("Key does not support encoding");
}
try {
return engineGeneratePublic(new X509EncodedKeySpec(encoded));
} catch (InvalidKeySpecException e) {
throw new InvalidKeyException(e);
}
} else {
throw new InvalidKeyException("Key must be EC public or private key; was " + key.getClass().getName());
}
}
use of java.security.spec.PKCS8EncodedKeySpec in project robovm by robovm.
the class EncryptedPrivateKeyInfoTest method test_ROUNDTRIP_GetKeySpecKey01.
/**
* Encrypted data contains valid PKCS8 key info encoding
*/
public final void test_ROUNDTRIP_GetKeySpecKey01() {
boolean performed = false;
for (int i = 0; i < algName.length; i++) {
try {
// generate test data
TestDataGenerator g = new TestDataGenerator(algName[i][0], algName[i][1], privateKeyInfo, null);
// create test object
EncryptedPrivateKeyInfo epki;
if (g.ap() == null) {
epki = new EncryptedPrivateKeyInfo(algName[i][0], g.ct());
} else {
epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
}
try {
PKCS8EncodedKeySpec eks = epki.getKeySpec(g.pubK() == null ? g.k() : g.pubK());
if (!Arrays.equals(privateKeyInfo, eks.getEncoded())) {
fail(algName[i][0] + " != " + algName[i][1]);
}
} catch (InvalidKeyException e) {
fail(algName[i][0] + ", " + algName[i][1] + ": " + e);
}
performed = true;
} catch (TestDataGenerator.AllowedFailure allowedFailure) {
} catch (NoSuchAlgorithmException allowedFailure) {
}
}
assertTrue("Test not performed", performed);
}
use of java.security.spec.PKCS8EncodedKeySpec in project robovm by robovm.
the class EncryptedPrivateKeyInfoTest method test_ROUNDTRIP_GetKeySpecKeyString01.
/**
* Encrypted data contains valid PKCS8 key info encoding
*/
public final void test_ROUNDTRIP_GetKeySpecKeyString01() throws Exception {
boolean performed = false;
for (int i = 0; i < algName.length; i++) {
for (int l = 0; l < provider.length; l++) {
if (provider[l] == null) {
continue;
}
TestDataGenerator g;
try {
// generate test data
g = new TestDataGenerator(algName[i][0], algName[i][1], privateKeyInfo, provider[l]);
} catch (TestDataGenerator.AllowedFailure allowedFailure) {
continue;
}
try {
// create test object
EncryptedPrivateKeyInfo epki;
if (g.ap() == null) {
epki = new EncryptedPrivateKeyInfo(algName[i][0], g.ct());
} else {
epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
}
try {
PKCS8EncodedKeySpec eks = epki.getKeySpec(g.pubK() == null ? g.k() : g.pubK(), provider[l].getName());
if (!Arrays.equals(privateKeyInfo, eks.getEncoded())) {
fail(algName[i][0] + " != " + algName[i][1]);
}
} catch (InvalidKeyException e) {
fail(algName[i][0] + ", " + algName[i][1] + ": " + e);
}
performed = true;
} catch (NoSuchAlgorithmException allowedFailure) {
}
}
}
assertTrue("Test not performed", performed);
}
Aggregations