AES简单介绍:
高级加密标准(英语:Advanced Encryption Standard,缩写:AES),是一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。AES可以使用128、192、和256位密钥,并且用128位分组加密和解密数据,相对来说安全很多。完善的加密算法在理论上是无法破解的,除非使用穷尽法。使用穷尽法破解密钥长度在128位以上的加密数据是不现实的,仅存在理论上的可能性。统计显示,即使使用目前世界上运算速度最快的计算机,穷尽128位密钥也要花上几十亿年的时间,更不用说去破解采用256位密钥长度的AES算法了。
AES-128-ECB工具:
import org.apache.commons.codec.binary.Base64;
import org.springframework.util.StringUtils;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
/**
* @Author :feiyang
* @Date :Created in 5:37 PM 2018/11/28
*/
public class AesEcbUtil {
/**
* aes-128-ecb 密钥字节数
*/
private static final int AES_KEY_LENGTH = 16;
private static final String AES_KEY = "abcdefghijklmnop";
/**
* 加密
* @param content
* @return
*/
public static String encrypt(String content){
if (StringUtils.isEmpty(AES_KEY)){
return null;
}
if (AES_KEY.length() != AES_KEY_LENGTH){
return null;
}
try {
byte[] raw =AES_KEY.getBytes("utf-8");
SecretKeySpec secretKeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encrypt = cipher.doFinal(content.getBytes("utf-8"));
return new Base64().encodeToString(encrypt);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return null;
}
/**
* 解密
* @param content
* @return
*/
public static String decrypt(String content){
if (StringUtils.isEmpty(AES_KEY)){
return null;
}
if (AES_KEY.length() != AES_KEY_LENGTH){
return null;
}
try {
byte[] raw = AES_KEY.getBytes();
SecretKeySpec secretKeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] decrypt = new Base64().decode(content);
byte[] original = cipher.doFinal(decrypt);
return new String(original, "utf-8");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}