java实现AES方式加密
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockS
千家信息网最后更新 2026年11月01日java实现AES方式加密
import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.NoSuchPaddingException;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import org.apache.commons.codec.binary.Base64;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.net.URLDecoder;import java.net.URLEncoder;import java.security.InvalidAlgorithmParameterException;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;public class AESUtil { private static final String IV_STRING = "sdf4ddfsFD86Vdf2"; private static final String encoding = "UTF-8"; public static String encryptAES(String content, String key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { byte[] byteContent = content.getBytes(encoding); // 注意,为了能与 iOS 统一 // 这里的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 生成 byte[] enCodeFormat = key.getBytes(encoding); SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES"); byte[] initParam = IV_STRING.getBytes(encoding); IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam); // 指定加密的算法、工作模式和填充方式 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); byte[] encryptedBytes = cipher.doFinal(byteContent); // 同样对加密后数据进行 base64 编码 String base64 = new Base64().encodeToString(encryptedBytes); //进行url编码 去掉= ? & return URLEncoder.encode(base64,encoding); } public static String decryptAES(String content, String key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, IOException { //URL解码 content = URLDecoder.decode(content,encoding); // base64 解码 byte[] encryptedBytes = Base64.decodeBase64(content); byte[] enCodeFormat = key.getBytes(encoding); SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, "AES"); byte[] initParam = IV_STRING.getBytes(encoding); IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec); byte[] result = cipher.doFinal(encryptedBytes); return new String(result, encoding); } public static void main(String[] args) throws Exception { JSONObject json = new JSONObject(); json.put("custNum", "111111"); /*json.put("name", "张三"); json.put("cityCode", "100001"); json.put("cityName", "北京市"); json.put("mobileNo", "15651876590");*/ String content = json.toJSONString(); System.out.println("加密前:" + content); String key = "djadiKJdj49dFJLd"; System.out.println("加密密钥和解密密钥:" + key); String encrypt = encryptAES(content, key); System.out.println("加密后:" + encrypt); String decrypt = decryptAES(encrypt , key); System.out.println("解密后:" + decrypt); }}
加密
密钥
编码
方式
数据
模式
算法
张三
北京
北京市
UTF-8
工作
生成
统一
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
中智农互联网科技
互联网科技公司排名
青岛互联网科技公司
朗涯互联网科技
互联网科技论文
美国互联网黑科技
上游互联网科技
互联网科技公司做什么
云创客互联网科技公司
常德市电业局互联网科技部
互联网新科技动漫ppt素材
浙南科技城互联网大厦建设工程
互联网科技创新的路径
伊宁互联网科技做什么的
郑州欣桑互联网科技有限公司
上海同邻互联网科技有限公司规模
好易买互联网科技
科技互联网公司两年变化
科技互联网专业工作会议发言稿
上海互联网科技园景观
互联网医疗科技
互联网 的科技创新
唐信互联网科技
小马过河互联网科技
友谊互联网络科技
佩信高科技互联网
华服互联网科技官网
互联网科技创新服务
互联网软件和科技
卓翼科技产业互联网合作