Search in sources :

Example 1 with Cipher

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

use of javax.crypto.Cipher in project cas by apereo.

the class DefaultRegisteredServiceCipherExecutor method initializeCipherBasedOnServicePublicKey.

/**
     * Initialize cipher based on service public key.
     *
     * @param publicKey the public key
     * @param registeredService the registered service
     * @return the false if no public key is found
     * or if cipher cannot be initialized, etc.
     */
private Cipher initializeCipherBasedOnServicePublicKey(final PublicKey publicKey, final RegisteredService registeredService) {
    try {
        LOGGER.debug("Using public key [{}] to initialize the cipher", registeredService.getPublicKey());
        final Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        LOGGER.debug("Initialized cipher in encrypt-mode via the public key algorithm [{}]", publicKey.getAlgorithm());
        return cipher;
    } catch (final Exception e) {
        LOGGER.warn("Cipher could not be initialized for service [{}]. Error [{}]", registeredService, e.getMessage());
    }
    return null;
}
Also used : Cipher(javax.crypto.Cipher)

Example 3 with Cipher

use of javax.crypto.Cipher 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 4 with Cipher

use of javax.crypto.Cipher in project OpenAttestation by OpenAttestation.

the class HisPrivacyCAWebService2ImplTest method encryptRSA.

public static byte[] encryptRSA(byte[] text, PublicKey pubRSA) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA", "BC");
    cipher.init(Cipher.ENCRYPT_MODE, pubRSA);
    return cipher.doFinal(text);
}
Also used : Cipher(javax.crypto.Cipher)

Example 5 with Cipher

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

Aggregations

Cipher (javax.crypto.Cipher)1531 SecretKeySpec (javax.crypto.spec.SecretKeySpec)516 SecretKey (javax.crypto.SecretKey)377 IvParameterSpec (javax.crypto.spec.IvParameterSpec)368 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)349 InvalidKeyException (java.security.InvalidKeyException)257 IOException (java.io.IOException)202 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)202 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)196 BadPaddingException (javax.crypto.BadPaddingException)183 GeneralSecurityException (java.security.GeneralSecurityException)161 SecretKeyFactory (javax.crypto.SecretKeyFactory)149 Key (java.security.Key)148 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)134 SecureRandom (java.security.SecureRandom)105 PrivateKey (java.security.PrivateKey)93 PublicKey (java.security.PublicKey)86 UnsupportedEncodingException (java.io.UnsupportedEncodingException)84 PBEKeySpec (javax.crypto.spec.PBEKeySpec)82 KeyGenerator (javax.crypto.KeyGenerator)78