aes cbc java实现_Java实现AES-128-CBC算法加解密

这是一个Java实现的AES-128-CBC加密解密工具类,包括加密、解密和签名生成的方法。示例代码展示了如何使用此类进行加密和解密操作。
摘要由CSDN通过智能技术生成

AES加解密工具类

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.util.ArrayList;

import java.util.Collections;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import javax.crypto.Cipher;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

/**

* AES加密128位CBC模式工具类

*/

public class AESUtils {

//解密密钥(自行随机生成)

public static final String KEY = "qxhzngy266a186ke";//密钥key

public static final String IV = "1ci5crnda6ojzgtr";//向量iv

//认证密钥(自行随机生成)

public static final String AK = "s2ip9g3y3bjr5zz7ws6kjgx3ysr82zzw";//AccessKey

public static final String SK = "uv8zr0uen7aim8m7umcuooqzdv8cbvtf";//SecretKey

//加密

public static String Encrypt(String content) throws Exception {

byte[] raw = KEY.getBytes("utf-8");

SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式"

//使用CBC模式,需要一个向量iv,可增加加密算法的强度

IvParameterSpec ips = new IvParameterSpec(IV.getBytes());

cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ips);

byte[] encrypted = cipher.doFinal(content.getBytes());

return new BASE64Encoder().encode(encrypted);

}

//解密

public static String Decrypt(String content) throws Exception {

try {

byte[] raw = KEY.getBytes("utf-8");

SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

IvParameterSpec ips = new IvParameterSpec(IV.getBytes());

cipher.init(Cipher.DECRYPT_MODE, skeySpec, ips);

byte[] encrypted1 = new BASE64Decoder().decodeBuffer(content);

try {

byte[] original = cipher.doFinal(encrypted1);

String originalString = new String(original);

return originalString;

} catch (Exception e) {

System.out.println(e.toString());

return null;

}

} catch (Exception ex) {

System.out.println(ex.toString());

return null;

}

}

//获取认证签名(身份认证需要)

public static String getSign(String currentTime) throws Exception {

String sign = "";

Mapmap=new HashMap();

map.put("ak",AK);

map.put("sk",SK);

map.put("ts",currentTime);

//获取 参数字典排序后字符串

String decrypt = getOrderMap(map);

try {

//指定sha1算法

MessageDigest digest = MessageDigest.getInstance("SHA-1");

digest.update(decrypt.getBytes());

//获取字节数组

byte messageDigest[] = digest.digest();

// Create Hex String

StringBuffer hexString = new StringBuffer();

// 字节数组转换为十六进制数

for (int i = 0; i < messageDigest.length; i++) {

String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);

if (shaHex.length() < 2) {

hexString.append(0);

}

hexString.append(shaHex);

}

sign = hexString.toString();

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

}

return sign;

}

//获取参数的字典排序

private static String getOrderMap(Mapmaps){

ListparamNames = new ArrayList();

for(Map.Entryentry : maps.entrySet()){

paramNames.add(entry.getValue().toString());

}

Collections.sort(paramNames);

StringBuilder paramStr = new StringBuilder();

for(String paramName : paramNames){

paramStr.append(paramName);

}

return paramStr.toString();

}

}

测试结果

public class AES128CBC {

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

String content = "需要加密的内容";

//加密

String ens = AESUtils.Encrypt(content);

System.out.println("加密后:" + ens);

//解密

String des = AESUtils.Decrypt(ens);

System.out.println("解密后:" + des);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值