java aes 模式_Java-AESUtil

//加密算法

private static final String ENCRY_ALGORITHM = "AES";//加密算法/加密模式/填充类型

private static final String CIPHER_MODE = "AES/CBC/PKCS7Padding";//设置iv偏移量,ECB加密模式不需要设置 iv 偏移量

private static final String IV = "0000000000000000";//设置加密字符集

private static final String CHARACTER = "UTF-8";//加密密码长度。默认 16 byte * 8 = 128 bit

private static final int PWD_SIZE = 16;static{//添加 AES/CBC/PKCS7Padding 支持

Security.addProvider(newBouncyCastleProvider());

}public static voidmain(String[] args) {

String str= "NiHao";byte[] encryptAES = encryptAES(str, "1234567899874563");byte[] decryptAES = decryptAES(encryptAES, "1234567899874563");

System.out.println(newString(decryptAES));

}/*** 密码长度不足补"0"*/

private static byte[] pwdHandler(String password) throwsUnsupportedEncodingException {byte[] data = null;if (password == null) {

password= "";

}

StringBuffer sb= newStringBuffer(PWD_SIZE);

sb.append(password);while (sb.length()

sb.append("0");

}if (sb.length() >PWD_SIZE) {

sb.setLength(PWD_SIZE);

}

data=sb.toString().getBytes(CHARACTER);returndata;

}/*** AES 加密

*

*@paramcleartext 明文

*@paramkey 密钥

*@return

*/

public static byte[] encryptAES(String cleartext, String key) {try{//获取加密密钥

SecretKeySpec keySpec = newSecretKeySpec(pwdHandler(key), ENCRY_ALGORITHM);//获取Cipher实例

Cipher cipher =Cipher.getInstance(CIPHER_MODE);//查看数据块位数 默认为16(byte) * 8 =128 bit//System.out.println("数据块位数(byte):" + cipher.getBlockSize());//初始化Cipher实例。设置执行模式以及加密密钥

cipher.init(Cipher.ENCRYPT_MODE, keySpec, newIvParameterSpec(IV.getBytes(CHARACTER)));//执行

byte[] cipherTextBytes =cipher.doFinal(cleartext.getBytes(CHARACTER));returncipherTextBytes;

}catch(NoSuchPaddingException e) {

e.printStackTrace();

}catch(NoSuchAlgorithmException e) {

e.printStackTrace();

}catch(BadPaddingException e) {

e.printStackTrace();

}catch(IllegalBlockSizeException e) {

e.printStackTrace();

}catch(InvalidKeyException e) {

e.printStackTrace();

}catch(Exception e) {

e.printStackTrace();

}return null;

}/*** AES 解密

*

*@paramciphertext 密文

*@paramkey 密钥

*@return

*/

public static byte[] decryptAES(byte[] ciphertext, String key) {try{//获取解密密钥

SecretKeySpec keySpec = newSecretKeySpec(pwdHandler(key), ENCRY_ALGORITHM);//获取Cipher实例

Cipher cipher =Cipher.getInstance(CIPHER_MODE);//查看数据块位数 默认为16(byte) * 8 =128 bit//System.out.println("数据块位数(byte):" + cipher.getBlockSize());//初始化Cipher实例。设置执行模式以及加密密钥

cipher.init(Cipher.DECRYPT_MODE, keySpec, newIvParameterSpec(IV.getBytes(CHARACTER)));//执行

byte[] clearTextBytes =cipher.doFinal(ciphertext);returnclearTextBytes;

}catch(NoSuchAlgorithmException e) {

e.printStackTrace();

}catch(InvalidKeyException e) {

e.printStackTrace();

}catch(NoSuchPaddingException e) {

e.printStackTrace();

}catch(BadPaddingException e) {

e.printStackTrace();

}catch(IllegalBlockSizeException e) {

e.printStackTrace();

}catch(Exception e) {

e.printStackTrace();

}//解密错误 返回 null

return null;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!关于Java AES加解密,需要使用到密钥和初始化向量(IV)。下面是一个简单的示例代码: ```java import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class AesUtil { private static final String ALGORITHM = "AES/CBC/PKCS5Padding"; // 加密算法 private static final String CHARSET = "UTF-8"; // 编码方式 /** * 加密方法 * * @param content 待加密字符串 * @param key 密钥 * @param iv 初始化向量 * @return 加密后的字符串 * @throws Exception */ public static String aesEncrypt(String content, String key, String iv) throws Exception { Cipher cipher = Cipher.getInstance(ALGORITHM); SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(CHARSET), "AES"); IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(CHARSET)); cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); byte[] encrypted = cipher.doFinal(content.getBytes(CHARSET)); return Base64.getEncoder().encodeToString(encrypted); } /** * 解密方法 * * @param content 待解密字符串 * @param key 密钥 * @param iv 初始化向量 * @return 解密后的字符串 * @throws Exception */ public static String aesDecrypt(String content, String key, String iv) throws Exception { Cipher cipher = Cipher.getInstance(ALGORITHM); SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(CHARSET), "AES"); IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(CHARSET)); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(content)); return new String(decrypted, CHARSET); } } ``` 其中,`aesEncrypt` 方法用于加密,`aesDecrypt` 方法用于解密。`key` 和 `iv` 参数分别为密钥和初始化向量,需要使用相同的 `key` 和 `iv` 才能成功解密。 使用示例: ```java public static void main(String[] args) throws Exception { String content = "Hello, AES!"; String key = "1234567890123456"; // 密钥长度必须为16/24/32位 String iv = "1234567890123456"; // 初始化向量长度必须为16位 String encrypted = AesUtil.aesEncrypt(content, key, iv); System.out.println("加密后:" + encrypted); String decrypted = AesUtil.aesDecrypt(encrypted, key, iv); System.out.println("解密后:" + decrypted); } ``` 输出结果: ``` 加密后:dS7U2yvJg3GRVTzB+TcHAA== 解密后:Hello, AES! ``` 注意,密钥长度必须为16/24/32位,初始化向量长度必须为16位。此外,加密算法使用的是`AES/CBC/PKCS5Padding`,这是一种安全可靠的加密方式,能够保证数据的机密性和完整性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值