本文主要实现加密代码,更多ASE简介,还请在简介的连接去观看大佬的介绍一、AES简介 AES为分组密码,分组密码也就是把明文分成一组一组的,每组长度相等,每次加密一组数据,直到加密完整个明文。在AES标准规范中,分组长度只能是128位,也就是说,每个分组为16个字节(每个字节8位)。密钥的长度可以使用128位、192位或256位。密钥的长度不同,推荐加密轮数也不同,推荐加密轮数也不同,如下表所示:
AES | 密钥长度(32位比特字) | 分组长度(32位比特字) | 加密轮数 |
---|---|---|---|
AES-128 | 4 | 4 | 10 |
AES-192 | 6 | 4 | 12 |
AES-256 | 8 | 4 | 14 |
@Slf4j
public class ASEUtil { private static final String PASS_NEED = "javayh-haiji-dylan";
private static final String INSTANCE = "AES/ECB/PKCS5Padding";
private static SecretKeySpec key;
private staticCipherencCipher = null;
private staticCipherdecCipher = null;
static {
byte[] enCodeFormat = Arrays.copyOf(PASS_NEED.getBytes(), 16);
key = new SecretKeySpec(enCodeFormat, "AES");
try {
encCipher = Cipher.getInstance(INSTANCE);
encCipher.init(Cipher.ENCRYPT_MODE, key);
decCipher = Cipher.getInstance(INSTANCE);
decCipher.init(Cipher.DECRYPT_MODE, key);
} catch (NoSuchAlgorithmException e) {
log.info(" NoSuchAlgorithmException is error {}", e.getMessage());
} catch (NoSuchPaddingException e) {
log.info(" NoSuchPaddingException is error {}", e.getMessage());
} catch (InvalidKeyException e) {
log.info(" InvalidKeyException is error {}", e.getMessage());
}
}
public static String encryData(String plaintData){
byte result[] = new byte[0];
try {
result = encCipher.doFinal(plaintData.getBytes());
} catch (IllegalBlockSizeException e) {
log.info(" IllegalBlockSizeException is error {}", e.getMessage());
} catch (BadPaddingException e) {
log.info(" BadPaddingException is error {}", e.getMessage());
}
return new String(Base64.encode(result));
}
public staticString decryData(String encData){
byte result[] = new byte[0];
try {
result = decCipher.doFinal(Base64.decode(encData.getBytes()));
}catch (IllegalBlockSizeException e) {
log.info(" IllegalBlockSizeException is error {}", e.getMessage());
} catch (BadPaddingException e) {
log.info(" BadPaddingException is error {}", e.getMessage());
}
return new String(result);
}
public static byte[] encryData(byte []plaintData){
byte result[] = new byte[0];
try {
result = encCipher.doFinal(plaintData);
} catch (IllegalBlockSizeException e) {
log.info(" IllegalBlockSizeException is error {}", e.getMessage());
} catch (BadPaddingException e) {
log.info(" BadPaddingException is error {}", e.getMessage());
}
return result;
}
public staticbyte[] decryData(byte [] encData){
byte result[] = new byte[0];
try {
result = decCipher.doFinal(encData);
} catch (IllegalBlockSizeException e) {
log.info(" IllegalBlockSizeException is error {}", e.getMessage());
} catch (BadPaddingException e) {
log.info(" BadPaddingException is error {}", e.getMessage());
}
return result;
}// public static void main(String[] args) throws BadPaddingException, IllegalBlockSizeException {
//String admin = encryData("admin");
//System.out.println(admin);
// }
}
关注 Java有货领取更多资料
文章图片
如遇到问题可以联系小编。微信:372787553,互相学习
技术博客:https://blog.csdn.net/weixin_38937840
SpringCloud学习代码: https://github.com/Dylan-haiji/javayh-cloud
【ASE实现数据加密】Redis、Mongo、Rabbitmq、Kafka学习代码: https://github.com/Dylan-haiji/javayh-middleware
AlibabaCloud学习代码:https://github.com/Dylan-haiji/javayh-cloud-nacos
SpringBoot+SpringSecurity实现自定义登录学习代码:https://github.com/Dylan-haiji/javayh-distribution
推荐阅读
- Java|Java基础——数组
- 人工智能|干货!人体姿态估计与运动预测
- java简介|Java是什么(Java能用来干什么?)
- Java|规范的打印日志
- Linux|109 个实用 shell 脚本
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- Spring注解驱动第十讲--@Autowired使用
- SqlServer|sql server的UPDLOCK、HOLDLOCK试验
- jvm|【JVM】JVM08(java内存模型解析[JMM])
- 技术|为参加2021年蓝桥杯Java软件开发大学B组细心整理常见基础知识、搜索和常用算法解析例题(持续更新...)