use of edu.umass.cs.gnscommon.exceptions.client.EncryptionException in project GNS by MobilityFirst.
the class GNSClientCommands method publicKeyLookupFromGuid.
/**
* Get the public key for a given guid.
*
* @param guid
* @return Public key for {@code guid}
* @throws edu.umass.cs.gnscommon.exceptions.client.ClientException
* if a protocol error occurs or the list cannot be parsed
* @throws java.io.IOException
* if a communication error occurs
*/
// Note: publicKeyLookupFromGUID is implemented incorrectly in GNSCommand
public PublicKey publicKeyLookupFromGuid(String guid) throws ClientException, IOException {
JSONObject guidInfo = lookupGuidRecord(guid);
try {
String key = guidInfo.getString(GNSProtocol.GUID_RECORD_PUBLICKEY.toString());
byte[] encodedPublicKey = Base64.decode(key);
KeyFactory keyFactory = KeyFactory.getInstance(GNSProtocol.RSA_ALGORITHM.toString());
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
return keyFactory.generatePublic(publicKeySpec);
} catch (JSONException e) {
throw new ClientException("Failed to parse LOOKUP_USER response", e);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new EncryptionException("Public key encryption failed", e);
}
}
use of edu.umass.cs.gnscommon.exceptions.client.EncryptionException 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;
}
Aggregations