white knight

导航

 
package com.xx.ws.util;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;

public class AESOperator {
    private String sKey = "abcdef0123456789";
    private String ivParameter = "9iuj87y2hbi5wxlk";
    private final String CIPHER = "AES/CBC/PKCS5Padding";
    private final String AES = "AES";
    private final String CHARSET = "UTF-8";

    public AESOperator() {
    }

    public AESOperator(String key) {
        this.sKey = key;
    }

    public String encrypt(String sSrc) throws Exception {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            byte[] raw = this.sKey.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            IvParameterSpec iv = new IvParameterSpec(this.ivParameter.getBytes());
            cipher.init(1, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(sSrc.getBytes("UTF-8"));
            return DatatypeConverter.printBase64Binary(encrypted);
        } catch (Exception var7) {
            throw var7;
        }
    }

    public String encrypt(String sSrc, String key) throws Exception {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            byte[] raw = key.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            IvParameterSpec iv = new IvParameterSpec(this.ivParameter.getBytes());
            cipher.init(1, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(sSrc.getBytes("UTF-8"));
            return DatatypeConverter.printBase64Binary(encrypted);
        } catch (Exception var8) {
            throw var8;
        }
    }

    public String decrypt(String sSrc) throws Exception {
        try {
            byte[] raw = this.sKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(this.ivParameter.getBytes());
            cipher.init(2, skeySpec, iv);
            byte[] encrypted1 = DatatypeConverter.parseBase64Binary(sSrc);
            byte[] original = cipher.doFinal(encrypted1);
            return new String(original, "UTF-8");
        } catch (Exception var8) {
            throw var8;
        }
    }

    public String decrypt(String sSrc, String key) throws Exception {
        try {
            byte[] raw = key.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(this.ivParameter.getBytes());
            cipher.init(2, skeySpec, iv);
            byte[] encrypted1 = DatatypeConverter.parseBase64Binary(sSrc);
            byte[] original = cipher.doFinal(encrypted1);
            return new String(original, "UTF-8");
        } catch (Exception var9) {
            throw var9;
        }
    }

    public static void main(String[] args) throws Exception {
        AESOperator aesOper = new AESOperator("abcdef0123456789");
        String cSrc = "test123";
        System.out.println(cSrc + "  长度为" + cSrc.length());
        long lStart = System.currentTimeMillis();
        String enString = aesOper.encrypt(cSrc);
        System.out.println("加密后的字串是:" + enString + "长度为" + enString.length());
        long lUseTime = System.currentTimeMillis() - lStart;
        System.out.println("加密耗时:" + lUseTime + "毫秒");
        lStart = System.currentTimeMillis();
        String DeString = aesOper.decrypt(enString);
        System.out.println("解密后的字串是:" + DeString + "  长度为" + DeString.length());
        lUseTime = System.currentTimeMillis() - lStart;
        System.out.println("解密耗时:" + lUseTime + "毫秒");
    }
}

 

posted on 2018-03-14 16:39  white knight  阅读(174)  评论(0编辑  收藏  举报