Search in sources :

Example 1 with InvalidParameterSpecException

use of java.security.spec.InvalidParameterSpecException in project XobotOS by xamarin.

the class JCERSACipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    AlgorithmParameterSpec paramSpec = null;
    if (params != null) {
        try {
            paramSpec = params.getParameterSpec(OAEPParameterSpec.class);
        } catch (InvalidParameterSpecException e) {
            throw new InvalidAlgorithmParameterException("cannot recognise parameters: " + e.toString(), e);
        }
    }
    engineParams = params;
    engineInit(opmode, key, paramSpec, random);
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) OAEPParameterSpec(javax.crypto.spec.OAEPParameterSpec)

Example 2 with InvalidParameterSpecException

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

the class TestNonexpanding method runTest.

public void runTest(String algo, String mo, String pad) throws Exception {
    Cipher ci = null;
    SecretKey key = null;
    try {
        // Initialization
        Random rdm = new Random();
        byte[] plainText = new byte[128];
        rdm.nextBytes(plainText);
        ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
        KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
        kg.init(KEY_LENGTH);
        key = kg.generateKey();
        // encrypt
        ci.init(Cipher.ENCRYPT_MODE, key);
        byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
        int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
        ci.doFinal(cipherText, offset);
        // Comparison
        if (!(plainText.length == cipherText.length)) {
            // authentication tag and cipher text.
            if (mo.equalsIgnoreCase("GCM")) {
                GCMParameterSpec spec = ci.getParameters().getParameterSpec(GCMParameterSpec.class);
                int cipherTextLength = cipherText.length - spec.getTLen() / 8;
                if (plainText.length == cipherTextLength) {
                    return;
                }
            }
            System.out.println("Original length: " + plainText.length);
            System.out.println("Cipher text length: " + cipherText.length);
            throw new RuntimeException("Test failed!");
        }
    } catch (NoSuchAlgorithmException e) {
        //CFB7 and OFB150 are for negative testing
        if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("OFB150")) {
            System.out.println("Unexpected NoSuchAlgorithmException with mode: " + mo);
            throw new RuntimeException("Test failed!");
        }
    } catch (NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | InvalidParameterSpecException | ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
        System.out.println("Test failed!");
        throw e;
    }
}
Also used : NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKey(javax.crypto.SecretKey) Random(java.util.Random) ShortBufferException(javax.crypto.ShortBufferException) Cipher(javax.crypto.Cipher) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) NoSuchProviderException(java.security.NoSuchProviderException) KeyGenerator(javax.crypto.KeyGenerator)

Example 3 with InvalidParameterSpecException

use of java.security.spec.InvalidParameterSpecException in project android_frameworks_base by AOSPA.

the class AndroidKeyStoreUnauthenticatedAESCipherSpi method engineGetParameters.

@Nullable
@Override
protected final AlgorithmParameters engineGetParameters() {
    if (!mIvRequired) {
        return null;
    }
    if ((mIv != null) && (mIv.length > 0)) {
        try {
            AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
            params.init(new IvParameterSpec(mIv));
            return params;
        } catch (NoSuchAlgorithmException e) {
            throw new ProviderException("Failed to obtain AES AlgorithmParameters", e);
        } catch (InvalidParameterSpecException e) {
            throw new ProviderException("Failed to initialize AES AlgorithmParameters with an IV", e);
        }
    }
    return null;
}
Also used : ProviderException(java.security.ProviderException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) AlgorithmParameters(java.security.AlgorithmParameters) Nullable(android.annotation.Nullable)

Example 4 with InvalidParameterSpecException

use of java.security.spec.InvalidParameterSpecException in project android_frameworks_base by AOSPA.

the class AndroidKeyStoreUnauthenticatedAESCipherSpi method initAlgorithmSpecificParameters.

@Override
protected final void initAlgorithmSpecificParameters(AlgorithmParameters params) throws InvalidAlgorithmParameterException {
    if (!mIvRequired) {
        if (params != null) {
            throw new InvalidAlgorithmParameterException("Unsupported parameters: " + params);
        }
        return;
    }
    // IV is used
    if (params == null) {
        if (!isEncrypting()) {
            // IV must be provided by the caller
            throw new InvalidAlgorithmParameterException("IV required when decrypting" + ". Use IvParameterSpec or AlgorithmParameters to provide it.");
        }
        return;
    }
    if (!"AES".equalsIgnoreCase(params.getAlgorithm())) {
        throw new InvalidAlgorithmParameterException("Unsupported AlgorithmParameters algorithm: " + params.getAlgorithm() + ". Supported: AES");
    }
    IvParameterSpec ivSpec;
    try {
        ivSpec = params.getParameterSpec(IvParameterSpec.class);
    } catch (InvalidParameterSpecException e) {
        if (!isEncrypting()) {
            // IV must be provided by the caller
            throw new InvalidAlgorithmParameterException("IV required when decrypting" + ", but not found in parameters: " + params, e);
        }
        mIv = null;
        return;
    }
    mIv = ivSpec.getIV();
    if (mIv == null) {
        throw new InvalidAlgorithmParameterException("Null IV in AlgorithmParameters");
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException)

Example 5 with InvalidParameterSpecException

use of java.security.spec.InvalidParameterSpecException in project sling by apache.

the class TopologyRequestValidator method encodeMessage.

/**
     * Encodes a request returning the encoded body
     *
     * @param body
     * @return the encoded body.
     * @throws IOException
     */
public String encodeMessage(String body) throws IOException {
    checkActive();
    if (encryptionEnabled) {
        try {
            JsonObjectBuilder json = Json.createObjectBuilder();
            JsonArrayBuilder array = Json.createArrayBuilder();
            for (String value : encrypt(body)) {
                array.add(value);
            }
            json.add("payload", array);
            StringWriter writer = new StringWriter();
            Json.createGenerator(writer).write(json.build()).close();
            return writer.toString();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (IllegalBlockSizeException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (BadPaddingException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (UnsupportedEncodingException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (NoSuchAlgorithmException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (NoSuchPaddingException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (JsonException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (InvalidKeySpecException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (InvalidParameterSpecException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        }
    }
    return body;
}
Also used : JsonException(javax.json.JsonException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IOException(java.io.IOException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) StringWriter(java.io.StringWriter) JsonArrayBuilder(javax.json.JsonArrayBuilder) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) JsonObjectBuilder(javax.json.JsonObjectBuilder)

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