Search in sources :

Example 51 with InvalidParameterSpecException

use of java.security.spec.InvalidParameterSpecException in project j2objc by google.

the class AlgorithmParameters method init.

/**
 * Initializes this parameter object using the parameters
 * specified in {@code paramSpec}.
 *
 * @param paramSpec the parameter specification.
 *
 * @exception InvalidParameterSpecException if the given parameter
 * specification is inappropriate for the initialization of this parameter
 * object, or if this parameter object has already been initialized.
 */
public final void init(AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException {
    if (this.initialized)
        throw new InvalidParameterSpecException("already initialized");
    paramSpi.engineInit(paramSpec);
    this.initialized = true;
}
Also used : InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException)

Example 52 with InvalidParameterSpecException

use of java.security.spec.InvalidParameterSpecException in project Payara by payara.

the class HazelcastSymmetricEncryptor method encode.

public static byte[] encode(byte[] value) {
    // Generate IV.
    byte[] saltBytes = new byte[20];
    random.nextBytes(saltBytes);
    try {
        // Encrypting the data
        Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        AlgorithmParameters params = cipher.getParameters();
        byte[] ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV();
        byte[] encryptedTextBytes = cipher.doFinal(value);
        // Prepend salt and IV
        byte[] buffer = new byte[saltBytes.length + ivBytes.length + encryptedTextBytes.length];
        System.arraycopy(saltBytes, 0, buffer, 0, saltBytes.length);
        System.arraycopy(ivBytes, 0, buffer, saltBytes.length, ivBytes.length);
        System.arraycopy(encryptedTextBytes, 0, buffer, saltBytes.length + ivBytes.length, encryptedTextBytes.length);
        return buffer;
    } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException | InvalidParameterSpecException | BadPaddingException exception) {
        throw new HazelcastException(exception);
    }
}
Also used : HazelcastException(com.hazelcast.core.HazelcastException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 53 with InvalidParameterSpecException

use of java.security.spec.InvalidParameterSpecException in project Payara by payara.

the class GenerateEncryptionKey method generateAndEncryptKey.

private byte[] generateAndEncryptKey(char[] masterpasswordChars) throws CommandException {
    byte[] saltBytes = new byte[20];
    random.nextBytes(saltBytes);
    try {
        // Derive the key
        SecretKeyFactory factory = SecretKeyFactory.getInstance(PBKDF_ALGORITHM);
        PBEKeySpec spec = new PBEKeySpec(masterpasswordChars, saltBytes, ITERATION_COUNT, KEYSIZE);
        SecretKeySpec secret = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), AES);
        // Encrypting the data
        Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secret);
        AlgorithmParameters params = cipher.getParameters();
        byte[] ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV();
        // Key length is in bits !
        byte[] keyBytes = new byte[KEYSIZE / 8];
        random.nextBytes(keyBytes);
        byte[] encryptedTextBytes = cipher.doFinal(keyBytes);
        // Prepend salt and IV
        byte[] buffer = new byte[saltBytes.length + ivBytes.length + encryptedTextBytes.length];
        System.arraycopy(saltBytes, 0, buffer, 0, saltBytes.length);
        System.arraycopy(ivBytes, 0, buffer, saltBytes.length, ivBytes.length);
        System.arraycopy(encryptedTextBytes, 0, buffer, saltBytes.length + ivBytes.length, encryptedTextBytes.length);
        return buffer;
    } catch (InvalidKeySpecException | NoSuchAlgorithmException | BadPaddingException | InvalidKeyException | NoSuchPaddingException | InvalidParameterSpecException | IllegalBlockSizeException exception) {
        throw new CommandException(exception.getMessage(), exception);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) CommandException(org.glassfish.api.admin.CommandException) InvalidKeyException(java.security.InvalidKeyException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory) AlgorithmParameters(java.security.AlgorithmParameters)

Example 54 with InvalidParameterSpecException

use of java.security.spec.InvalidParameterSpecException in project perun by CESNET.

the class urn_perun_user_attribute_def_def_sshPublicKey method getECParameterSpec.

/**
 * Gets the curve parameters for the given key type identifier.
 *
 * @param identifier According to RFC 5656:
 *                   "The string [identifier] is the identifier of the elliptic curve domain parameters."
 * @return An ECParameterSpec suitable for creating a JCE ECPublicKeySpec.
 */
private ECParameterSpec getECParameterSpec(String identifier) {
    try {
        // http://www.bouncycastle.org/wiki/pages/viewpage.action?pageId=362269#SupportedCurves(ECDSAandECGOST)-NIST(aliasesforSECcurves)
        String name = identifier.replace("nist", "sec") + "r1";
        AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
        parameters.init(new ECGenParameterSpec(name));
        return parameters.getParameterSpec(ECParameterSpec.class);
    } catch (InvalidParameterSpecException | NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("Unable to parse curve parameters: ", e);
    }
}
Also used : ECGenParameterSpec(java.security.spec.ECGenParameterSpec) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 55 with InvalidParameterSpecException

use of java.security.spec.InvalidParameterSpecException in project druid by druid-io.

the class CryptoService method encrypt.

public byte[] encrypt(byte[] plain) {
    try {
        byte[] salt = new byte[saltSize];
        SECURE_RANDOM_INSTANCE.nextBytes(salt);
        SecretKey tmp = getKeyFromPassword(passPhrase, salt);
        SecretKey secret = new SecretKeySpec(tmp.getEncoded(), cipherAlgName);
        // error-prone warns if the transformation is not a compile-time constant
        // since it cannot check it for insecure combinations.
        @SuppressWarnings("InsecureCryptoUsage") Cipher ecipher = Cipher.getInstance(transformation);
        ecipher.init(Cipher.ENCRYPT_MODE, secret);
        return new EncryptedData(salt, ecipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV(), ecipher.doFinal(plain)).toByteAray();
    } catch (InvalidKeySpecException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidParameterSpecException | IllegalBlockSizeException | BadPaddingException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException)

Aggregations

InvalidParameterSpecException (java.security.spec.InvalidParameterSpecException)57 AlgorithmParameters (java.security.AlgorithmParameters)22 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)18 IvParameterSpec (javax.crypto.spec.IvParameterSpec)16 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)11 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)11 InvalidKeyException (java.security.InvalidKeyException)8 BadPaddingException (javax.crypto.BadPaddingException)7 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)7 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)7 DSAParameterSpec (java.security.spec.DSAParameterSpec)6 Cipher (javax.crypto.Cipher)6 OAEPParameterSpec (javax.crypto.spec.OAEPParameterSpec)6 Nullable (android.annotation.Nullable)5 ProviderException (java.security.ProviderException)5 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)5 BigInteger (java.math.BigInteger)4 DSAParams (java.security.interfaces.DSAParams)4 SecretKeySpec (javax.crypto.spec.SecretKeySpec)4 IOException (java.io.IOException)3