Search in sources :

Example 26 with IllegalBlockSizeException

use of javax.crypto.IllegalBlockSizeException in project apn-proxy by apn-proxy.

the class TestAes method test.

@Test
public void test() {
    try {
        Key securekey = new SecretKeySpec("fuckgfw123456789".getBytes(), "AES");
        IvParameterSpec iv = new IvParameterSpec("1234567890123456".getBytes());
        Cipher c1 = Cipher.getInstance("AES/CFB/NoPadding");
        c1.init(Cipher.ENCRYPT_MODE, securekey, iv);
        byte[] raw = c1.doFinal(new byte[] { 1, 2, 3 });
        Cipher c2 = Cipher.getInstance("AES/CFB/NoPadding");
        c2.init(Cipher.DECRYPT_MODE, securekey, iv);
        byte[] orig = c2.doFinal(raw);
        byte[] orig2 = c2.doFinal(new byte[] { raw[0] });
        System.out.println(orig2);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    }
}
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) Key(java.security.Key) Test(org.junit.Test)

Example 27 with IllegalBlockSizeException

use of javax.crypto.IllegalBlockSizeException in project nhin-d by DirectProject.

the class AES method engineWrap.

/**
	 * {@inheritDoc}
	 */
@Override
protected byte[] engineWrap(Key key) throws IllegalBlockSizeException, InvalidKeyException {
    try {
        final Class<?> mechParamClazz = getClass().getClassLoader().loadClass(MECH_PARAM_CLAZZ_NAME);
        final Class<?> mechClazz = getClass().getClassLoader().loadClass(MECH_CLAZZ_NAME);
        final Constructor<?> mechConst = mechClazz.getConstructor(Integer.TYPE, mechParamClazz);
        final Object mech = mechConst.newInstance(4225, null);
        final Class<?> cryObjClazz = getClass().getClassLoader().loadClass(CRY_OBJECT_CLAZZ_NAME);
        final Method wrapKeyMeth = localSession.getClass().getMethod("wrapKey", mechClazz, cryObjClazz, cryObjClazz, byte[].class, Integer.TYPE);
        final Object wrapper = toCrptokiObject(this.cipherKey);
        final Object wrappee = toCrptokiObject(key);
        int size = (Integer) wrapKeyMeth.invoke(localSession, mech, wrapper, wrappee, null, 0);
        final byte[] wrapBuffer = new byte[size];
        wrapKeyMeth.invoke(localSession, mech, wrapper, wrappee, wrapBuffer, 0);
        return wrapBuffer;
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}
Also used : Method(java.lang.reflect.Method) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 28 with IllegalBlockSizeException

use of javax.crypto.IllegalBlockSizeException in project android_frameworks_base by DirtyUnicorns.

the class AndroidKeyStoreCipherSpiBase method engineWrap.

@Override
protected final byte[] engineWrap(Key key) throws IllegalBlockSizeException, InvalidKeyException {
    if (mKey == null) {
        throw new IllegalStateException("Not initilized");
    }
    if (!isEncrypting()) {
        throw new IllegalStateException("Cipher must be initialized in Cipher.WRAP_MODE to wrap keys");
    }
    if (key == null) {
        throw new NullPointerException("key == null");
    }
    byte[] encoded = null;
    if (key instanceof SecretKey) {
        if ("RAW".equalsIgnoreCase(key.getFormat())) {
            encoded = key.getEncoded();
        }
        if (encoded == null) {
            try {
                SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(key.getAlgorithm());
                SecretKeySpec spec = (SecretKeySpec) keyFactory.getKeySpec((SecretKey) key, SecretKeySpec.class);
                encoded = spec.getEncoded();
            } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
                throw new InvalidKeyException("Failed to wrap key because it does not export its key material", e);
            }
        }
    } else if (key instanceof PrivateKey) {
        if ("PKCS8".equalsIgnoreCase(key.getFormat())) {
            encoded = key.getEncoded();
        }
        if (encoded == null) {
            try {
                KeyFactory keyFactory = KeyFactory.getInstance(key.getAlgorithm());
                PKCS8EncodedKeySpec spec = keyFactory.getKeySpec(key, PKCS8EncodedKeySpec.class);
                encoded = spec.getEncoded();
            } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
                throw new InvalidKeyException("Failed to wrap key because it does not export its key material", e);
            }
        }
    } else if (key instanceof PublicKey) {
        if ("X.509".equalsIgnoreCase(key.getFormat())) {
            encoded = key.getEncoded();
        }
        if (encoded == null) {
            try {
                KeyFactory keyFactory = KeyFactory.getInstance(key.getAlgorithm());
                X509EncodedKeySpec spec = keyFactory.getKeySpec(key, X509EncodedKeySpec.class);
                encoded = spec.getEncoded();
            } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
                throw new InvalidKeyException("Failed to wrap key because it does not export its key material", e);
            }
        }
    } else {
        throw new InvalidKeyException("Unsupported key type: " + key.getClass().getName());
    }
    if (encoded == null) {
        throw new InvalidKeyException("Failed to wrap key because it does not export its key material");
    }
    try {
        return engineDoFinal(encoded, 0, encoded.length);
    } catch (BadPaddingException e) {
        throw (IllegalBlockSizeException) new IllegalBlockSizeException().initCause(e);
    }
}
Also used : PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 29 with IllegalBlockSizeException

use of javax.crypto.IllegalBlockSizeException in project android_frameworks_base by DirtyUnicorns.

the class LockSettingsService method verifyTiedProfileChallenge.

@Override
public VerifyCredentialResponse verifyTiedProfileChallenge(String password, boolean isPattern, long challenge, int userId) throws RemoteException {
    checkPasswordReadPermission(userId);
    if (!isManagedProfileWithUnifiedLock(userId)) {
        throw new RemoteException("User id must be managed profile with unified lock");
    }
    final int parentProfileId = mUserManager.getProfileParent(userId).id;
    // Unlock parent by using parent's challenge
    final VerifyCredentialResponse parentResponse = isPattern ? doVerifyPattern(password, true, challenge, parentProfileId, null) : doVerifyPassword(password, true, challenge, parentProfileId, null);
    if (parentResponse.getResponseCode() != VerifyCredentialResponse.RESPONSE_OK) {
        // Failed, just return parent's response
        return parentResponse;
    }
    try {
        // Unlock work profile, and work profile with unified lock must use password only
        return doVerifyPassword(getDecryptedPasswordForTiedProfile(userId), true, challenge, userId, null);
    } catch (UnrecoverableKeyException | InvalidKeyException | KeyStoreException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | CertificateException | IOException e) {
        Slog.e(TAG, "Failed to decrypt child profile key", e);
        throw new RemoteException("Unable to get tied profile token");
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) VerifyCredentialResponse(com.android.internal.widget.VerifyCredentialResponse) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) RemoteException(android.os.RemoteException)

Example 30 with IllegalBlockSizeException

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

the class TestNonexpanding method runTest.

public void runTest(String algo, String mo, String pad) throws Exception {
    Cipher ci = null;
    SecretKey key = null;
    try {
        // Initialization
        Random rdm = new Random();
        byte[] plainText = new byte[128];
        rdm.nextBytes(plainText);
        ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
        KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
        kg.init(KEY_LENGTH);
        key = kg.generateKey();
        // encrypt
        ci.init(Cipher.ENCRYPT_MODE, key);
        byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
        int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
        ci.doFinal(cipherText, offset);
        // Comparison
        if (!(plainText.length == cipherText.length)) {
            // authentication tag and cipher text.
            if (mo.equalsIgnoreCase("GCM")) {
                GCMParameterSpec spec = ci.getParameters().getParameterSpec(GCMParameterSpec.class);
                int cipherTextLength = cipherText.length - spec.getTLen() / 8;
                if (plainText.length == cipherTextLength) {
                    return;
                }
            }
            System.out.println("Original length: " + plainText.length);
            System.out.println("Cipher text length: " + cipherText.length);
            throw new RuntimeException("Test failed!");
        }
    } catch (NoSuchAlgorithmException e) {
        //CFB7 and OFB150 are for negative testing
        if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("OFB150")) {
            System.out.println("Unexpected NoSuchAlgorithmException with mode: " + mo);
            throw new RuntimeException("Test failed!");
        }
    } catch (NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | InvalidParameterSpecException | ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
        System.out.println("Test failed!");
        throw e;
    }
}
Also used : NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKey(javax.crypto.SecretKey) Random(java.util.Random) ShortBufferException(javax.crypto.ShortBufferException) Cipher(javax.crypto.Cipher) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) NoSuchProviderException(java.security.NoSuchProviderException) KeyGenerator(javax.crypto.KeyGenerator)

Aggregations

IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)110 BadPaddingException (javax.crypto.BadPaddingException)95 InvalidKeyException (java.security.InvalidKeyException)77 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)66 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)65 Cipher (javax.crypto.Cipher)54 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)45 IOException (java.io.IOException)38 SecretKey (javax.crypto.SecretKey)26 IvParameterSpec (javax.crypto.spec.IvParameterSpec)26 UnrecoverableKeyException (java.security.UnrecoverableKeyException)25 CertificateException (java.security.cert.CertificateException)25 KeyStoreException (java.security.KeyStoreException)24 SecretKeySpec (javax.crypto.spec.SecretKeySpec)23 RemoteException (android.os.RemoteException)15 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)15 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)13 KeyGenerator (javax.crypto.KeyGenerator)13 ShortBufferException (javax.crypto.ShortBufferException)13 UnsupportedEncodingException (java.io.UnsupportedEncodingException)11