Search in sources :

Example 21 with KeyException

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStream(java.io.DataInputStream) KeyException(java.security.KeyException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyException(java.security.KeyException)

Example 22 with KeyException

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);
}
Also used : SecretKey(javax.crypto.SecretKey) KeyException(java.security.KeyException)

Example 23 with KeyException

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);
    }
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) FileOutputStream(java.io.FileOutputStream) File(java.io.File) KeyException(java.security.KeyException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyException(java.security.KeyException)

Example 24 with KeyException

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;
}
Also used : Cipher(javax.crypto.Cipher) KeyException(java.security.KeyException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyException(java.security.KeyException)

Example 25 with KeyException

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();
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyGenerator(javax.crypto.KeyGenerator) KeyException(java.security.KeyException)

Aggregations

KeyException (java.security.KeyException)59 IOException (java.io.IOException)22 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 File (java.io.File)10 PublicKey (java.security.PublicKey)8 FileInputStream (java.io.FileInputStream)7 Cipher (javax.crypto.Cipher)6 Throwables.getStackTraceAsString (com.google.common.base.Throwables.getStackTraceAsString)5 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)5 LoginException (javax.security.auth.login.LoginException)5 Credentials (org.ow2.proactive.authentication.crypto.Credentials)5 FileNotFoundException (java.io.FileNotFoundException)4 PrivateKey (java.security.PrivateKey)4 CredData (org.ow2.proactive.authentication.crypto.CredData)4 RMException (org.ow2.proactive.resourcemanager.exception.RMException)4 CommandLineBuilder (org.ow2.proactive.resourcemanager.utils.CommandLineBuilder)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 DataInputStream (java.io.DataInputStream)3 InputStream (java.io.InputStream)3 Key (java.security.Key)3