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:cn.lynx.emi.license.ViewLicense.java

private static final String _decrypt(String data) {
    byte[] corekey = Base64.decodeBase64(LICENSE_CORE_KEY);
    byte[] rawData = Base64.decodeBase64(data);

    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(corekey);
    try {//from   w  ww  . ja v  a 2s  .co  m
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        Key publicKey = keyFactory.generatePublic(x509EncodedKeySpec);

        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, publicKey);

        return new String(cipher.doFinal(rawData), "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.netsteadfast.greenstep.util.EncryptorUtils.java

public static String encrypt(String key1, String iv1, String value) {
    try {//  w  w  w . j  a  va  2s .  c  o  m
        IvParameterSpec iv = new IvParameterSpec(iv1.getBytes(Constants.BASE_ENCODING));
        SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes(Constants.BASE_ENCODING), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(value.getBytes());
        //System.out.println("encrypted string:" + Base64.encodeBase64String(encrypted));
        return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:com.lecaddyfute.utils.security.AESCrypto.java

public static String encrypt1(String Data) throws Exception {
    KeyGenerator keyGen = KeyGenerator.getInstance(ALGO);
    keyGen.init(128);/*from   www  .  ja v a  2  s .  c om*/
    SecretKey key = keyGen.generateKey();
    System.out.println("Generated key: " + key);
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data.getBytes("UTF8"));

    String encryptedValue = ConvertionHelper.bytesToHex(encVal);
    //        String encryptedValue = new String(Base64.encodeBase64(encVal));
    return encryptedValue;
}

From source file:com.bytecode.util.Crypto.java

private static Key generateKey(String keystring, int bits) throws Exception {
    byte[] keyBytes = new byte[bits];
    byte[] key = new byte[bits];
    for (int i = 0; i < bits; i++) {
        keyBytes[i] = (byte) keystring.codePointAt(i);
    }//from w ww. j a  va  2  s .com
    SecretKey secretKey = new SecretKeySpec(keyBytes, "AES");
    Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    key = cipher.doFinal(keyBytes);
    for (int i = 0; i < bits - 16; i++) {
        key[16 + i] = key[i];
    }

    return new SecretKeySpec(key, "AES");
}

From source file:Main.java

private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
    byte[] decrypted = cipher.doFinal(encrypted);
    return decrypted;
}

From source file:com.zf.decipher.DataEn.java

private static byte[] encrypt(byte[] data) throws Exception {
    if (null == secretKey) {
        throw new SecurityException("secretKey is null");
    }// w  w w  .  ja  v a2  s .co  m
    Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    return cipher.doFinal(data);
}

From source file:com.zf.decipher.DataEn.java

private static byte[] decrypt(byte[] data) throws Exception {
    if (null == secretKey) {
        throw new SecurityException("secretKey is null");
    }/*from   w  ww .  j  av  a2s.  c o  m*/
    Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    return cipher.doFinal(data);
}

From source file:com.sds.acube.ndisc.xnapi.XNApiDesCipher.java

/**
 * //from w w w .  jav a  2  s .c  om
 * 
 * @param data  ?
 * @return  ?
 */
public static String decrypt(String data) {
    try {
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, getKey());
        byte d[] = cipher.doFinal(Base64.decodeBase64(data.getBytes()));
        return new String(d);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.sds.acube.ndisc.xnapi.XNApiDesCipher.java

/**
 * //from   w w  w.  j  a va 2  s  . c  o m
 * 
 * @param data  ?
 * @return  ?
 */
public static String encrypt(String data) {
    try {
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, getKey());
        byte e[] = cipher.doFinal(data.getBytes());
        return Base64.encodeBase64String(e);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String decode(String key, String ciphertext) throws Exception {
    byte[] bs = parseHexStr2Byte(ciphertext);
    IvParameterSpec ivSpec = new IvParameterSpec(HEX.getBytes());
    SecretKeySpec secretKeySpec = createKey(key);
    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, secretKeySpec, ivSpec);
    return new String(c.doFinal(bs), "UTF-8");
}