use of java.security.spec.X509EncodedKeySpec in project GNS by MobilityFirst.
the class KeyPairUtils method getGuidEntry.
/**
* Retrieves the public/private key pair for the given user.
*
* @param gnsName the name of the GNS instance (e.g. "server.gns.name:8080")
* @param username the user name
* @return the GNSProtocol.GUID.toString() entry if found, null otherwise
*/
public static GuidEntry getGuidEntry(String gnsName, String username) {
if (username == null) {
return null;
}
if (IS_ANDROID) {
return KeyPairUtilsAndroid.getGuidEntryFromPreferences(gnsName, username);
}
createSingleton();
String guid = keyStorageObj.get(generateKey(gnsName, username, GUID), "");
String publicString = keyStorageObj.get(generateKey(gnsName, username, PUBLIC), "");
String privateString = keyStorageObj.get(generateKey(gnsName, username, PRIVATE), "");
if (!guid.isEmpty() && !publicString.isEmpty() && !privateString.isEmpty()) {
try {
byte[] encodedPublicKey = DatatypeConverter.parseHexBinary(publicString);
//byte[] encodedPublicKey = ByteUtils.hexStringToByteArray(publicString);
byte[] encodedPrivateKey = DatatypeConverter.parseHexBinary(privateString);
//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) {
System.out.println(e.toString());
return null;
}
} else {
return null;
}
}
use of java.security.spec.X509EncodedKeySpec in project GNS by MobilityFirst.
the class KeyPairUtilsAndroid method getAllGuids.
/**
* Return the list of all GUIDs stored locally that belong to a particular GNS
* instance
*
* @param gnsName the GNS host:port
* @return all matching GUIDs
*/
public static List<GuidEntry> getAllGuids(String gnsName) {
List<GuidEntry> guids = new LinkedList<>();
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);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
String aliasKey = line;
String guid = br.readLine();
String publicString = br.readLine();
String privateString = br.readLine();
if (aliasKey.contains(gnsName) && !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);
// Strip gnsName from stored alias to only return the entity name
guids.add(new GuidEntry(aliasKey.substring(gnsName.length() + 1), guid, publicKey, privateKey));
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
Log.e(KeyPairUtilsAndroid.class.getName(), "Cannot decode keys", e);
} catch (EncryptionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (IOException e) {
e.printStackTrace();
// You'll need to add proper error handling here
}
return guids;
}
use of java.security.spec.X509EncodedKeySpec in project lucene-solr by apache.
the class CryptoKeys method getX509PublicKey.
/**
* Create PublicKey from a .DER file
*/
public static PublicKey getX509PublicKey(byte[] buf) throws Exception {
X509EncodedKeySpec spec = new X509EncodedKeySpec(buf);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePublic(spec);
}
use of java.security.spec.X509EncodedKeySpec in project karaf by apache.
the class KnownHostsManager method getKnownKeyInternal.
private PublicKey getKnownKeyInternal(SocketAddress remoteAddress, String checkAlgorithm, BufferedReader reader) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
String checkServerAddress = getAddressString(remoteAddress);
String line = reader.readLine();
while (line != null) {
String[] lineParts = line.split(" ");
String serverAddress = lineParts[0];
String algorithm = lineParts[1];
if (checkServerAddress.equals(serverAddress) && checkAlgorithm.equals(algorithm)) {
byte[] key = Base64.getDecoder().decode(lineParts[2].getBytes());
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(key);
return keyFactory.generatePublic(keySpec);
}
line = reader.readLine();
}
return null;
}
use of java.security.spec.X509EncodedKeySpec in project cloudstack by apache.
the class SAMLUtils method savePublicKey.
public static String savePublicKey(PublicKey key) {
try {
KeyFactory keyFactory = SAMLUtils.getKeyFactory();
if (keyFactory == null)
return null;
X509EncodedKeySpec spec = keyFactory.getKeySpec(key, X509EncodedKeySpec.class);
return new String(org.bouncycastle.util.encoders.Base64.encode(spec.getEncoded()), Charset.forName("UTF-8"));
} catch (InvalidKeySpecException e) {
s_logger.error("Unable to create KeyFactory:" + e.getMessage());
}
return null;
}
Aggregations