Search in sources :

Example 36 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project MTweaks-KernelAdiutorMOD by morogoku.

the class SecurityActivity method loadFingerprint.

private void loadFingerprint() {
    try {
        KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
        KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
        mCipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
        keyStore.load(null);
        keyGenerator.init(new KeyGenParameterSpec.Builder(KEY_NAME, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT).setBlockModes(KeyProperties.BLOCK_MODE_CBC).setUserAuthenticationRequired(true).setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7).build());
        keyGenerator.generateKey();
        SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME, null);
        mCipher.init(Cipher.ENCRYPT_MODE, key);
    } catch (KeyStoreException | NoSuchProviderException | NoSuchAlgorithmException | NoSuchPaddingException | UnrecoverableKeyException | InvalidKeyException | CertificateException | InvalidAlgorithmParameterException | IOException e) {
        return;
    }
    mCryptoObject = new FingerprintManagerCompat.CryptoObject(mCipher);
    FrameLayout fingerprintParent = (FrameLayout) findViewById(R.id.fingerprint_parent);
    final SwirlView swirlView = new SwirlView(new ContextThemeWrapper(this, R.style.Swirl));
    swirlView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    fingerprintParent.addView(swirlView);
    fingerprintParent.setVisibility(View.VISIBLE);
    mFingerprintUiHelper = new FingerprintUiHelper.FingerprintUiHelperBuilder(mFingerprintManagerCompat).build(swirlView, new FingerprintUiHelper.Callback() {

        @Override
        public void onAuthenticated() {
            try {
                mCipher.doFinal(SECRET_MESSAGE.getBytes());
                mPasswordWrong.setVisibility(View.GONE);
                setResult(1);
                finish();
            } catch (IllegalBlockSizeException | BadPaddingException e) {
                e.printStackTrace();
                swirlView.setState(SwirlView.State.ERROR);
            }
        }

        @Override
        public void onError() {
        }
    });
    mFingerprintUiHelper.startListening(mCryptoObject);
}
Also used : FingerprintManagerCompat(android.support.v4.hardware.fingerprint.FingerprintManagerCompat) SwirlView(com.mattprecious.swirl.SwirlView) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) KeyGenerator(javax.crypto.KeyGenerator) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyGenParameterSpec(android.security.keystore.KeyGenParameterSpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) KeyStore(java.security.KeyStore) SecretKey(javax.crypto.SecretKey) ContextThemeWrapper(android.support.v7.view.ContextThemeWrapper) FrameLayout(android.widget.FrameLayout) NoSuchProviderException(java.security.NoSuchProviderException)

Example 37 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project data-transfer-project by google.

the class DecrypterImpl method decrypt.

@Override
public String decrypt(String encrypted) {
    try {
        byte[] decoded = BaseEncoding.base64Url().decode(encrypted);
        Cipher cipher = Cipher.getInstance(transformation);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decrypted = cipher.doFinal(decoded);
        if (decrypted == null || decrypted.length <= 8) {
            throw new RuntimeException("incorrect decrypted text.");
        }
        byte[] data = new byte[decrypted.length - 8];
        System.arraycopy(decrypted, 8, data, 0, data.length);
        return new String(data, Charsets.UTF_8);
    } catch (BadPaddingException | IllegalBlockSizeException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e) {
        logger.error("Error decrypting data, length: {}", encrypted.length(), e);
        throw new RuntimeException(e);
    }
}
Also used : 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 38 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project data-transfer-project by google.

the class EncrypterImpl method encrypt.

@Override
public String encrypt(String data) {
    try {
        Cipher cipher = Cipher.getInstance(transformation);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] salt = new byte[8];
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.nextBytes(salt);
        cipher.update(salt);
        byte[] encrypted = cipher.doFinal(data.getBytes(Charsets.UTF_8));
        return BaseEncoding.base64Url().encode(encrypted);
    } catch (BadPaddingException | IllegalBlockSizeException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e) {
        logger.error("Exception encrypting data, length: {}", data.length(), e);
        throw new RuntimeException(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 39 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project pdfbox by apache.

the class SecurityHandler method encryptDataAESother.

/**
 * Encrypt or decrypt data with AES with key length other than 256 bits.
 *
 * @param finalKey The final key obtained with via {@link #calcFinalKey(long, long)}.
 * @param data The data to encrypt.
 * @param output The output to write the encrypted data to.
 * @param decrypt true to decrypt the data, false to encrypt it.
 *
 * @throws IOException If there is an error reading the data.
 */
private void encryptDataAESother(byte[] finalKey, InputStream data, OutputStream output, boolean decrypt) throws IOException {
    byte[] iv = new byte[16];
    if (!prepareAESInitializationVector(decrypt, iv, data, output)) {
        return;
    }
    try {
        Cipher decryptCipher;
        try {
            decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        } catch (NoSuchAlgorithmException e) {
            // should never happen
            throw new RuntimeException(e);
        }
        SecretKey aesKey = new SecretKeySpec(finalKey, "AES");
        IvParameterSpec ips = new IvParameterSpec(iv);
        decryptCipher.init(decrypt ? Cipher.DECRYPT_MODE : Cipher.ENCRYPT_MODE, aesKey, ips);
        byte[] buffer = new byte[256];
        int n;
        while ((n = data.read(buffer)) != -1) {
            byte[] dst = decryptCipher.update(buffer, 0, n);
            if (dst != null) {
                output.write(dst);
            }
        }
        output.write(decryptCipher.doFinal());
    } catch (InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
        throw new IOException(e);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher)

Example 40 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project Signal-Android by signalapp.

the class KeyStoreHelper method seal.

@RequiresApi(Build.VERSION_CODES.M)
public static SealedData seal(@NonNull byte[] input) {
    SecretKey secretKey = getOrCreateKeyStoreEntry();
    try {
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] iv = cipher.getIV();
        byte[] data = cipher.doFinal(input);
        return new SealedData(iv, data);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
        throw new AssertionError(e);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) RequiresApi(android.support.annotation.RequiresApi)

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