decrypt/encrypt String and return a String - Java Security

Java examples for Security:decrypt encrypt String

Description

decrypt/encrypt String and return a String

Demo Code



import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;


public class AESHelper {
    public static byte[] keyValue = new byte[] {
    22, 25, -35, -45, 25, 98, -55, -45, 10, 35, -45, 25, 55, 26, -95, 25,
            -65, -78, -99, 85, 45, -62, 10, -0, 11, 77, -35, 48, -98, 65,
            -32, 14, -78, 25, 36, -56, -45, -45, -20, 12, 15, -35, -75, 15,
            -14, 62, -25, 33, -45, 55, 68, -88, 36, 42, -9 };
    private static byte[] iv = new byte[] { //
    -12, 35, -25, 65, 45, -87, 95, -22, -15, 45, 55, -66, 32, 5 - 4, 84, 55 };
    private static SecretKey key; //
    private static AlgorithmParameterSpec paramSpec; //
    private static Cipher ecipher; //

    static {//from   w w  w.ja v a2  s  . com
        KeyGenerator kgen;
        try {
            //?
            kgen = KeyGenerator.getInstance("AES");
            //
            kgen.init(128, new SecureRandom(keyValue));
            //KeyGenerator
            key = kgen.generateKey();
            //ivIV? ?
            paramSpec = new IvParameterSpec(iv);
            // Cipher 
            ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        }
    }

    /**
     * ?AES
     * @param msg ?
     * @return
     */
    public static String encrypt(String msg) {
        String str = "";
        try {
            //? cipher
            ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
            //?16?
            str = asHex(ecipher.doFinal(msg.getBytes()));
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return str;
    }

    /**
     * 16
     * @param value ?
     * @return
     */
    public static String decrypt(String value) {
        try {
            ecipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
            return new String(ecipher.doFinal(asBin(value)));
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * ?16?
     * @param buf
     * @return
     */
    private static String asHex(byte buf[]) {
        StringBuffer strbuf = new StringBuffer(buf.length * 2);
        int i;
        for (i = 0; i < buf.length; i++) {
            if (((int) buf[i] & 0xff) < 0x10)//?
                strbuf.append("0");
            strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
        }
        return strbuf.toString();
    }

    /**
     * ?16?
     * @param src
     * @return
     */
    private static byte[] asBin(String src) {
        if (src.length() < 1)
            return null;
        byte[] encrypted = new byte[src.length() / 2];
        for (int i = 0; i < src.length() / 2; i++) {
            int high = Integer
                    .parseInt(src.substring(i * 2, i * 2 + 1), 16);//
            int low = Integer.parseInt(src.substring(i * 2 + 1, i * 2 + 2),
                    16);//
            encrypted[i] = (byte) (high * 16 + low);
        }
        return encrypted;
    }
}

Related Tutorials