JAVA實現AES的加密和解密演算法

來源:互聯網
上載者:User

標籤:

原文 JAVA實現AES的加密和解密演算法

import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;/** * AES 是一種可逆密碼編譯演算法,對使用者的敏感資訊加密處理 * 對未經處理資料進行AES加密後,在進行Base64編碼轉化; */public class AESOperator {    /*    * 加密用的Key 可以用26個字母和數字組成    * 此處使用AES-128-CBC加密模式,key需要為16位。    */    private String sKey = "0123456789abcdef";    private String ivParameter = "0123456789abcdef";    private static AESOperator instance = null;    private AESOperator() {    }    public static AESOperator getInstance() {        if (instance == null)            instance = new AESOperator();        return instance;    }    // 加密    public String encrypt(String sSrc) throws Exception {        Cipher cipher = Cipher.getInstance("AES / CBC / PKCS5Padding");        byte[] raw = sKey.getBytes();        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");        IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());//使用CBC模式,需要一個向量iv,可增加密碼編譯演算法的強度        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));        return new BASE64Encoder().encode(encrypted);//此處使用BASE64做轉碼。    }    // 解密    public String decrypt(String sSrc) throws Exception {        try {            byte[] raw = sKey.getBytes("ASCII");            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);//先用base64解密            byte[] original = cipher.doFinal(encrypted1);            String originalString = new String(original, "utf-8");            return originalString;        } catch (Exception ex) {            return null;        }    }    public static void main(String[] args) throws Exception {// 需要加密的字串        String cSrc = "我來自www.wenhq.com";        System.out.println(cSrc);// 加密        long lStart = System.currentTimeMillis();        String enString = AESOperator.getInstance().encrypt(cSrc);        System.out.println("加密後的字串是:" + enString);        long lUseTime = System.currentTimeMillis() - lStart;        System.out.println("加密耗時:" + lUseTime + "毫秒");// 解密        lStart = System.currentTimeMillis();        String DeString = AESOperator.getInstance().decrypt(enString);        System.out.println("解密後的字串是:" + DeString);        lUseTime = System.currentTimeMillis() - lStart;        System.out.println("解密耗時:" + lUseTime + "毫秒");    }}

 

JAVA實現AES的加密和解密演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.