Search in sources :

Example 36 with BadPaddingException

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

Example 37 with BadPaddingException

use of javax.crypto.BadPaddingException in project jgnash by ccavanaugh.

the class EncryptionManager method encrypt.

/**
     * Encrypts the supplied string.
     *
     * @param plain String to encrypt
     * @return the encrypted string
     */
public String encrypt(final String plain) {
    try {
        final Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return printBase64Binary(cipher.doFinal(plain.getBytes(StandardCharsets.UTF_8)));
    } catch (final InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException e) {
        logger.log(Level.SEVERE, e.getMessage(), e);
    }
    return null;
}
Also used : NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Example 38 with BadPaddingException

use of javax.crypto.BadPaddingException in project jgnash by ccavanaugh.

the class EncryptionManager method decrypt.

/**
     * Decrypts the supplied string.
     *
     * @param encrypted String to decrypt
     * @return The decrypted string of {@code DECRYPTION_ERROR_TAG} if decryption fails
     * @see #DECRYPTION_ERROR_TAG
     */
public String decrypt(final String encrypted) {
    try {
        final Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        return new String(cipher.doFinal(parseBase64Binary(encrypted)), StandardCharsets.UTF_8);
    } catch (final InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException e) {
        logger.log(Level.SEVERE, "Invalid password");
        return DECRYPTION_ERROR_TAG;
    }
}
Also used : NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Example 39 with BadPaddingException

use of javax.crypto.BadPaddingException in project jdk8u_jdk by JetBrains.

the class RSAPadding method unpadOAEP.

/**
     * PKCS#1 v2.1 OAEP unpadding (MGF1).
     */
private byte[] unpadOAEP(byte[] padded) throws BadPaddingException {
    byte[] EM = padded;
    boolean bp = false;
    int hLen = lHash.length;
    if (EM[0] != 0) {
        bp = true;
    }
    int seedStart = 1;
    int seedLen = hLen;
    int dbStart = hLen + 1;
    int dbLen = EM.length - dbStart;
    mgf1(EM, dbStart, dbLen, EM, seedStart, seedLen);
    mgf1(EM, seedStart, seedLen, EM, dbStart, dbLen);
    // verify lHash == lHash'
    for (int i = 0; i < hLen; i++) {
        if (lHash[i] != EM[dbStart + i]) {
            bp = true;
        }
    }
    int padStart = dbStart + hLen;
    int onePos = -1;
    for (int i = padStart; i < EM.length; i++) {
        int value = EM[i];
        if (onePos == -1) {
            if (value == 0x00) {
            // continue;
            } else if (value == 0x01) {
                onePos = i;
            } else {
                // Anything other than {0,1} is bad.
                bp = true;
            }
        }
    }
    // We either ran off the rails or found something other than 0/1.
    if (onePos == -1) {
        bp = true;
        // Don't inadvertently return any data.
        onePos = EM.length - 1;
    }
    int mStart = onePos + 1;
    // copy useless padding array for a constant-time method
    byte[] tmp = new byte[mStart - padStart];
    System.arraycopy(EM, padStart, tmp, 0, tmp.length);
    byte[] m = new byte[EM.length - mStart];
    System.arraycopy(EM, mStart, m, 0, m.length);
    BadPaddingException bpe = new BadPaddingException("Decryption error");
    if (bp) {
        throw bpe;
    } else {
        return m;
    }
}
Also used : BadPaddingException(javax.crypto.BadPaddingException)

Example 40 with BadPaddingException

use of javax.crypto.BadPaddingException in project jdk8u_jdk by JetBrains.

the class RSAPadding method mgf1.

/**
     * Compute MGF1 using mgfMD as the message digest.
     * Note that we combine MGF1 with the XOR operation to reduce data
     * copying.
     *
     * We generate maskLen bytes of MGF1 from the seed and XOR it into
     * out[] starting at outOfs;
     */
private void mgf1(byte[] seed, int seedOfs, int seedLen, byte[] out, int outOfs, int maskLen) throws BadPaddingException {
    // 32 bit counter
    byte[] C = new byte[4];
    byte[] digest = new byte[mgfMd.getDigestLength()];
    while (maskLen > 0) {
        mgfMd.update(seed, seedOfs, seedLen);
        mgfMd.update(C);
        try {
            mgfMd.digest(digest, 0, digest.length);
        } catch (DigestException e) {
            // should never happen
            throw new BadPaddingException(e.toString());
        }
        for (int i = 0; (i < digest.length) && (maskLen > 0); maskLen--) {
            out[outOfs++] ^= digest[i++];
        }
        if (maskLen > 0) {
            // increment counter
            for (int i = C.length - 1; (++C[i] == 0) && (i > 0); i--) {
            // empty
            }
        }
    }
}
Also used : 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