JAVA_AES加密

来源:互联网 发布:淘宝网卡包 编辑:程序博客网 时间:2024/05/22 15:44


        使用AES加密解密的一个小Demo,其中用到一个第三方库:commons-codec-1.6

        commons-codec-1.6库的官网:http://commons.apache.org/proper/commons-codec/download_codec.cgi

        下载地址: http://download.csdn.net/detail/two_water/9552669

Demo:

package com.liangdianshui;import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;import org.apache.commons.codec.binary.Base64;/** * * @author liangdianshui * */public class AES_ECB {// 加密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做转码功能,同时能起到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 = "12345678abcdefgh";// 需要加密的字串String cSrc = "liangdianshui";System.out.println(cSrc);// 加密String enString = AES_ECB.Encrypt(cSrc, cKey);System.out.println("加密后的字串是:" + enString);// 解密String DeString = AES_ECB.Decrypt(enString, cKey);System.out.println("解密后的字串是:" + DeString);}}
运行结果:

package com.liangdianshui;import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import org.apache.commons.codec.binary.Base64;/** * AES 是一种可逆加密算法,对用户的敏感信息加密处理 对原始数据进行AES加密后,在进行Base64编码转化; */public class AES_CBC {/* * 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式,key需要为16位。 */private String sKey = "12345678abcdefgh";private String ivParameter = "12345678abcdefgh";private static AES_CBC instance = null;private AES_CBC() {}public static AES_CBC getInstance() {if (instance == null)instance = new AES_CBC();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 Base64().encodeToString(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[] encrypted1 = new Base64().decode(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 = "liangdianshui";System.out.println(cSrc);// 加密long lStart = System.currentTimeMillis();String enString = AES_CBC.getInstance().encrypt(cSrc);System.out.println("加密后的字串是:" + enString);// 解密lStart = System.currentTimeMillis();String DeString = AES_CBC.getInstance().decrypt(enString);System.out.println("解密后的字串是:" + DeString);}}
运行结果:

Demo的下载地址:http://download.csdn.net/detail/two_water/9552675

1 0
原创粉丝点击