Search in sources :

Example 21 with InvalidParameterSpecException

use of java.security.spec.InvalidParameterSpecException in project jdk8u_jdk by JetBrains.

the class Cipher method checkCryptoPerm.

private void checkCryptoPerm(CipherSpi checkSpi, Key key, AlgorithmParameters params) throws InvalidKeyException, InvalidAlgorithmParameterException {
    if (cryptoPerm == CryptoAllPermission.INSTANCE) {
        return;
    }
    // Convert the specified parameters into specs and then delegate.
    AlgorithmParameterSpec pSpec;
    try {
        pSpec = getAlgorithmParameterSpec(params);
    } catch (InvalidParameterSpecException ipse) {
        throw new InvalidAlgorithmParameterException("Failed to retrieve algorithm parameter specification");
    }
    checkCryptoPerm(checkSpi, key, pSpec);
}
Also used : InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 22 with InvalidParameterSpecException

use of java.security.spec.InvalidParameterSpecException in project metrics by dropwizard.

the class PacketWriter method encrypt.

private static EncryptionResult encrypt(byte[] password, ByteBuffer input) {
    final Cipher cipher;
    try {
        cipher = Cipher.getInstance(AES_CYPHER);
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(sha256(password), AES));
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
        throw new RuntimeException(e);
    }
    final byte[] iv;
    try {
        iv = cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
    } catch (InvalidParameterSpecException e) {
        throw new RuntimeException(e);
    }
    if (iv.length != IV_LENGTH) {
        throw new IllegalStateException("Bad initialization vector");
    }
    final ByteBuffer output = ByteBuffer.allocate(input.remaining() * 2);
    try {
        cipher.doFinal(input, output);
    } catch (ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
        throw new RuntimeException(e);
    }
    return new EncryptionResult(iv, (ByteBuffer) output.flip());
}
Also used : NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) ByteBuffer(java.nio.ByteBuffer) SecretKeySpec(javax.crypto.spec.SecretKeySpec) ShortBufferException(javax.crypto.ShortBufferException) Cipher(javax.crypto.Cipher) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException)

Example 23 with InvalidParameterSpecException

use of java.security.spec.InvalidParameterSpecException in project Bytecoder by mirkosertic.

the class GCMParameters method engineInit.

protected void engineInit(AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException {
    if (!(paramSpec instanceof GCMParameterSpec)) {
        throw new InvalidParameterSpecException("Inappropriate parameter specification");
    }
    GCMParameterSpec gps = (GCMParameterSpec) paramSpec;
    // need to convert from bits to bytes for ASN.1 encoding
    this.tLen = gps.getTLen() / 8;
    this.iv = gps.getIV();
}
Also used : GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException)

Example 24 with InvalidParameterSpecException

use of java.security.spec.InvalidParameterSpecException in project Bytecoder by mirkosertic.

the class RC2Parameters method engineInit.

protected void engineInit(AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException {
    if (!(paramSpec instanceof RC2ParameterSpec)) {
        throw new InvalidParameterSpecException("Inappropriate parameter specification");
    }
    RC2ParameterSpec rps = (RC2ParameterSpec) paramSpec;
    // check effective key size (a value of 0 means it is unspecified)
    effectiveKeySize = rps.getEffectiveKeyBits();
    if (effectiveKeySize != 0) {
        if (effectiveKeySize < 1 || effectiveKeySize > 1024) {
            throw new InvalidParameterSpecException("RC2 effective key " + "size must be between 1 and 1024 bits");
        }
        if (effectiveKeySize < 256) {
            version = EKB_TABLE[effectiveKeySize];
        } else {
            version = effectiveKeySize;
        }
    }
    this.iv = rps.getIV();
}
Also used : RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException)

Example 25 with InvalidParameterSpecException

use of java.security.spec.InvalidParameterSpecException in project Bytecoder by mirkosertic.

the class DSAParameterGenerator method engineGenerateParameters.

/**
 * Generates the parameters.
 *
 * @return the new AlgorithmParameters object
 */
@Override
protected AlgorithmParameters engineGenerateParameters() {
    AlgorithmParameters algParams = null;
    try {
        if (this.random == null) {
            this.random = new SecureRandom();
        }
        if (valueL == -1) {
            try {
                engineInit(DEFAULTS, this.random);
            } catch (InvalidAlgorithmParameterException iape) {
            // should never happen
            }
        }
        BigInteger[] pAndQ = generatePandQ(this.random, valueL, valueN, seedLen);
        BigInteger paramP = pAndQ[0];
        BigInteger paramQ = pAndQ[1];
        BigInteger paramG = generateG(paramP, paramQ);
        DSAParameterSpec dsaParamSpec = new DSAParameterSpec(paramP, paramQ, paramG);
        algParams = AlgorithmParameters.getInstance("DSA", "SUN");
        algParams.init(dsaParamSpec);
    } catch (InvalidParameterSpecException e) {
        // this should never happen
        throw new RuntimeException(e.getMessage());
    } catch (NoSuchAlgorithmException e) {
        // this should never happen, because we provide it
        throw new RuntimeException(e.getMessage());
    } catch (NoSuchProviderException e) {
        // this should never happen, because we provide it
        throw new RuntimeException(e.getMessage());
    }
    return algParams;
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) BigInteger(java.math.BigInteger) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) AlgorithmParameters(java.security.AlgorithmParameters)

Aggregations

InvalidParameterSpecException (java.security.spec.InvalidParameterSpecException)54 AlgorithmParameters (java.security.AlgorithmParameters)19 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)15 IvParameterSpec (javax.crypto.spec.IvParameterSpec)14 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)11 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)11 InvalidKeyException (java.security.InvalidKeyException)6 DSAParameterSpec (java.security.spec.DSAParameterSpec)6 OAEPParameterSpec (javax.crypto.spec.OAEPParameterSpec)6 Nullable (android.annotation.Nullable)5 ProviderException (java.security.ProviderException)5 DSAParams (java.security.interfaces.DSAParams)4 BadPaddingException (javax.crypto.BadPaddingException)4 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)4 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)4 IOException (java.io.IOException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 BigInteger (java.math.BigInteger)3 NoSuchProviderException (java.security.NoSuchProviderException)3 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)3