java aes-128加密工具

僅做記錄,非原創(chuàng)

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
/**
 *
 * @author hman
 *
 */
public class AESUtil {

    // 加密
    public static String Encrypt(String sSrc, String sKey) throws Exception {
        if (sKey == null) {
            System.out.print("Key為空null");
            return null;
        }
        // 判斷Key是否為16位
        if (sKey.length() != 16) {
            System.out.print("Key長度不是16位");
            return null;
        }
        byte[] raw = sKey.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/補碼方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));

        return new Base64().encodeToString(encrypted);//此處使用BASE64做轉(zhuǎn)碼功能,同時能起到2次加密的作用。
    }

    // 解密
    public static String Decrypt(String sSrc, String sKey) throws Exception {
        try {
            // 判斷Key是否正確
            if (sKey == null) {
                System.out.print("Key為空null");
                return null;
            }
            // 判斷Key是否為16位
            if (sKey.length() != 16) {
                System.out.print("Key長度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = new Base64().decode(sSrc);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }

    public static void main(String[] args) throws Exception {
        /*
         * 此處使用AES-128-ECB加密模式,key需要為16位。
         */
        String cKey = "1234567812345678";
        // 需要加密的字串
        String cSrc = "111111111";
        System.out.println(cSrc);
        // 加密
        String enString = AESUtil.Encrypt(cSrc, cKey);
        System.out.println("加密后的字串是:" + enString);

        // 解密
        String DeString = AESUtil.Decrypt(enString, cKey);
        System.out.println("解密后的字串是:" + DeString);
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.計算機出現(xiàn)以前的密碼 這篇文章旨在淺顯易懂的介紹標題所述的各個算法概念與應(yīng)用,文中沒有數(shù)學(xué)公式。在主要概念出現(xiàn)...
    ZIJIAN94閱讀 2,132評論 0 2
  • 國家電網(wǎng)公司企業(yè)標準(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報批稿:20170802 前言: 排版 ...
    庭說閱讀 12,309評論 6 13
  • “子在川上曰:逝者如斯夫!不舍晝夜。”時間轉(zhuǎn)眼來到2019年,好快!二寶降臨,使得這個年關(guān)更加親切而有意義。四十多...
    我在當下閱讀 1,200評論 0 6
  • 突然覺得我活了24年,從來不知道想要干什么……小學(xué)是父母安排的,初中是小學(xué)同學(xué)想去,我跟她關(guān)系好,結(jié)果我去了她沒去...
    鬧喵閱讀 179評論 0 0
  • 山巔底谷輪回轉(zhuǎn), 歡愉傷悲交換現(xiàn); ?人間滋味各種有, 感知眼下待白頭。
    翱藍閱讀 165評論 0 1

友情鏈接更多精彩內(nèi)容