use of java.security.spec.InvalidKeySpecException in project opennms by OpenNMS.
the class JCEKSSecureCredentialsVault method setCredentials.
@Override
public void setCredentials(String alias, Credentials credentials) {
try {
byte[] credentialBytes = toBase64EncodedByteArray(credentials);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBE");
SecretKey generatedSecret = factory.generateSecret(new PBEKeySpec(new String(credentialBytes).toCharArray(), m_salt, m_iterationCount, m_keyLength));
KeyStore.PasswordProtection keyStorePP = new KeyStore.PasswordProtection(m_password);
m_keystore.setEntry(alias, new KeyStore.SecretKeyEntry(generatedSecret), keyStorePP);
writeKeystoreToDisk();
} catch (KeyStoreException | InvalidKeySpecException | NoSuchAlgorithmException | IOException e) {
throw Throwables.propagate(e);
}
}
use of java.security.spec.InvalidKeySpecException in project opennms by OpenNMS.
the class JCEKSSecureCredentialsVault method setCredentials.
@Override
public void setCredentials(String alias, Credentials credentials) {
try {
byte[] credentialBytes = toBase64EncodedByteArray(credentials);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBE");
SecretKey generatedSecret = factory.generateSecret(new PBEKeySpec(new String(credentialBytes).toCharArray(), m_salt, m_iterationCount, m_keyLength));
KeyStore.PasswordProtection keyStorePP = new KeyStore.PasswordProtection(m_password);
m_keystore.setEntry(alias, new KeyStore.SecretKeyEntry(generatedSecret), keyStorePP);
writeKeystoreToDisk();
} catch (KeyStoreException | InvalidKeySpecException | NoSuchAlgorithmException | IOException e) {
throw Throwables.propagate(e);
}
}
use of java.security.spec.InvalidKeySpecException in project android_frameworks_base by DirtyUnicorns.
the class PackageParser method parsePublicKey.
public static final PublicKey parsePublicKey(final String encodedPublicKey) {
if (encodedPublicKey == null) {
Slog.w(TAG, "Could not parse null public key");
return null;
}
EncodedKeySpec keySpec;
try {
final byte[] encoded = Base64.decode(encodedPublicKey, Base64.DEFAULT);
keySpec = new X509EncodedKeySpec(encoded);
} catch (IllegalArgumentException e) {
Slog.w(TAG, "Could not parse verifier public key; invalid Base64");
return null;
}
/* First try the key as an RSA key. */
try {
final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
Slog.wtf(TAG, "Could not parse public key: RSA KeyFactory not included in build");
} catch (InvalidKeySpecException e) {
// Not a RSA public key.
}
/* Now try it as a ECDSA key. */
try {
final KeyFactory keyFactory = KeyFactory.getInstance("EC");
return keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
Slog.wtf(TAG, "Could not parse public key: EC KeyFactory not included in build");
} catch (InvalidKeySpecException e) {
// Not a ECDSA public key.
}
/* Now try it as a DSA key. */
try {
final KeyFactory keyFactory = KeyFactory.getInstance("DSA");
return keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
Slog.wtf(TAG, "Could not parse public key: DSA KeyFactory not included in build");
} catch (InvalidKeySpecException e) {
// Not a DSA public key.
}
/* Not a supported key type */
return null;
}
use of java.security.spec.InvalidKeySpecException in project GNS by MobilityFirst.
the class ClientAsynchExample method main.
/**
*
* @param args
* @throws IOException
* @throws InvalidKeySpecException
* @throws NoSuchAlgorithmException
* @throws ClientException
* @throws InvalidKeyException
* @throws SignatureException
* @throws Exception
*/
public static void main(String[] args) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException, ClientException, InvalidKeyException, SignatureException, Exception {
// Create the client
GNSClientCommands client = new GNSClientCommands(null);
GuidEntry accountGuidEntry = null;
try {
// Create a guid (which is also an account guid)
accountGuidEntry = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS, "password", true);
} catch (Exception e) {
System.out.println("Exception during accountGuid creation: " + e);
e.printStackTrace();
System.exit(1);
}
System.out.println("Client connected to GNS");
JSONObject command;
if (args.length > 0 && args[0].equals("-write")) {
JSONObject json = new JSONObject("{\"occupation\":\"busboy\"," + "\"friends\":[\"Joe\",\"Sam\",\"Billy\"]," + "\"gibberish\":{\"meiny\":\"bloop\",\"einy\":\"floop\"}," + "\"location\":\"work\",\"name\":\"frank\"}");
command = createAndSignCommand(CommandType.ReplaceUserJSON, accountGuidEntry, GNSProtocol.GUID.toString(), accountGuidEntry.getGuid(), GNSProtocol.USER_JSON.toString(), json.toString(), GNSProtocol.WRITER.toString(), accountGuidEntry.getGuid());
} else {
command = createAndSignCommand(CommandType.Read, accountGuidEntry, GNSProtocol.GUID.toString(), accountGuidEntry.getGuid(), GNSProtocol.FIELD.toString(), "occupation", GNSProtocol.READER.toString(), accountGuidEntry.getGuid());
}
// Create the command packet with a bogus id
// arun: can not change request ID
CommandPacket commandPacket = new CommandPacket((long) (Math.random() * Long.MAX_VALUE), command);
// Keep track of what we've sent for the other thread to look at.
Set<Long> pendingIds = Collections.newSetFromMap(new ConcurrentHashMap<Long, Boolean>());
// Create and run another thread to pick up the responses
Runnable companion = new Runnable() {
@Override
public void run() {
lookForResponses(client, pendingIds);
}
};
//Does this on Android as of 9/16:
//ERROR: ClientAsynchExample.java:114: Lambda coming from jar file need their interfaces
//on the classpath to be compiled, unknown interfaces are java.lang.Runnable
// Runnable companion = () -> {
// lookForResponses(client, pendingIds);
// };
new Thread(companion).start();
while (true) {
//long id = client.generateNextRequestID();
// Important to set the new request id each time
//commandPacket.setClientRequestId(id);
// Record what we're sending
pendingIds.add(commandPacket.getRequestID());
// arun: disabled
if (true) {
throw new RuntimeException("disabled");
}
// Actually send out the packet
//client.sendCommandPacketAsynch(commandPacket);
// if you generate them too fast you'll clog things up
ThreadUtils.sleep(100);
}
}
use of java.security.spec.InvalidKeySpecException in project GNS by MobilityFirst.
the class SimpleClientExample method main.
/**
*
* @param args
* @throws IOException
* @throws InvalidKeySpecException
* @throws NoSuchAlgorithmException
* @throws ClientException
* @throws InvalidKeyException
* @throws SignatureException
* @throws Exception
*/
public static void main(String[] args) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException, ClientException, InvalidKeyException, SignatureException, Exception {
// Create the client. Connects to a default reconfigurator as specified in gigapaxos.properties file.
client = new GNSClientCommands();
try {
// Create an account guid if one doesn't already exists.
// The true makes it verbosely print out what it is doing.
// The password is for future use.
// Note that lookupOrCreateAccountGuid "cheats" by bypassing the account verification
// mechanisms.
accountGuid = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS, PASSWORD, true);
} catch (Exception e) {
System.out.println("Exception during accountGuid creation: " + e);
System.exit(1);
}
System.out.println("Client connected to GNS");
// Retrive the GUID using the account id
String guid = client.lookupGuid(ACCOUNT_ALIAS);
System.out.println("Retrieved GUID for " + ACCOUNT_ALIAS + ": " + guid);
// Get the public key from the GNS
PublicKey publicKey = client.publicKeyLookupFromGuid(guid);
System.out.println("Retrieved public key: " + publicKey.toString());
// Use the GuidEntry create an new record in the GNS
client.fieldUpdate(accountGuid, "homestate", "Florida");
System.out.println("Added homestate -> Florida record to the GNS for GUID " + accountGuid.getGuid());
// Retrive that record from the GNS
String result = client.fieldRead(accountGuid.getGuid(), "homestate", accountGuid);
System.out.println("Result of read location: " + result);
System.exit(0);
}
Aggregations