AES加密模式有ECB,CBC,CTR等

AES填充模式有pkcs5padding, pkcs7padding, no padding等

AES数据块则有128位 192位 256位

AES加密密码,如果选择数据块128位,则要求密码长度为16,数据块192位则要求密码长度为24,数据块长度为256位,则要求密码长度为32

AES的偏移量和AES加密类似

AES输出则有base64和hex两种

AES字符集有gb2312,gbk,utf-8等

1 使用CBC+PKCS5Padding+base64+utf-8 做加密解密,代码如下

     /**
	 *加密操作
	 * @param content  待加密内容
	 * @param encodingFormat 编码方式
	 * @param sKey 密码
	 * @param ivParameter 偏移量
	 * @return
	 * @throws Exception
	 */
	public String encrypt(String content, String encodingFormat, String sKey, String ivParameter) {
		try {
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			byte[] raw = sKey.getBytes();
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
			byte[] encrypted = cipher.doFinal(content.getBytes(encodingFormat));
			return Base64Utils.encodeBase64String(encrypted);
		}catch (Exception e){
			log.error("data {} encrypt on error {}",content,e);
		}
		return null;
	}

	/**
	 *解密操作
	 * @param content 待解密内容
	 * @param encodingFormat 编码方式
	 * @param sKey 密码
	 * @param ivParameter 偏移量
	 * @return
	 * @throws Exception
	 */
	public String decrypt(String content, String encodingFormat, String sKey, String ivParameter) {
		try {
			byte[] raw = sKey.getBytes("ASCII");
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
			cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

			byte[] encrypted1 = Base64Utils.decodeBase64(content);
			byte[] original = cipher.doFinal(encrypted1);
			String originalString = new String(original, encodingFormat);
			return originalString;
		} catch (Exception ex) {
			log.error("data {} decrypt on error {}",content,e);
		}
		return null;
	}

2 使用CBC+no padding+hex+utf-8 做加密解密,代码如下

注意:此方式加密或者解密内容字节长度必须为16的整数倍,否则会报错

     /**
	 *加密操作
	 * @param content  待加密内容
	 * @param encodingFormat 编码方式
	 * @param sKey 密码
	 * @param ivParameter 偏移量
	 * @return
	 * @throws Exception
	 */
	public String encrypt(String content, String encodingFormat, String sKey, String ivParameter) {
		try {
			byte[] raw = sKey.getBytes();
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/CBC/nopadding");
			IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
			while (content.getBytes(encodingFormat).length % 16 != 0){
				content+=" ";
			}
			byte[] encrypted = cipher.doFinal(content.getBytes(encodingFormat));
			return Hex.encodeHexString(encrypted).toUpperCase(Locale.ROOT);
		}catch (Exception e){
			log.error("data {} decrtpy on error {}",content,e);
		}
		return null;
	}

	/**
	 *解密操作
	 * @param content 待解密内容
	 * @param encodingFormat 编码方式
	 * @param sKey 密码
	 * @param ivParameter 偏移量
	 * @return
	 * @throws Exception
	 */
	public String decrypt(String content, String encodingFormat, String sKey, String ivParameter) {
		try {
			byte[] raw = sKey.getBytes("ASCII");
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/CBC/nopadding");
			IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
			cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
			byte[] bytes = Hex.decodeHex(content);
			byte[] original = cipher.doFinal(bytes);
			String originalString = new String(original, encodingFormat);
			return originalString;
		} catch (Exception ex) {
			log.error("data {} decrtpy on error {}",content,ex);
		}
		return null;
	}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐