Search in sources :

Example 11 with BadPaddingException

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

the class CipherTest method testDoFinalbyteArrayintintbyteArrayint.

/*
     * Class under test for int doFinal(byte[], int, int, byte[], int)
     */
public void testDoFinalbyteArrayintintbyteArrayint() throws Exception {
    byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    byte[] b1 = new byte[30];
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    try {
        c.doFinal(b, 0, 10, b1, 5);
        fail();
    } catch (IllegalBlockSizeException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        c.doFinal(b, 0, 10, b1, 5);
        fail();
    } catch (IllegalStateException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    int len = c.doFinal(b, 0, 16, b1, 0);
    assertEquals(16, len);
    c = Cipher.getInstance("DES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
    try {
        c.doFinal(b1, 0, 24, new byte[42], 0);
        fail();
    } catch (BadPaddingException expected) {
    }
    b1 = new byte[6];
    c = Cipher.getInstance("DESede");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
    try {
        c.doFinal(b, 3, 6, b1, 5);
        fail();
    } catch (IllegalBlockSizeException maybeExpected) {
        assertTrue(StandardNames.IS_RI);
    } catch (ShortBufferException maybeExpected) {
        assertFalse(StandardNames.IS_RI);
    }
}
Also used : IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) ShortBufferException(javax.crypto.ShortBufferException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) BadPaddingException(javax.crypto.BadPaddingException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 12 with BadPaddingException

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

the class BadPaddingExceptionTest method testBadPaddingException03.

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

Example 13 with BadPaddingException

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

the class BadPaddingExceptionTest method testBadPaddingException01.

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

Example 14 with BadPaddingException

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

the class BadPaddingExceptionTest method testBadPaddingException02.

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

Example 15 with BadPaddingException

use of javax.crypto.BadPaddingException in project Signal-Android by WhisperSystems.

the class DecryptingPartInputStream method readFinal.

private int readFinal(byte[] buffer, int offset, int length) throws IOException {
    try {
        int flourish = cipher.doFinal(buffer, offset);
        //mac.update(buffer, offset, flourish);
        byte[] ourMac = mac.doFinal();
        byte[] theirMac = new byte[mac.getMacLength()];
        readFully(theirMac);
        if (!Arrays.equals(ourMac, theirMac))
            throw new IOException("MAC doesn't match! Potential tampering?");
        done = true;
        return flourish;
    } catch (IllegalBlockSizeException e) {
        Log.w(TAG, e);
        throw new IOException("Illegal block size exception!");
    } catch (ShortBufferException e) {
        Log.w(TAG, e);
        throw new IOException("Short buffer exception!");
    } catch (BadPaddingException e) {
        Log.w(TAG, e);
        throw new IOException("Bad padding exception!");
    }
}
Also used : IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) ShortBufferException(javax.crypto.ShortBufferException) IOException(java.io.IOException) BadPaddingException(javax.crypto.BadPaddingException)

Aggregations

BadPaddingException (javax.crypto.BadPaddingException)120 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)103 InvalidKeyException (java.security.InvalidKeyException)80 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)70 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)69 Cipher (javax.crypto.Cipher)53 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)45 IOException (java.io.IOException)39 KeyStoreException (java.security.KeyStoreException)25 UnrecoverableKeyException (java.security.UnrecoverableKeyException)25 CertificateException (java.security.cert.CertificateException)25 SecretKey (javax.crypto.SecretKey)25 IvParameterSpec (javax.crypto.spec.IvParameterSpec)25 SecretKeySpec (javax.crypto.spec.SecretKeySpec)23 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)17 RemoteException (android.os.RemoteException)15 ShortBufferException (javax.crypto.ShortBufferException)14 KeyGenerator (javax.crypto.KeyGenerator)13 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 FileNotFoundException (java.io.FileNotFoundException)11