Search in sources :

Example 1 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project zeppelin by apache.

the class Authentication method decrypt.

private String decrypt(String value, String initVector) {
    LOG.debug("IV is {}, IV length is {}", initVector, initVector.length());
    if (StringUtils.isBlank(value) || StringUtils.isBlank(initVector)) {
        LOG.error("String to decode or salt is not provided");
        return StringUtils.EMPTY;
    }
    try {
        IvParameterSpec iv = generateIV(initVector);
        Key key = generateKey();
        Cipher cipher = Cipher.getInstance(CIPHER_MODE);
        cipher.init(Cipher.DECRYPT_MODE, key, iv);
        byte[] decryptedString = Base64.decodeBase64(toBytes(value));
        decryptedString = cipher.doFinal(decryptedString);
        return new String(decryptedString);
    } catch (GeneralSecurityException e) {
        LOG.error("Error when decrypting", e);
        return StringUtils.EMPTY;
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) Key(java.security.Key)

Example 2 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project UltimateAndroid by cymcsg.

the class TripleDES method decrypt.

/**
     * Decrypt the message with TripleDES
     *
     * @param message
     * @return
     * @throws Exception
     */
public static String decrypt(String message) throws Exception {
    if (message == null || message == "")
        return "";
    byte[] values = Base64decoding(message, 0);
    final MessageDigest md = MessageDigest.getInstance("SHA-1");
    final byte[] digestOfPassword = md.digest(token.getBytes("utf-8"));
    final byte[] keyBytes = copyOf(digestOfPassword, 24);
    for (int j = 0, k = 16; j < 8; ) {
        keyBytes[k++] = keyBytes[j++];
    }
    final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
    String s1 = "12345678";
    byte[] bytes = s1.getBytes();
    final IvParameterSpec iv = new IvParameterSpec(bytes);
    final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    decipher.init(Cipher.DECRYPT_MODE, key, iv);
    final byte[] plainText = decipher.doFinal(values);
    return new String(plainText, "UTF-8");
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MessageDigest(java.security.MessageDigest)

Example 3 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project Openfire by igniterealtime.

the class K method A.

byte[] A(byte[] abyte0, int i, int j) {
    if (C == null)
        return abyte0;
    try {
        byte[] abyte1 = new byte[16];
        IvParameterSpec ivparameterspec = new IvParameterSpec(abyte1);
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        if (E == Encryption.ENCRYPT)
            cipher.init(1, C, ivparameterspec);
        if (E == Encryption.DECRYPT)
            cipher.init(2, C, ivparameterspec);
        return cipher.doFinal(abyte0, i, j);
    } catch (NoSuchAlgorithmException nosuchalgorithmexception) {
        D.error(nosuchalgorithmexception.getMessage(), nosuchalgorithmexception);
    } catch (NoSuchPaddingException nosuchpaddingexception) {
        D.error(nosuchpaddingexception.getMessage(), nosuchpaddingexception);
    } catch (InvalidKeyException invalidkeyexception) {
        D.error(invalidkeyexception.getMessage(), invalidkeyexception);
    } catch (BadPaddingException badpaddingexception) {
        D.error(badpaddingexception.getMessage(), badpaddingexception);
    } catch (IllegalBlockSizeException illegalblocksizeexception) {
        D.error(illegalblocksizeexception.getMessage(), illegalblocksizeexception);
    } catch (InvalidAlgorithmParameterException invalidalgorithmparameterexception) {
        D.error(invalidalgorithmparameterexception.getMessage(), invalidalgorithmparameterexception);
    }
    return null;
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec)

Example 4 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project Signal-Android by WhisperSystems.

the class DecryptingPartInputStream method initializeCipher.

private Cipher initializeCipher(SecretKeySpec key) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, IOException {
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec iv = readIv(cipher.getBlockSize());
    cipher.init(Cipher.DECRYPT_MODE, key, iv);
    return cipher;
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher)

Example 5 with IvParameterSpec

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

Aggregations

IvParameterSpec (javax.crypto.spec.IvParameterSpec)509 Cipher (javax.crypto.Cipher)369 SecretKeySpec (javax.crypto.spec.SecretKeySpec)268 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)112 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)112 InvalidKeyException (java.security.InvalidKeyException)104 SecretKey (javax.crypto.SecretKey)91 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)69 BadPaddingException (javax.crypto.BadPaddingException)68 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)68 IOException (java.io.IOException)63 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)60 GeneralSecurityException (java.security.GeneralSecurityException)52 Key (java.security.Key)36 SecureRandom (java.security.SecureRandom)34 UnsupportedEncodingException (java.io.UnsupportedEncodingException)26 SecretKeyFactory (javax.crypto.SecretKeyFactory)26 KeyGenerator (javax.crypto.KeyGenerator)25 MessageDigest (java.security.MessageDigest)22 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)21