Example usage for javax.crypto Cipher doFinal

List of usage examples for javax.crypto Cipher doFinal

Introduction

In this page you can find the example usage for javax.crypto Cipher doFinal.

Prototype

public final byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException 

Source Link

Document

Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.

Usage

From source file:club.jmint.crossing.specs.Security.java

public static String desEncrypt(String data, String key) throws CrossException {
    String ret = null;//from   w  w w .j a v a2  s.  c  om
    try {
        DESKeySpec desKey = new DESKeySpec(key.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(desKey);

        Cipher cipher = Cipher.getInstance(CIPHER_DES_ALGORITHM);
        SecureRandom random = new SecureRandom();
        cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
        byte[] results = cipher.doFinal(data.getBytes("UTF-8"));
        ret = Base64.encodeBase64String(results);
    } catch (Exception e) {
        CrossLog.printStackTrace(e);
        throw new CrossException(ErrorCode.COMMON_ERR_ENCRYPTION.getCode(),
                ErrorCode.COMMON_ERR_ENCRYPTION.getInfo());
    }
    return ret;
}

From source file:com.hhi.bigdata.platform.push.client.RegisterUtil.java

/**
 * <pre>// w  w w  .j  a va2 s .co  m
 * private key ? app name? ? ? return
 * </pre>
 * @return
 * @throws Exception
 */
private static String getAppNameEnc(PrivateKey privKey, String appName) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, privKey);

    return Base64.encodeBase64String(cipher.doFinal(appName.getBytes()));
}

From source file:com.app.utils.StringEncrypt.java

/**
 * Funcin de tipo String que recibe una llave (key), un vector de inicializacin (iv)
 * y el texto que se desea cifrar/*from  www.ja v a 2s .  c  o  m*/
 * @param key la llave en tipo String a utilizar
 * @param iv el vector de inicializacin a utilizar
 * @param cleartext el texto sin cifrar a encriptar
 * @return el texto cifrado en modo String
 * @throws Exception puede devolver excepciones de los siguientes tipos: NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException
 */
public static String encrypt(String key, String iv, String cleartext) throws Exception {
    Cipher cipher = Cipher.getInstance(cI);
    SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
    byte[] encrypted = cipher.doFinal(cleartext.getBytes());
    return new String(encodeBase64(encrypted));
}

From source file:com.servoy.j2db.util.SecuritySupport.java

@SuppressWarnings("nls")
public static String encrypt(Settings settings, String value) throws Exception {
    if (value == null)
        return value;
    Cipher cipher = Cipher.getInstance("DESede");
    cipher.init(Cipher.ENCRYPT_MODE, SecuritySupport.getCryptKey(settings));
    return Utils.encodeBASE64(cipher.doFinal(value.getBytes()));
}

From source file:io.stallion.utils.Encrypter.java

private static String doDecryptString(String password, String encryptedBase32) throws Exception {
    encryptedBase32 = StringUtils.strip(encryptedBase32, "=");
    String salt = encryptedBase32.substring(0, 16);
    String ivString = encryptedBase32.substring(16, 48);
    byte[] iv = new byte[0];
    try {/*w ww.  ja  v  a2  s. c o m*/
        iv = Hex.decodeHex(ivString.toCharArray());
    } catch (DecoderException e) {
        throw new RuntimeException(e);
    }
    encryptedBase32 = encryptedBase32.substring(48);
    Base32 decoder = new Base32();
    byte[] encrypted = decoder.decode(encryptedBase32.toUpperCase());
    SecretKeySpec skeySpec = makeKeySpec(password, salt);

    /*
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec,
            new IvParameterSpec(iv));
      */
    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, new GCMParameterSpec(128, iv));

    byte[] original = cipher.doFinal(encrypted);
    return new String(original, Charset.forName("UTF-8"));

}

From source file:br.com.ufjf.labredes.crypto.Cryptography.java

public static Object decrypt(String encryptedObject, byte[] aesKey) {
    Object object = null;//from   w w w  .  j ava 2s .  com
    SecretKeySpec secretKey = new SecretKeySpec(aesKey, ALGORITHM_SYM);
    try {
        byte[] cipherText = new BASE64Decoder().decodeBuffer(encryptedObject);
        Cipher cipher = Cipher.getInstance(ALGORITHM_SYM);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] serialized_obj = cipher.doFinal(cipherText);
        object = (Object) SerializationUtils.deserialize(serialized_obj);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return object;
}

From source file:com.imaginary.home.cloud.Configuration.java

static public @Nonnull String encrypt(@Nonnull String keySalt, @Nonnull String value) {
    try {/*from  w ww  .j a va2  s  . co  m*/
        SecretKeySpec spec = new SecretKeySpec(getConfiguration().getCustomSalt(keySalt), "AES");
        Cipher cipher = Cipher.getInstance("AES");

        cipher.init(Cipher.ENCRYPT_MODE, spec);

        byte[] raw = value.getBytes("utf-8");
        byte[] encrypted = cipher.doFinal(raw);
        byte[] b64 = Base64.encodeBase64(encrypted);
        return new String(b64, "utf-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.netcrest.pado.internal.security.AESCipher.java

private static byte[] encryptBinaryToBinary(byte[] privateKeyEncrypted, byte[] clearBinary) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(privateKeyEncrypted, "AES");

    // Instantiate the cipher
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] encrypted = cipher.doFinal(clearBinary);
    return encrypted;
}

From source file:com._64bitlabs.util.Encryptors.java

/**
 * Given AES encryption algorithm, decrypts the string value.
 * @param encryptedData/*from w w  w  .j  a v a 2 s  .c  o  m*/
 * @return decrypted String
 */
public static String decrypt(String encryptedData) {
    Key key = generateKey();
    try {
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.DECRYPT_MODE, key);

        Base64 dec = new Base64();
        byte[] decodedValue = dec.decode(encryptedData.getBytes());
        byte[] decValue = c.doFinal(decodedValue);

        return new String(decValue);
    } catch (Exception e) {
        return null;
    }
}

From source file:ec.edu.uce.medicina.seguimiento.util.EncryptionUtility.java

/**
 *Mtodo que permite la encriptacin de la contrasea.
 * @param cleartext/*  w w w  .  j  ava2 s. co m*/
 * @return
 * @throws java.lang.Exception
 */
public static String encrypt(String cleartext) throws Exception {
    String key = "02AE31B79CCCB2A3"; //llave
    String iv = "0123456789ABCDEF";
    Cipher cipher = Cipher.getInstance(cI);
    SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
    byte[] encrypted = cipher.doFinal(cleartext.getBytes());
    return new String(encodeBase64(encrypted));
}