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:kr.co.exsoft.eframework.util.LicenseUtil.java

/**
 * ./*  ww w .j  av a  2  s  .com*/
 * 
 * @param word
 * @param key
 * @return String
 */
private static String unspell(String word, Key key) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.DECRYPT_MODE, key);

    byte[] ciphertext = getBytes(word);
    byte[] cleartext = cipher.doFinal(ciphertext);

    return new String(cleartext);
}

From source file:kr.co.exsoft.eframework.util.LicenseUtil.java

/**
 * ./*from  w  ww. j av a  2  s  .co m*/
 * 
 * @param word
 * @param key
 * @return String
 * @throws NoSuchPaddingException 
 * @throws NoSuchAlgorithmException 
 */
private static String spell(String word, Key key) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key);

    byte[] cleartext = word.getBytes();
    byte[] ciphertext = cipher.doFinal(cleartext);

    return getString(ciphertext);
}

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

public static String encrypt(Serializable object, byte[] aesKey) {
    SecretKeySpec secretKey = new SecretKeySpec(aesKey, ALGORITHM_SYM);
    String object_encrypted = null;
    try {//  w  w w. ja va  2s . co m
        final Cipher cipher = Cipher.getInstance(ALGORITHM_SYM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] data = SerializationUtils.serialize(object);
        byte[] cipherText = cipher.doFinal(data);
        object_encrypted = new BASE64Encoder().encode(cipherText);
    } catch (Exception e) {
        e.printStackTrace();
        return "bbb";
    }
    return object_encrypted;
}

From source file:com.fujitsu.dc.common.auth.token.LocalToken.java

/**
 * ??.// w  w w . jav  a 2 s  .c  o m
 * @param in 
 * @param ivBytes 
 * @return ???
 */
public static String encode(final String in, final byte[] ivBytes) {
    // IV??CELL?URL??????
    Cipher cipher;
    try {
        cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, aesKey, new IvParameterSpec(ivBytes));
        byte[] cipherBytes = cipher.doFinal(in.getBytes(CharEncoding.UTF_8));
        return DcCoreUtils.encodeBase64Url(cipherBytes);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

From source file:io.personium.common.auth.token.LocalToken.java

/**
 * ??./*from   ww  w  . j a  v  a 2 s  .  co  m*/
 * @param in 
 * @param ivBytes 
 * @return ???
 */
public static String encode(final String in, final byte[] ivBytes) {
    // IV??CELL?URL??????
    Cipher cipher;
    try {
        cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, aesKey, new IvParameterSpec(ivBytes));
        byte[] cipherBytes = cipher.doFinal(in.getBytes(CharEncoding.UTF_8));
        return PersoniumCoreUtils.encodeBase64Url(cipherBytes);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

From source file:com.jopss.logico.negocio.util.CriptoUtils.java

public static String desEncode(String texto, String chave) {
    Cipher ecipher;
    SecretKey key;/*from   w  w  w. j a  v a2  s  . co m*/
    String encod = null;
    try {
        key = new SecretKeySpec(chave.getBytes("UTF-8"), 0, 8, "DES");
        ecipher = Cipher.getInstance("DES");
        ecipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] utf8 = texto.getBytes("UTF8");
        byte[] crip = ecipher.doFinal(utf8);
        encod = new String(Hex.encodeHex(crip));

    } catch (Exception e) {
        log.error(e);
    }

    return encod;
}

From source file:com.jopss.logico.negocio.util.CriptoUtils.java

public static String desDecode(String texto, String chave) {
    Cipher dcipher;
    SecretKey key;//w w  w.ja  va 2 s  .  c  o  m
    String decod = null;

    try {
        key = new SecretKeySpec(chave.getBytes(), 0, 8, "DES");
        dcipher = Cipher.getInstance("DES");
        dcipher.init(Cipher.DECRYPT_MODE, key);
        byte[] dec = Hex.decodeHex(texto.toCharArray());
        byte[] utf8 = dcipher.doFinal(dec);
        decod = new String(utf8, "UTF8");
    } catch (Exception e) {
        log.error(e);
    }

    return decod;
}

From source file:com.cloudbees.plugins.credentials.SecretBytes.java

/**
 * Reverse operation of {@link #getEncryptedData()}. Returns null
 * if the given cipher text was invalid.
 *
 * @param data the bytes to decrypt./*from  w  w  w . j  a  va2  s . com*/
 * @return the secret bytes or {@code null} if the data was not originally encrypted.
 */
// copied from https://github.com/codehaus-plexus/plexus-cipher/blob/6ab0e38df80beed9ab3227ffab938b21dcdf5505/src
// /main/java/org/sonatype/plexus/components/cipher/PBECipher.java
@CheckForNull
public static SecretBytes decrypt(byte[] data) {
    if (data == null || data.length <= SALT_SIZE + 1) {
        return null;
    }
    try {
        int totalLen = data.length;
        byte[] salt = new byte[SALT_SIZE];
        System.arraycopy(data, 0, salt, 0, salt.length);
        byte padLen = data[salt.length];
        int len = totalLen - salt.length - 1 - (padLen & 0xff);
        if (len < 0) {
            return null;
        }
        byte[] encryptedBytes = new byte[len];
        System.arraycopy(data, salt.length + 1, encryptedBytes, 0, len);
        Cipher cipher = KEY.decrypt(salt);
        cipher.doFinal(encryptedBytes);
        return new SecretBytes(true, data);
    } catch (GeneralSecurityException e) {
        return null;
    }
}

From source file:com.lling.qiqu.utils.AesUtils.java

/**
 * Encrypts a message using a 128bit Bse64 key using AES.
 *
 * @param key     128bit Base64 AES key/*ww  w  .  j av a2 s . com*/
 * @param message Message to encrypt
 * @return hex encoded encrypted message
 */
public static String encrypt(String key, String message) {
    String encrypted = "";
    try {
        // Generate the secret key specs.
        byte[] keyArray = Base64.decodeBase64(key.getBytes());
        SecretKeySpec skeySpec = new SecretKeySpec(keyArray, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypt = cipher.doFinal(message.getBytes());
        encrypted = new String(hexCodec.encode(encrypt));
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RuntimeException(e);
    }
    return encrypted;
}

From source file:com.lling.qiqu.utils.AesUtils.java

/**
 * Encrypts a message using a 128bit Bse64 key using AES.
 *
 * @param key     128bit Base64 AES key/* w w w.  jav a  2s.c o m*/
 * @param message Message to encrypt
 * @return bse64 encoded encrypted message
 */
public static String encryptBase64(String key, String message) {
    String encrypted = "";
    try {
        // Generate the secret key specs.
        byte[] keyArray = Base64.decodeBase64(key.getBytes());
        SecretKeySpec skeySpec = new SecretKeySpec(keyArray, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypt = cipher.doFinal(message.getBytes());
        encrypted = new String(Base64.encodeBase64(encrypt));
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RuntimeException(e);
    }
    return encrypted;
}