use of java.security.KeyException in project scheduling by ow2-proactive.
the class Credentials method getCredentialsBase64.
/**
* Creates a Credentials given its base64 encoded representation
*
* @param base64enc the Credentials representation as a base64 encoded byte array,
* as returned by {@link Credentials#getBase64()}
* @return the Credentials object corresponding the <code>base64en</code> representation
* @throws KeyException
*/
public static Credentials getCredentialsBase64(byte[] base64enc) throws KeyException {
String algo = "", cipher = "", tmp = "";
byte[] data;
byte[] aes;
int size;
byte[] asciiEnc;
try {
asciiEnc = Base64.decodeBase64(base64enc);
} catch (Exception e) {
throw new KeyException("Unable to decode base64 credentials", e);
}
try {
DataInputStream in = new DataInputStream(new ByteArrayInputStream(asciiEnc));
int read, tot = 0;
while ((read = in.read()) != '\n') {
if (read == -1)
throw new KeyException("Failed to parse malformed credentials");
algo += (char) read;
tot++;
}
tot++;
while ((read = in.read()) != '\n') {
if (read == -1)
throw new KeyException("Failed to parse malformed credentials");
tmp += (char) read;
tot++;
}
tot++;
size = Integer.parseInt(tmp);
while ((read = in.read()) != '\n') {
if (read == -1)
throw new KeyException("Failed to parse malformed credentials");
cipher += (char) read;
tot++;
}
tot++;
aes = new byte[size / 8];
for (int i = 0; i < size / 8; i++) {
aes[i] = (byte) in.read();
tot++;
}
data = new byte[asciiEnc.length - tot];
in.readFully(data);
} catch (Exception e) {
throw new KeyException("Could not decode credentials", e);
}
return new Credentials(algo, size, cipher, aes, data);
}
use of java.security.KeyException in project scheduling by ow2-proactive.
the class HybridEncryptionUtil method encrypt.
public static HybridEncryptedData encrypt(PublicKey publicKey, String cipher, byte[] message) throws KeyException {
// generate symmetric key
SecretKey aesKey = KeyUtil.generateKey(AES_ALGO, AES_KEYSIZE);
byte[] encData;
byte[] encAes;
// encrypt AES key with public RSA key
try {
encAes = KeyPairUtil.encrypt(publicKey, cipher, aesKey.getEncoded());
} catch (KeyException e) {
throw new KeyException("Symmetric key encryption failed", e);
}
// encrypt clear credentials with AES key
try {
encData = KeyUtil.encrypt(aesKey, AES_CIPHER, message);
} catch (KeyException e) {
throw new KeyException("Message encryption failed", e);
}
return new HybridEncryptedData(encAes, encData);
}
use of java.security.KeyException in project scheduling by ow2-proactive.
the class KeyPairUtil method generateKeyPair.
/**
* Generates a pair of public and private keys
*
* @param algorithm algorithm used for key generation, ie RSA
* @param size size of the generated key, must be power of 2 and greater than 512
* @param privPath path to file to which the generated private key will be saved
* @param pubPath path to file to which the generated public key will be saved
* @throws KeyException key generation or saving failed
*/
public static void generateKeyPair(String algorithm, int size, String privPath, String pubPath) throws KeyException {
KeyPair keyPair = generateKeyPair(algorithm, size);
PrivateKey privKey = keyPair.getPrivate();
PublicKey pubKey = keyPair.getPublic();
try (FileOutputStream out = new FileOutputStream(new File(privPath))) {
out.write(privKey.getEncoded());
} catch (Exception e) {
throw new KeyException("Cannot write private key to disk", e);
}
try (FileOutputStream out = new FileOutputStream(new File(pubPath))) {
out.write((algorithm + "\n").getBytes());
out.write((size + "\n").getBytes());
out.write(pubKey.getEncoded());
} catch (Exception e) {
throw new KeyException("Cannot write public key to disk", e);
}
}
use of java.security.KeyException in project scheduling by ow2-proactive.
the class KeyUtil method decrypt.
/**
* Decrypt a message using a symmetric key
*
* @param key secret key used for decryption
* @param cipherParams cipher parameters: transformations, ie AES
* @param message the encrypted message
* @return the decrypted message
* @throws KeyException private key recovery failed, decryption failed
*/
public static synchronized byte[] decrypt(SecretKey key, String cipherParams, byte[] message) throws KeyException {
Cipher ciph = null;
try {
ciph = Cipher.getInstance(cipherParams);
ciph.init(Cipher.DECRYPT_MODE, key);
} catch (Exception e) {
e.printStackTrace();
throw new KeyException("Coult not initialize cipher", e);
}
byte[] res = null;
try {
res = ciph.doFinal(message);
} catch (Exception e) {
throw new KeyException("Could not decrypt message", e);
}
return res;
}
use of java.security.KeyException in project scheduling by ow2-proactive.
the class KeyUtil method generateKey.
/**
* Generates a secret symmetric key
*
* @param algorithm algorithm used for key generation, ie AES
* @param size size of the generated key, must be one of 128, 192, 256. Use 128 when unsure,
* default configurations and providers should refuse to use longer keys.
* @return the generated key
* @throws KeyException key generation or saving failed
*/
public static synchronized SecretKey generateKey(String algorithm, int size) throws KeyException {
KeyGenerator keyGen = null;
try {
keyGen = KeyGenerator.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
throw new KeyException("Cannot initialize key generator", e);
}
keyGen.init(size, getSecureRandom());
return keyGen.generateKey();
}
Aggregations