use of java.security.KeyException in project scheduling by ow2-proactive.
the class Credentials method getBase64.
/**
* Returns a representation of this credentials as a base64 encoded byte array
* <p>
* Prior to base64 encoding, format is the following:
* <ul>
* <li>The key generation algorithm, in human readable format, on a single
* line
* <li>The key size, in human readable format, on a single line
* <li>The cipher parameters, in human readable format, on a single line
* <li>The encrypted AES key, which should be exactly <code>size / 8</code> bytes
* <li>The encrypted data, which can be of arbitrary length, should occupy the rest of the file
* </ul>
* @throws KeyException
*/
public byte[] getBase64() throws KeyException {
ByteArrayOutputStream b = new ByteArrayOutputStream();
try {
b.write((algorithm + '\n').getBytes());
b.write(("" + size + '\n').getBytes());
b.write((cipher + '\n').getBytes());
b.write(this.aes);
b.write(this.data);
} catch (IOException e) {
}
byte[] ret;
try {
ret = Base64.encodeBase64(b.toByteArray());
} catch (Exception e) {
throw new KeyException("Unable to encode credentials to base64", e);
}
return ret;
}
use of java.security.KeyException in project scheduling by ow2-proactive.
the class Credentials method getCredentials.
/**
* Retrieves a credentials from disk
* <p>
* See {@link org.ow2.proactive.authentication.crypto.Credentials#writeToDisk(String)} for details on how information is
* stored on disk.
*
* @param path to the file in which credentials are stored
* @return the Credentials object represented by the file located at <code>path</code>
* @throws KeyException Credentials could not be recovered
*/
public static Credentials getCredentials(String path) throws KeyException {
File f = new File(path);
byte[] bytes = new byte[(int) f.length()];
try (FileInputStream fin = new FileInputStream(f)) {
fin.read(bytes);
} catch (Exception e) {
throw new KeyException("Could not read credentials from " + path, e);
}
return getCredentialsBase64(bytes);
}
use of java.security.KeyException in project scheduling by ow2-proactive.
the class HybridEncryptionUtil method decrypt.
public static byte[] decrypt(PrivateKey privateKey, String cipher, HybridEncryptedData encryptedData) throws KeyException {
byte[] decryptedData;
byte[] decryptedSymmetricKey;
// recover clear AES key using the private key
try {
decryptedSymmetricKey = KeyPairUtil.decrypt(privateKey, cipher, encryptedData.getEncryptedSymmetricKey());
} catch (KeyException e) {
throw new KeyException("Could not decrypt symmetric key", e);
}
// recover clear credentials using the AES key
try {
decryptedData = KeyUtil.decrypt(new SecretKeySpec(decryptedSymmetricKey, AES_ALGO), AES_CIPHER, encryptedData.getEncryptedData());
} catch (KeyException e) {
throw new KeyException("Could not decrypt data", e);
}
return decryptedData;
}
use of java.security.KeyException in project scheduling by ow2-proactive.
the class KeyGen method main.
/**
* Entry point
*
* @param args arguments, try '-h' for help
* @throws KeyException keypair generation failure
*/
public static void main(String[] args) throws KeyException {
/**
* Default values
*/
// should work fine with default providers
String algo = "RSA";
String pubKey = null;
String privKey = null;
int size = 1024;
/**
* Arguments handling
*/
int index = 0;
while (index < args.length) {
if (args[index].equals("--size") || args[index].equals("-s")) {
if (++index == args.length) {
printError("No value provided for option --size");
printUsage(algo, size);
return;
}
size = Integer.parseInt(args[index]);
}
if (args[index].equals("--private") || args[index].equals("-p")) {
if (++index == args.length) {
printError("No value provided for argument --private");
printUsage(algo, size);
return;
}
privKey = args[index];
}
if (args[index].equals("--public") || args[index].equals("-P")) {
if (++index == args.length) {
printError("No value provided for argument --public");
printUsage(algo, size);
return;
}
pubKey = args[index];
}
if (args[index].equals("--algo") || args[index].equals("-a")) {
if (++index == args.length) {
printError("No value provided for option --algo");
printUsage(algo, size);
return;
}
algo = args[index];
}
if (args[index].equals("--help") || args[index].equals("-h")) {
printUsage(algo, size);
return;
}
index++;
}
if (privKey == null) {
printError("--private argument is mandatory");
printUsage(algo, size);
return;
}
if (pubKey == null) {
printError("--public argument is mandatory");
printUsage(algo, size);
return;
}
/**
* Create directories if not existing
*/
try {
File f = new File(pubKey).getParentFile();
if (f != null && !f.isDirectory()) {
f.mkdirs();
}
f = new File(privKey).getParentFile();
if (f != null && !f.isDirectory()) {
f.mkdirs();
}
} catch (Exception e) {
printError("Could not create directory: " + e.getMessage());
}
KeyPairUtil.generateKeyPair(algo, size, privKey, pubKey);
System.out.println("Successfully stored generated keypair at:");
System.out.println("\t" + privKey);
System.out.println("\t" + pubKey);
}
use of java.security.KeyException in project scheduling by ow2-proactive.
the class KeyPairUtil method encrypt.
/**
* Encrypt a message using asymmetric keys
*
* @param pubKey public key used for encryption
* @param cipherParams cipher parameters: transformations (ie RSA/ECB/NoPadding)
* @param message the message to encrypt
* @return the encrypted message
* @throws KeyException encryption failed, public key recovery failed
*/
public static synchronized byte[] encrypt(PublicKey pubKey, String cipherParams, byte[] message) throws KeyException {
Cipher ciph = null;
try {
ciph = Cipher.getInstance(cipherParams);
ciph.init(Cipher.ENCRYPT_MODE, pubKey, KeyUtil.getSecureRandom());
} catch (Exception e) {
throw new KeyException("Could not initialize cipher", e);
}
byte[] res = null;
try {
res = ciph.doFinal(message);
} catch (Exception e) {
throw new KeyException("Could not encrypt message.", e);
}
return res;
}
Aggregations