use of java.security.spec.X509EncodedKeySpec in project cas by apereo.
the class PublicKeyFactoryBean method createInstance.
@Override
protected PublicKey createInstance() throws Exception {
LOGGER.debug("Creating public key instance from [{}] using [{}]", this.resource.getFilename(), this.algorithm);
try (InputStream pubKey = this.resource.getInputStream()) {
final byte[] bytes = new byte[(int) this.resource.contentLength()];
pubKey.read(bytes);
final X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(bytes);
final KeyFactory factory = KeyFactory.getInstance(this.algorithm);
return factory.generatePublic(pubSpec);
}
}
use of java.security.spec.X509EncodedKeySpec in project Payara by payara.
the class SignedJWTIdentityStore method readPublicKey.
public PublicKey readPublicKey(String resourceName) throws Exception {
byte[] byteBuffer = new byte[16384];
int length = currentThread().getContextClassLoader().getResource(resourceName).openStream().read(byteBuffer);
String key = new String(byteBuffer, 0, length).replaceAll("-----BEGIN (.*)-----", "").replaceAll("-----END (.*)----", "").replaceAll("\r\n", "").replaceAll("\n", "").trim();
return KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(key)));
}
use of java.security.spec.X509EncodedKeySpec in project scheduling by ow2-proactive.
the class Credentials method getPublicKey.
/**
* Retrieves a public key stored in a local file
* <p>
*
* @param pubPath path to the public key on the local filesystem
* @return the key encapsulated in a regular JCE container
* @throws KeyException the key could not be retrieved or is malformed
*/
public static PublicKey getPublicKey(String pubPath) throws KeyException {
String algo = "", tmp = "";
byte[] bytes;
File f = new File(pubPath);
// recover public key bytes
try (DataInputStream in = new DataInputStream(new FileInputStream(f))) {
int read, tot = 0;
while ((read = in.read()) != '\n') {
algo += (char) read;
tot++;
}
tot++;
while ((read = in.read()) != '\n') {
tmp += (char) read;
tot++;
}
tot++;
bytes = new byte[(int) f.length() - tot];
in.readFully(bytes);
} catch (Exception e) {
throw new KeyException("Could not retrieve public key from " + pubPath, e);
}
// reconstruct public key
X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(bytes);
PublicKey pubKey;
KeyFactory keyFactory;
try {
keyFactory = KeyFactory.getInstance(algo);
} catch (NoSuchAlgorithmException e) {
throw new KeyException("Cannot initialize key factory", e);
}
try {
pubKey = keyFactory.generatePublic(pubKeySpec);
} catch (InvalidKeySpecException e) {
throw new KeyException("Cannot re-generate public key", e);
}
return pubKey;
}
use of java.security.spec.X509EncodedKeySpec in project GNS by MobilityFirst.
the class KeyPairUtilsAndroid method readGuidEntryFromFile.
private static GuidEntry readGuidEntryFromFile(String gnsName, String username) {
File gnsFolder = new File(GNS_KEY_DIR);
// Save the path as a string value
String extStorageDirectory = gnsFolder.toString();
File file = new File(extStorageDirectory, GNS_KEYS_FILENAME);
String aliasKey = gnsName + "@" + username;
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
if (line.equals(aliasKey)) {
// found the correct alias, read next three lines for guid, public
// key,
// private key
String guid = br.readLine();
String publicString = br.readLine();
String privateString = br.readLine();
if (!publicString.isEmpty() && !privateString.isEmpty()) {
try {
byte[] encodedPublicKey = DatatypeConverter.parseHexBinary(publicString);
byte[] encodedPrivateKey = DatatypeConverter.parseHexBinary(privateString);
// byte[] encodedPublicKey = ByteUtils.hexStringToByteArray(publicString);
// byte[] encodedPrivateKey = ByteUtils.hexStringToByteArray(privateString);
KeyFactory keyFactory = KeyFactory.getInstance(GNSProtocol.RSA_ALGORITHM.toString());
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
return new GuidEntry(username, guid, publicKey, privateKey);
} catch (NoSuchAlgorithmException | InvalidKeySpecException | EncryptionException e) {
Log.e(KeyPairUtilsAndroid.class.getName(), "Cannot decode keys", e);
}
}
break;
}
}
} catch (IOException e) {
e.printStackTrace();
// You'll need to add proper error handling here
}
return null;
}
use of java.security.spec.X509EncodedKeySpec in project xabber-android by redsolution.
the class AccountRealm method getKeyPair.
public KeyPair getKeyPair() {
if (this.privateKeyBytes == null || this.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);
}
Aggregations