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

}

三:结果

相关推荐
lly2024062 小时前
Bootstrap 警告框
开发语言
2601_949146532 小时前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
曹牧2 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
KYGALYX2 小时前
服务异步通信
开发语言·后端·微服务·ruby
zmzb01032 小时前
C++课后习题训练记录Day98
开发语言·c++
爬山算法3 小时前
Hibernate(90)如何在故障注入测试中使用Hibernate?
java·后端·hibernate
七夜zippoe3 小时前
CANN Runtime任务描述序列化与持久化源码深度解码
大数据·运维·服务器·cann
盟接之桥3 小时前
盟接之桥说制造:引流品 × 利润品,全球电商平台高效产品组合策略(供讨论)
大数据·linux·服务器·网络·人工智能·制造
kfyty7253 小时前
集成 spring-ai 2.x 实践中遇到的一些问题及解决方案
java·人工智能·spring-ai
猫头虎3 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven