java aes256 加密_java AES256加密怎么实现啊?

这是一个Java实现的AES256加密解密工具类,使用CBC模式和NoPadding填充。代码中定义了固定的加密密钥,并提供了加密和解密的方法,支持对字符串进行加解密操作。通过示例展示了如何使用该工具类进行加解密,并打印了结果。
摘要由CSDN通过智能技术生成

展开全部

java.security.InvalidKeyException: Illegal key

package com.jlins;

import java.io.UnsupportedEncodingException;

import javax.crypto.Cipher;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;

import com.jlins.util.Hex;

/**

* java Aes256 加密

*

* @author jlins

*

*/

public class Aes256Encryptor {

// 说明 key 需要大家自己去设定加密解密的key,key牵涉到安全信息32313133353236313431303231363533e78988e69d8331333337396362,所以这里无法公布

private static final byte[] key = {};

private static final String transform = "AES/CBC/NoPadding";

private static final String algorithm = "AES";

private static final SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);

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

String pwds[] = { "123", "0123456789012345", "01234567890123456", "123", "123", "0123456789012345678",

"012345678901234567890123456789", "b", "0123456789012345", "01234567890123456", "012345678901234567" };

String ivss[] = { "test", "test", "test", "test0123456789012", "test01234567890123", "test", "test", "a",

"test", "test", "test" };

String rr[] = new String[ivss.length];

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

String en = encrypt(pwds[i], ivss[i]);

String decy = decrypt(en, ivss[i]);

rr[i] = "[" + ivss[i] + "],[" + decy + "]-->[" + en + "]";

System.out.println(rr[i]);

}

System.out.println("---------");

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

System.out.println(rr[i]);

}

}

/**

*/

public static String decrypt(String pHexText, String pIv) throws Exception {

Cipher cipher = Cipher.getInstance(transform);

byte[] encryptedBytes = Hex.decode(pHexText);

byte[] iv = createIV(pIv);

cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv));

byte[] decryptedBytes = cipher.doFinal(encryptedBytes);

System.arraycopy(decryptedBytes, 0, encryptedBytes, 0, encryptedBytes.length);

String result = new String(encryptedBytes);

return result.trim();

}

/**

*/

public static String encrypt(String pData, String pIv) throws Exception {

Cipher cipher = Cipher.getInstance(transform);

byte[] iv = createIV(pIv);

cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv));

byte[] output = cipher.doFinal(paddingData(pData));

byte[] encryptedContent = new byte[output.length];

System.arraycopy(output, 0, encryptedContent, 0, encryptedContent.length);

String result = new String(Hex.encode(encryptedContent)).toUpperCase();

return result;

}

/**

* 补齐的16位的整数倍

*

* @param pData

* @return

*/

private static byte[] paddingData(String pData) {

byte[] bytes = pData.getBytes();

int length = bytes.length / 16;

if (length * 16 < bytes.length) {

length++;

}

byte[] result = new byte[length * 16];

System.arraycopy(bytes, 0, result, 0, bytes.length);

for (int i = bytes.length; i < result.length; i++) {

result[i] = 0x00;

}

return result;

}

/**

* 初始化向量到16位

* */

private static byte[] createIV(String pIv) throws UnsupportedEncodingException {

byte[] bytes = pIv.getBytes("US-ASCII");

int length = bytes.length / 16;

if (length * 16 < bytes.length) {

length++;

}

byte[] result = new byte[16];

System.arraycopy(bytes, 0, result, 0, bytes.length > 16 ? 16 : bytes.length);

for (int i = bytes.length; i < result.length; i++) {

result[i] = 0x00;

}

return result;

}

}

本回答由电脑网络分类达人 系统推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值