不能明文传证件号码后端加密解密最简单的方式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);

}

三:结果

相关推荐
毕设源码-郭学长1 小时前
【开题答辩全过程】以 基于SpringBoot技术的美妆销售系统为例,包含答辩的问题和答案
java·spring boot·后端
故事不长丨1 小时前
C#正则表达式完全攻略:从基础到实战的全场景应用指南
开发语言·正则表达式·c#·regex
猫头虎1 小时前
2025最新OpenEuler系统安装MySQL的详细教程
linux·服务器·数据库·sql·mysql·macos·openeuler
梨落秋霜1 小时前
Python入门篇【文件处理】
android·java·python
Java 码农1 小时前
RabbitMQ集群部署方案及配置指南03
java·python·rabbitmq
哈库纳玛塔塔2 小时前
放弃 MyBatis,拥抱新一代 Java 数据访问库
java·开发语言·数据库·mybatis·orm·dbvisitor
木子.李3472 小时前
ssh连接远程服务器相关总结
运维·服务器·ssh
phltxy2 小时前
从零入门JavaScript:基础语法全解析
开发语言·javascript
S***q3772 小时前
Spring Boot管理用户数据
java·spring boot·后端
BD_Marathon2 小时前
SpringBoot——辅助功能之切换web服务器
服务器·前端·spring boot