Java实现AES-128-ECB加密解密
AES加密标准又称为高级加密标准Rijndael加密法,是美国国家标准技术研究所NIST旨在取代DES的21世纪的加密标准。
下面就是我使用AES加密的java代码实现,可供大家参考:
package com.aas.fc.util;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* AES加密工具类
* MD5值后16位为key,AES采用128位ECB模式,填充模式pkcs5,加密之后再base64转码
*/
public class AESUtils {
/**
* 加密
* @param content
* @param key
* @return
* @throws Exception
*/
public static String Encrypt(String content, String key) throws Exception {
// 判断Key是否为16位
if (key.length() != 16) {
System.out.print("Key长度不是16位");
return null;
}
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128, new SecureRandom(key.getBytes()));
SecretKey secretKey = keyGenerator.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat , "AES");
//"算法/模式/补码方式"
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] encrypted = cipher.doFinal(content.getBytes("utf-8"));
//使用BASE64做转码功能,能起到2次加密的作用。
return new Base64().encodeToString(encrypted);
}
/**
* 解密
* @param content
* @param key
* @return
* @throws Exception
*/
public static String Decrypt(String content, String key) throws Exception {
try {
// 判断Key是否为16位
if (key.length() != 16) {
System.out.print("Key长度不是16位");
return null;
}
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(key.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat , "AES");
//"算法/模式/补码方式"
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, keySpec);
//先base64解密
byte[] encrypted1 = new Base64().decode(content);
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 {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
//此处使用AES-128-ECB加密模式,key需要为16位。
String key = "8749284927849201";
// 原字符串
String content = "hello world!";
System.out.println(simpleDateFormat.format(new Date())+"原字符串是:"+content);
// 加密
String encryptContent = AESUtils.Encrypt(content, key);
System.out.println(simpleDateFormat.format(new Date()) + "加密后的字符串是:" + encryptContent);
// 解密
String decryptContent = AESUtils.Decrypt(encryptContent, key);
System.out.println(simpleDateFormat.format(new Date()) + "解密后的字符串是:" + decryptContent);
}
}
执行效果如下: