Search in sources :

Example 86 with BadPaddingException

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

the class CipherTest method test_doFinal.

/**
     * javax.crypto.Cipher#doFinal()
     */
public void test_doFinal() throws Exception {
    for (int index = 1; index < 4; index++) {
        Cipher c = Cipher.getInstance("DESEDE/CBC/PKCS5Padding");
        byte[] keyMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".key");
        DESedeKeySpec keySpec = new DESedeKeySpec(keyMaterial);
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DESEDE");
        Key k = skf.generateSecret(keySpec);
        byte[] ivMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".iv");
        IvParameterSpec iv = new IvParameterSpec(ivMaterial);
        c.init(Cipher.ENCRYPT_MODE, k, iv);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] input = new byte[256];
        String resPath = "hyts_" + "des-ede3-cbc.test" + index + ".plaintext";
        File resources = Support_Resources.createTempFolder();
        Support_Resources.copyFile(resources, null, resPath);
        InputStream is = Support_Resources.getStream(resPath);
        int bytesRead = is.read(input, 0, 256);
        while (bytesRead > 0) {
            byte[] output = c.update(input, 0, bytesRead);
            if (output != null) {
                baos.write(output);
            }
            bytesRead = is.read(input, 0, 256);
        }
        byte[] output = c.doFinal();
        if (output != null) {
            baos.write(output);
        }
        byte[] encryptedPlaintext = baos.toByteArray();
        is.close();
        byte[] cipherText = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".ciphertext");
        assertEquals("Operation produced incorrect results for index " + index, Arrays.toString(cipherText), Arrays.toString(encryptedPlaintext));
    }
    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);
    c.update(b, 0, 10, b1, 5);
    try {
        c.doFinal();
        fail();
    } catch (IllegalBlockSizeException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        c.doFinal();
        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);
    assertTrue(Arrays.equals(c.getIV(), IV));
    c.update(b1, 0, 24, b, 0);
    try {
        c.doFinal();
        fail();
    } catch (BadPaddingException expected) {
    }
}
Also used : InputStream(java.io.InputStream) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BadPaddingException(javax.crypto.BadPaddingException) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) File(java.io.File) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) Key(java.security.Key)

Example 87 with BadPaddingException

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

the class CipherTest method test_doFinalLjava_nio_ByteBufferLjava_nio_ByteBuffer.

public void test_doFinalLjava_nio_ByteBufferLjava_nio_ByteBuffer() throws Exception {
    byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    ByteBuffer bInput = ByteBuffer.allocate(64);
    ByteBuffer bOutput = ByteBuffer.allocate(64);
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    bInput.put(b, 0, 10);
    try {
        c.doFinal(bInput, bOutput);
        fail();
    } catch (IllegalBlockSizeException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        c.doFinal(bInput, bOutput);
        fail();
    } catch (IllegalStateException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    bInput = ByteBuffer.allocate(16);
    bInput.put(b, 0, 16);
    int len = c.doFinal(bInput, bOutput);
    assertEquals(0, len);
    c = Cipher.getInstance("DES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
    bInput = ByteBuffer.allocate(64);
    try {
        c.doFinal(bOutput, bInput);
        fail();
    } catch (BadPaddingException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES);
    bInput.put(b, 0, 16);
    try {
        c.doFinal(bInput, bInput);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES);
    bInput.put(b, 0, 16);
    try {
        c.doFinal(bInput, bOutput.asReadOnlyBuffer());
        fail();
    } catch (ReadOnlyBufferException expected) {
    }
    bInput.rewind();
    bInput.put(b, 0, 16);
    bOutput = ByteBuffer.allocate(8);
    c = Cipher.getInstance("DESede");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
    try {
        c.doFinal(bInput, bOutput);
        fail();
    } catch (ShortBufferException expected) {
    }
}
Also used : ReadOnlyBufferException(java.nio.ReadOnlyBufferException) 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) ByteBuffer(java.nio.ByteBuffer) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 88 with BadPaddingException

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

the class BaseBlockCipher method engineDoFinal.

protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException {
    int len = 0;
    byte[] tmp = new byte[engineGetOutputSize(inputLen)];
    if (inputLen != 0) {
        len = cipher.processBytes(input, inputOffset, inputLen, tmp, 0);
    }
    try {
        len += cipher.doFinal(tmp, len);
    } catch (DataLengthException e) {
        throw new IllegalBlockSizeException(e.getMessage());
    } catch (InvalidCipherTextException e) {
        throw new BadPaddingException(e.getMessage());
    }
    if (len == tmp.length) {
        return tmp;
    }
    byte[] out = new byte[len];
    System.arraycopy(tmp, 0, out, 0, len);
    return out;
}
Also used : InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) DataLengthException(org.bouncycastle.crypto.DataLengthException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException)

Example 89 with BadPaddingException

use of javax.crypto.BadPaddingException in project cloudstack by apache.

the class CertServiceImpl method validateKeys.

private void validateKeys(final PublicKey pubKey, final PrivateKey privKey) {
    Preconditions.checkNotNull(pubKey);
    Preconditions.checkNotNull(privKey);
    if (!pubKey.getAlgorithm().equals(privKey.getAlgorithm())) {
        throw new IllegalArgumentException("Public and private key have different algorithms");
    }
    // No encryption for DSA
    if (pubKey.getAlgorithm() != "RSA") {
        return;
    }
    try {
        final String data = "ENCRYPT_DATA";
        final SecureRandom random = new SecureRandom();
        final Cipher cipher = Cipher.getInstance(pubKey.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, privKey, random);
        final byte[] encryptedData = cipher.doFinal(data.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, pubKey, random);
        final String decreptedData = new String(cipher.doFinal(encryptedData));
        if (!decreptedData.equals(data)) {
            throw new IllegalStateException("Bad public-private key");
        }
    } catch (final BadPaddingException | IllegalBlockSizeException | InvalidKeyException | NoSuchPaddingException e) {
        throw new IllegalStateException("Bad public-private key", e);
    } catch (final NoSuchAlgorithmException e) {
        throw new IllegalStateException("Invalid algorithm for public-private key", e);
    }
}
Also used : SecureRandom(java.security.SecureRandom) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) Cipher(javax.crypto.Cipher) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 90 with BadPaddingException

use of javax.crypto.BadPaddingException in project cloudstack by apache.

the class ConsoleProxyPasswordBasedEncryptor method decryptText.

public String decryptText(String encryptedText) {
    if (encryptedText == null || encryptedText.isEmpty())
        return encryptedText;
    try {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec keySpec = new SecretKeySpec(keyIvPair.getKeyBytes(), "AES");
        cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(keyIvPair.getIvBytes()));
        byte[] encryptedBytes = Base64.decodeBase64(encryptedText);
        return new String(cipher.doFinal(encryptedBytes));
    } catch (NoSuchAlgorithmException e) {
        s_logger.error("Unexpected exception ", e);
        return null;
    } catch (NoSuchPaddingException e) {
        s_logger.error("Unexpected exception ", e);
        return null;
    } catch (IllegalBlockSizeException e) {
        s_logger.error("Unexpected exception ", e);
        return null;
    } catch (BadPaddingException e) {
        s_logger.error("Unexpected exception ", e);
        return null;
    } catch (InvalidKeyException e) {
        s_logger.error("Unexpected exception ", e);
        return null;
    } catch (InvalidAlgorithmParameterException e) {
        s_logger.error("Unexpected exception ", e);
        return null;
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

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