Search in sources :

Example 1 with IllegalBlockSizeException

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

Example 2 with IllegalBlockSizeException

use of javax.crypto.IllegalBlockSizeException in project remusic by aa112901.

the class AESTools method encrpty.

public static String encrpty(String paramString) {
    MessageDigest messageDigest = null;
    try {
        messageDigest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    messageDigest.update(INPUT.getBytes());
    byte[] stringBytes = messageDigest.digest();
    StringBuilder stringBuilder = new StringBuilder(stringBytes.length * 2);
    for (int i = 0; i < stringBytes.length; i++) {
        stringBuilder.append(CHARS[((stringBytes[i] & 0xF0) >>> 4)]);
        stringBuilder.append(CHARS[(stringBytes[i] & 0xF)]);
    }
    String str = stringBuilder.toString();
    SecretKeySpec localSecretKeySpec = new SecretKeySpec(str.substring(str.length() / 2).getBytes(), "AES");
    Cipher localCipher;
    try {
        localCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        localCipher.init(1, localSecretKeySpec, new IvParameterSpec(IV.getBytes()));
        return URLEncoder.encode(new String(BytesHandler.getChars(localCipher.doFinal(paramString.getBytes()))), "utf-8");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }
    return "";
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MessageDigest(java.security.MessageDigest)

Example 3 with IllegalBlockSizeException

use of javax.crypto.IllegalBlockSizeException in project remusic by aa112901.

the class Aes method decrypt.

private static byte[] decrypt(byte[] content, String password) {
    try {
        byte[] keyStr = getKey(password);
        SecretKeySpec key = new SecretKeySpec(keyStr, "AES");
        //algorithmStr
        Cipher cipher = Cipher.getInstance(algorithmStr);
        //   ΚΌ
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] result = cipher.doFinal(content);
        //
        return result;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) 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 4 with IllegalBlockSizeException

use of javax.crypto.IllegalBlockSizeException 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 5 with IllegalBlockSizeException

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

the class OpenSSLCipherRSA method engineDoFinal.

@Override
protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException {
    if (input != null) {
        engineUpdate(input, inputOffset, inputLen);
    }
    if (inputTooLarge) {
        throw new IllegalBlockSizeException("input must be under " + buffer.length + " bytes");
    }
    final byte[] tmpBuf;
    if (bufferOffset != buffer.length) {
        if (padding == NativeCrypto.RSA_NO_PADDING) {
            tmpBuf = new byte[buffer.length];
            System.arraycopy(buffer, 0, tmpBuf, buffer.length - bufferOffset, bufferOffset);
        } else {
            tmpBuf = Arrays.copyOf(buffer, bufferOffset);
        }
    } else {
        tmpBuf = buffer;
    }
    byte[] output = new byte[buffer.length];
    int resultSize;
    if (encrypting) {
        if (usingPrivateKey) {
            resultSize = NativeCrypto.RSA_private_encrypt(tmpBuf.length, tmpBuf, output, key.getPkeyContext(), padding);
        } else {
            resultSize = NativeCrypto.RSA_public_encrypt(tmpBuf.length, tmpBuf, output, key.getPkeyContext(), padding);
        }
    } else {
        try {
            if (usingPrivateKey) {
                resultSize = NativeCrypto.RSA_private_decrypt(tmpBuf.length, tmpBuf, output, key.getPkeyContext(), padding);
            } else {
                resultSize = NativeCrypto.RSA_public_decrypt(tmpBuf.length, tmpBuf, output, key.getPkeyContext(), padding);
            }
        } catch (SignatureException e) {
            IllegalBlockSizeException newE = new IllegalBlockSizeException();
            newE.initCause(e);
            throw newE;
        }
    }
    if (!encrypting && resultSize != output.length) {
        output = Arrays.copyOf(output, resultSize);
    }
    bufferOffset = 0;
    return output;
}
Also used : IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) SignatureException(java.security.SignatureException)

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