Search in sources :

Example 11 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException 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)

Example 12 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project robovm by robovm.

the class NoSuchPaddingExceptionTest method testNoSuchPaddingException01.

/**
     * Test for <code>NoSuchPaddingException()</code> constructor Assertion:
     * constructs NoSuchPaddingException with no detail message
     */
public void testNoSuchPaddingException01() {
    NoSuchPaddingException tE = new NoSuchPaddingException();
    assertNull("getMessage() must return null.", tE.getMessage());
    assertNull("getCause() must return null", tE.getCause());
}
Also used : NoSuchPaddingException(javax.crypto.NoSuchPaddingException)

Example 13 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project robovm by robovm.

the class NoSuchPaddingExceptionTest method testNoSuchPaddingException03.

/**
     * Test for <code>NoSuchPaddingException(String)</code> constructor
     * Assertion: constructs NoSuchPaddingException when <code>msg</code> is
     * null
     */
public void testNoSuchPaddingException03() {
    String msg = null;
    NoSuchPaddingException tE = new NoSuchPaddingException(msg);
    assertNull("getMessage() must return null.", tE.getMessage());
    assertNull("getCause() must return null", tE.getCause());
}
Also used : NoSuchPaddingException(javax.crypto.NoSuchPaddingException)

Example 14 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project robovm by robovm.

the class NoSuchPaddingExceptionTest method testNoSuchPaddingException02.

/**
     * Test for <code>NoSuchPaddingException(String)</code> constructor
     * Assertion: constructs NoSuchPaddingException with detail message msg.
     * Parameter <code>msg</code> is not null.
     */
public void testNoSuchPaddingException02() {
    NoSuchPaddingException tE;
    for (int i = 0; i < msgs.length; i++) {
        tE = new NoSuchPaddingException(msgs[i]);
        assertEquals("getMessage() must return: ".concat(msgs[i]), tE.getMessage(), msgs[i]);
        assertNull("getCause() must return null", tE.getCause());
    }
}
Also used : NoSuchPaddingException(javax.crypto.NoSuchPaddingException)

Example 15 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project robovm by robovm.

the class AlgorithmParameterSymmetricHelper method test.

@Override
public void test(AlgorithmParameters parameters) {
    KeyGenerator generator = null;
    try {
        generator = KeyGenerator.getInstance(algorithmName);
    } catch (NoSuchAlgorithmException e) {
        Assert.fail(e.getMessage());
    }
    generator.init(keySize);
    Key key = generator.generateKey();
    Cipher cipher = null;
    try {
        String transformation = algorithmName;
        if (blockmode != null) {
            transformation += "/" + blockmode;
        }
        cipher = Cipher.getInstance(transformation);
    } catch (NoSuchAlgorithmException e) {
        Assert.fail(e.getMessage());
    } catch (NoSuchPaddingException e) {
        Assert.fail(e.getMessage());
    }
    try {
        cipher.init(Cipher.ENCRYPT_MODE, key, parameters);
    } catch (InvalidKeyException e) {
        Assert.fail(e.getMessage());
    } catch (InvalidAlgorithmParameterException e) {
        Assert.fail(e.getMessage());
    }
    byte[] bs = null;
    try {
        bs = cipher.doFinal(plainData.getBytes());
    } catch (IllegalBlockSizeException e) {
        Assert.fail(e.getMessage());
    } catch (BadPaddingException e) {
        Assert.fail(e.getMessage());
    }
    try {
        cipher.init(Cipher.DECRYPT_MODE, key, parameters);
    } catch (InvalidKeyException e) {
        Assert.fail(e.getMessage());
    } catch (InvalidAlgorithmParameterException e) {
        Assert.fail(e.getMessage());
    }
    byte[] decrypted = null;
    try {
        decrypted = cipher.doFinal(bs);
    } catch (IllegalBlockSizeException e) {
        Assert.fail(e.getMessage());
    } catch (BadPaddingException e) {
        Assert.fail(e.getMessage());
    }
    Assert.assertTrue(Arrays.equals(plainData.getBytes(), decrypted));
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Cipher(javax.crypto.Cipher) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Aggregations

NoSuchPaddingException (javax.crypto.NoSuchPaddingException)259 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)237 InvalidKeyException (java.security.InvalidKeyException)216 Cipher (javax.crypto.Cipher)187 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)181 BadPaddingException (javax.crypto.BadPaddingException)180 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)119 SecretKeySpec (javax.crypto.spec.SecretKeySpec)91 IOException (java.io.IOException)83 IvParameterSpec (javax.crypto.spec.IvParameterSpec)66 SecretKey (javax.crypto.SecretKey)45 KeyStoreException (java.security.KeyStoreException)40 CertificateException (java.security.cert.CertificateException)40 UnrecoverableKeyException (java.security.UnrecoverableKeyException)35 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)30 UnsupportedEncodingException (java.io.UnsupportedEncodingException)27 NoSuchProviderException (java.security.NoSuchProviderException)27 GCMParameterSpec (javax.crypto.spec.GCMParameterSpec)18 FileNotFoundException (java.io.FileNotFoundException)16 SecureRandom (java.security.SecureRandom)16