AES128加密解密以及参数的处理

	/**
	 * AES加密实现方法
	 */
	public static byte[] AESEncrypt(byte[] plainBytes, byte[] keyBytes, String keyAlgorithm, String cipherAlgorithm, String IV)
			throws Exception {
		try {
			// AES密钥长度为128bit、192bit、256bit,默认为128bit
			if (keyBytes.length % 8 != 0 || keyBytes.length < 16 || keyBytes.length > 32) {
				throw new Exception("AES密钥长度不合法");
			}
			Cipher cipher = Cipher.getInstance(cipherAlgorithm);
			SecretKey secretKey = new SecretKeySpec(keyBytes, keyAlgorithm);
			if (StringUtils.trimToNull(IV) != null) {
				IvParameterSpec ivspec = new IvParameterSpec(IV.getBytes());
				cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
			} else {
				cipher.init(Cipher.ENCRYPT_MODE, secretKey);
			}
			byte[] encryptedBytes = cipher.doFinal(plainBytes);

			return encryptedBytes;
		} catch (NoSuchAlgorithmException e) {
			throw new Exception(String.format("没有[%s]此类加密算法", cipherAlgorithm));
		} catch (NoSuchPaddingException e) {
			throw new Exception(String.format("没有[%s]此类填充模式", cipherAlgorithm));
		} catch (InvalidKeyException e) {
			System.out.println(e);
			throw new Exception("无效密钥");
		} catch (InvalidAlgorithmParameterException e) {
			throw new Exception("无效密钥参数");
		} catch (BadPaddingException e) {
			throw new Exception("错误填充模式");
		} catch (IllegalBlockSizeException e) {
			throw new Exception("加密块大小不合法");
		}
	}

	/**
	 * AES解密实现方法
	 */
	public static byte[] AESDecrypt(byte[] encryptedBytes, byte[] keyBytes, String keyAlgorithm, String cipherAlgorithm, String IV)
			throws Exception {
		try {
			// AES密钥长度为128bit、192bit、256bit,默认为128bit
			if (keyBytes.length % 8 != 0 || keyBytes.length < 16 || keyBytes.length > 32) {
				throw new Exception("AES密钥长度不合法");
			}

			Cipher cipher = Cipher.getInstance(cipherAlgorithm);
			SecretKey secretKey = new SecretKeySpec(keyBytes, keyAlgorithm);
			if (IV != null && StringUtils.trimToNull(IV) != null) {
				IvParameterSpec ivspec = new IvParameterSpec(IV.getBytes());
				cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);
			} else {
				cipher.init(Cipher.DECRYPT_MODE, secretKey);
			}

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

			return decryptedBytes;
		} catch (NoSuchAlgorithmException e) {
			throw new Exception(String.format("没有[%s]此类加密算法", cipherAlgorithm));
		} catch (NoSuchPaddingException e) {
			throw new Exception(String.format("没有[%s]此类填充模式", cipherAlgorithm));
		} catch (InvalidKeyException e) {
			throw new Exception("无效密钥");
		} catch (InvalidAlgorithmParameterException e) {
			throw new Exception("无效密钥参数");
		} catch (BadPaddingException e) {
			throw new Exception("错误填充模式");
		} catch (IllegalBlockSizeException e) {
			throw new Exception("解密块大小不合法");
		}
	}
	/**
	 * AES加密
	 * @author zc-t208
	 * @param param 需要加密的字符串
	 * @return
	 */
	public static String AESEncodeParam(String param){
		if(StringUtils.isEmpty(param)){
			return null;
		}
		String encryptData = "";
		String strKey = sysConfigService.getAESStrKey().getConfigValue();
		try {
			byte[] base64encryptDataBytes = Base64.encodeBase64(AESEncrypt(param.getBytes("UTF-8"),strKey.getBytes("UTF-8"),"AES","AES/ECB/PKCS5Padding",null));
			encryptData = new String(base64encryptDataBytes, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			log.info(e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			log.info(e.getMessage());
			e.printStackTrace();
		}
		return encryptData;
		
	}
	/**
	 * AES解密
	 * @author zc-t208
	 * @param param 需要解密的参数
	 * @return
	 */
	public static String AESdecodeParam(String param){
		if(StringUtils.isEmpty(param)){
			return null;
		}
		String resData = "";
		String strKey = sysConfigService.getAESStrKey().getConfigValue();
		try {
			byte[] decodeBase64DataBytes = Base64.decodeBase64(param.getBytes("UTF-8"));
			byte[] merchantXmlDataBytes = AESDecrypt(decodeBase64DataBytes,strKey.getBytes("UTF-8"), "AES", "AES/ECB/PKCS5Padding", null);
			resData = new String(merchantXmlDataBytes,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			log.info(e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			log.info(e.getMessage());
			e.printStackTrace();
		}
		return resData;
		
	}



	/**
	 * 获取AES秘钥方法
	 * @param length
	 * @return
	 */
	public static String generateLenString(int length) {
		char[] cResult = new char[length];
		int[] flag = { 0, 0, 0 }; // A-Z, a-z, 0-9
		int i = 0;
		while (flag[0] == 0 || flag[1] == 0 || flag[2] == 0 || i < length) {
			i = i % length;
			int f = (int) (Math.random() * 3 % 3);
			if (f == 0)
				cResult[i] = (char) ('A' + Math.random() * 26);
			else if (f == 1)
				cResult[i] = (char) ('a' + Math.random() * 26);
			else
				cResult[i] = (char) ('0' + Math.random() * 10);
			flag[f] = 1;
			i++;
		}
		return new String(cResult);
	}
注意:加密秘钥为16位随机数字,并且加密和解密使用同一秘钥,加密后的参数想要传递,需要再进行一次加密后再传输
keyAlgorithm为"AES"
cipherAlgorithm为"AES/ECB/PKCS5Padding"

本博客为自己记录所做,如果有什么不对的地方,请多多指教




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值