Search in sources :

Example 51 with KeyException

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

Example 52 with KeyException

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

Example 53 with KeyException

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

Example 54 with KeyException

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

Example 55 with KeyException

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