不能明文传证件号码后端加密解密最简单的方式AES

一:生成密钥

private static final String ALGORITHM = "AES";

// 生成密钥

public static String generateKey() throws Exception {

KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);

// 初始化128位密钥

keyGen.init(128);

SecretKey secretKey = keyGen.generateKey();

return Base64.getEncoder().encodeToString(secretKey.getEncoded());

}

二:利用生成的密钥加密解密

public class SimpleAesUtil {

private static final String ALGORITHM = "AES";

private static String key="LKrFJ0OA8bYDCjIsPRju+A==";

// 加密

public static String encrypt(String data) throws Exception {

byte\[\] keyBytes = Base64.getDecoder().decode(key);

SecretKeySpec keySpec = new SecretKeySpec(keyBytes, ALGORITHM);

Cipher cipher = Cipher.getInstance(ALGORITHM);

cipher.init(Cipher.ENCRYPT_MODE, keySpec);

byte\[\] encryptedBytes = cipher.doFinal(data.getBytes());

return Base64.getEncoder().encodeToString(encryptedBytes);

}

// 解密

public static String decrypt(String encryptedData) throws Exception {

byte\[\] keyBytes = Base64.getDecoder().decode(key);

SecretKeySpec keySpec = new SecretKeySpec(keyBytes, ALGORITHM);

Cipher cipher = Cipher.getInstance(ALGORITHM);

cipher.init(Cipher.DECRYPT_MODE, keySpec);

byte\[\] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData));

return new String(decryptedBytes);

}

public static void main(String\[\] args) throws Exception {

String originalText = "371502198906089802";

// 加密

String encrypted = encrypt(originalText);

System.out.println("加密结果: " + encrypted);

// 解密

String decrypted = decrypt(encrypted);

System.out.println("解密结果: " + decrypted);

}

三:结果

相关推荐
一线大码18 分钟前
Smart-Doc 的简单使用
java·后端·restful
MacroZheng2 小时前
Claude Code官方桌面端正式发布,夯爆了!
java·人工智能·后端
虚无境2 小时前
如何编写一个SpringBoot项目告警推送的Starter
java·prometheus·webhook
NE_STOP17 小时前
Vide Coding--AI编程工具的选择
java
大树8817 小时前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
LDR00617 小时前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术17 小时前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园17 小时前
C++20 Modules 模块详解
java·开发语言·spring
程序员黑豆17 小时前
JDK 下载安装与配置详细教程
java·前端·ai编程