AES加密
来源:互联网 发布:淘宝宁美国度装机怎样 编辑:程序博客网 时间:2024/06/05 20:17
import java.util.Base64;import java.util.Base64.Decoder;import java.util.Base64.Encoder;import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;public class AESUtil { private static final String IV_STRING = "16-Bytes--String"; /** * 加密 * @param content * @param key * @return * @throws Exception */ public static String encryptAES(String content, String key) throws Exception { byte[] byteContent = content.getBytes("UTF-8"); // 注意,为了能与 iOS 统一 // 这里的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 生成 byte[] enCodeFormat = key.getBytes(); SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES"); byte[] initParam = IV_STRING.getBytes(); 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 编码 Encoder encoder = Base64.getEncoder(); return encoder.encodeToString(encryptedBytes); } /** * 解密 * * @param content * @param key * @return * @throws Exception */ public static String decryptAES(String content, String key) throws Exception { // base64 解码 Decoder decoder = Base64.getDecoder(); byte[] encryptedBytes = decoder.decode(content); byte[] enCodeFormat = key.getBytes(); SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, "AES"); byte[] initParam = IV_STRING.getBytes(); 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, "UTF-8"); } public static void main(String[] args) throws Exception { String key=getChar(16); System.out.println(key); String contextString ="http://60.255.161.39/vod/sccn,COST2016120214152367.m3u8"; String encryptAES = encryptAES(contextString, key); System.out.println("加密后-》"+encryptAES); System.err.println("-----------------------------"); String decryptAES = decryptAES(encryptAES, key); System.out.println("解密后-》"+decryptAES); } private static String baseChars = "qwertyuiopasdfghjklzxcvbnm0123456789"; /** * @Title: getChar * @Description: 随机字符串 * @param @param len * @param @return 参数说明 * @return String 返回类型 * @author lihongliang */ public static String getChar(int len){ String chars = ""; for(int i=0;i<len;i++){ int baseIndex = (int)(Math.random() * baseChars.length()) + 1; chars += baseChars.charAt(baseIndex); } return chars.toUpperCase(); }}
阅读全文
0 0
- AES加密
- AES加密
- AES加密
- AES加密
- AES 加密
- AES加密
- AES 加密
- AES加密
- AES加密
- AES加密
- AES加密
- AES加密
- AES加密
- AES加密
- AES加密
- AES加密
- AES加密
- AES 加密
- L1-048. 矩阵A乘以B
- 数字图像处理--空间滤波
- JavaScript浏览器对象
- mysql 使用 GROUP BY 时报错 ERROR 1055 (42000)
- 飞控打信息打印
- AES加密
- Docker小技巧(二)——私服Registry搭建
- 模拟器Genymotion和Android Studio相关联
- huazi用户没有权限使用sudo命令以root身份执行命令 解决方法
- Linux 下的IPC控制命令
- 微信公众号授权登录
- 如何快速建设一个网站
- jpa domain设置默认值,Timestamp设置
- 多线程还是多进程的选择及区别