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:com.iterzp.momo.utils.RSAUtils.java

/**
 * //from w w  w.  j  a  va2s . co  m
 * 
 * @param privateKey
 *            ?
 * @param data
 *            ?
 * @return ??
 */
public static byte[] decrypt(PrivateKey privateKey, byte[] data) {
    Assert.notNull(privateKey);
    Assert.notNull(data);
    try {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", PROVIDER);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(data);
    } catch (Exception e) {
        return null;
    }
}

From source file:com.example.license.RSAUtil.java

/**
 * ?/*  ww w .  j  ava 2  s  . c  o  m*/
 * 
 * @param data
 *            ?
 * @param key
 *            
 * @return ??
 */
public static String decrypt(String data, String seed) throws Exception {
    KeyPair keyPair = generatorKeyPair(seed);
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    // ?Cipher?
    cipher.init(Cipher.DECRYPT_MODE, keyPair.getPublic());
    // ?
    return new String(cipher.doFinal(Base64.decodeBase64(data)));
}

From source file:com.formatAdmin.utils.UtilsSecurity.java

public static String cifrarMD5(String texto) {
    String base64EncryptedString = "";
    try {/* w w w.  j a v a 2 s  .c o  m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(SECRET_MD5_KEY.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

        SecretKey key = new SecretKeySpec(keyBytes, ENCRYPT_ALGORITHM);
        Cipher cipher = Cipher.getInstance(ENCRYPT_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] plainTextBytes = texto.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        base64EncryptedString = new String(base64Bytes);

    } catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException | BadPaddingException
            | IllegalBlockSizeException | NoSuchPaddingException ex) {
    }
    return base64EncryptedString;
}

From source file:com.ec2box.manage.util.EncryptionUtil.java

/**
 * return encrypted value of string/* ww  w.j  a  v  a  2  s . c om*/
 *
 * @param str unencrypted string
 * @return encrypted string
 */
public static String encrypt(String str) {

    String retVal = null;
    if (str != null && str.length() > 0) {
        try {
            Cipher c = Cipher.getInstance("AES");
            c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"));
            byte[] encVal = c.doFinal(str.getBytes());
            retVal = new String(Base64.encodeBase64(encVal));
        } catch (Exception ex) {
            log.error(ex.toString(), ex);
        }

    }
    return retVal;
}

From source file:com.ikon.util.SecureStore.java

/**
 * DES encoder//www .  jav a2s  .c om
 */
public static byte[] desEncode(String key, byte[] src)
        throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException,
        InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
    DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey sKey = keyFactory.generateSecret(keySpec);

    Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe
    cipher.init(Cipher.ENCRYPT_MODE, sKey);
    byte[] dst = cipher.doFinal(src);

    return dst;
}

From source file:com.ikon.util.SecureStore.java

/**
 * DES decoder/*from  ww w . j a  v a2s.co m*/
 */
public static byte[] desDecode(String key, byte[] src)
        throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException,
        InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
    DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey sKey = keyFactory.generateSecret(keySpec);

    Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe
    cipher.init(Cipher.DECRYPT_MODE, sKey);
    byte[] dst = cipher.doFinal(src);

    return dst;
}

From source file:net.ftb.util.CryptoUtils.java

/**
 * Method to AES encrypt string if fails, will attempt to use {@link #encryptLegacy(String str, byte[] key)}
 * @param str string to encrypt//from   w  w  w  .  j a v a 2  s .  co  m
 * @param key encryption key
 * @return encrypted string or "" if legacy fails
 */
public static String encrypt(String str, byte[] key) {
    try {
        Cipher aes = Cipher.getInstance("AES");
        aes.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(pad(key), "AES"));
        return Base64.encodeBase64String(aes.doFinal(("FDT:" + str).getBytes("utf8")));
    } catch (Exception e) {
        Logger.logError("Error Encrypting information, reverting to legacy format", e);
        return encryptLegacy(str, key);
    }
}

From source file:com.almende.util.EncryptionUtil.java

/**
 * Decrypt an encrypted string.//w  w  w . j  a  va  2 s .  c o m
 * 
 * @param encryptedText
 *            the encrypted text
 * @return text
 * @throws InvalidKeyException
 *             the invalid key exception
 * @throws InvalidAlgorithmParameterException
 *             the invalid algorithm parameter exception
 * @throws NoSuchAlgorithmException
 *             the no such algorithm exception
 * @throws InvalidKeySpecException
 *             the invalid key spec exception
 * @throws NoSuchPaddingException
 *             the no such padding exception
 * @throws IllegalBlockSizeException
 *             the illegal block size exception
 * @throws BadPaddingException
 *             the bad padding exception
 * @throws UnsupportedEncodingException
 *             the unsupported encoding exception
 */
public static String decrypt(final String encryptedText) throws InvalidKeyException,
        InvalidAlgorithmParameterException, NoSuchAlgorithmException, InvalidKeySpecException,
        NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
    final PBEParameterSpec pbeParamSpec = new PBEParameterSpec(S, C);
    final PBEKeySpec pbeKeySpec = new PBEKeySpec(P);
    final SecretKeyFactory keyFac = SecretKeyFactory.getInstance(ENC);
    final SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

    final Cipher pbeCipher = Cipher.getInstance(ENC);
    pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);

    final byte[] text = pbeCipher.doFinal(Base64.decodeBase64(encryptedText));
    return new String(text, "UTF-8").intern();
}

From source file:de.dennishoersch.web.chat.Encryption.java

private static String decryptRSA(Key key, byte[] input) {
    try {/*from   w ww  . jav  a  2s .c o  m*/
        Cipher rsa;
        rsa = Cipher.getInstance("RSA");
        rsa.init(Cipher.DECRYPT_MODE, key);
        byte[] utf8 = rsa.doFinal(input);
        return new String(utf8, "UTF8");
    } catch (Exception e) {
        throw Throwables.throwUnchecked(e);
    }
}

From source file:encryptdecrypt.util.Security.java

public static String encrypt(String strToEncrypt) {
    try {//from  w  ww. j  av  a2s  .c  om
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return new String(Base64.encodeBase64(cipher.doFinal(strToEncrypt.getBytes())));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}