java AES解密ECB模式ZeroPadding填充

AES是分组加密的,16字节一组加密,最后可能会不足16字节的,所以需要padding补充到16字节。

ZeroPadding它是使用“0”作为填充数据的填充方式,但是java提供了NoPadding.

NoPadding意思是不做填充,也满足不了,那就自己手动改下吧。

package com.util;

import org.apache.commons.codec.binary.Base64;

import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * ZeroPadding填充
 * 
 * @author libaibai
 * @version 1.0 
 */
public class AesUtils {

	// private static final String KEY_ALGORITHM = "AES";
	private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/NoPadding";// 默认的加密算法

	/**
	 * AES 加密操作
	 *
	 * @param content 待加密内容
	 * @param password 加密密码
	 * @return 
	 */
	public static byte[] encrypt(String content, String password) {
		try {
			Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);// 创建密码器
			int blockSize = cipher.getBlockSize();
			byte[] byteContent = content.getBytes();
			int plaintextLength = byteContent.length;
			if (plaintextLength % blockSize != 0) {
				plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
			}
			byte[] plaintext = new byte[plaintextLength];
			System.arraycopy(byteContent, 0, plaintext, 0, byteContent.length);
			cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(password));// 初始化为加密模式的密码器
			//return cipher.doFinal(plaintext);// 加密
                        return Base64.encodeBase64String(cipher.doFinal(plaintext)).getBytes();//
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

	/**
	 * AES 解密操作
	 *
	 * @param content
	 * @param password
	 * @return
	 */
	public static String decrypt(String content, String password) {
		try {
			// 实例化
			Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
			cipher.init(Cipher.DECRYPT_MODE, getSecretKey(password));// 初始化为加密模式的密码器
			// 执行操作
			byte[] result = cipher.doFinal(Base64.decodeBase64(content));// 解密
			return new String(result, "utf-8");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

	/**
	 * 生成加密秘钥
	 *
	 * @return
	 */
	private static SecretKeySpec getSecretKey(final String password) {
		int len = password.getBytes().length;
		if (len % 16 != 0) {
			len = len + (16 - (len % 16));
		}
		byte[] newpass = new byte[len];
		System.arraycopy(password.getBytes(), 0, newpass, 0, password.getBytes().length);
		SecretKeySpec keySpec = new SecretKeySpec(newpass, "AES");
		return keySpec;
	}

	public static void main(String[] args) {
		String str = "hK1yEuGwGuOXo/argbmMhHUyFC7C7QHDMrOGNCVhXrLTMSCMhE6BUt1ACSBJ4Pf9bWE46+c2np2j6elUbpcN6w==";
		String key = "1234567890abcdef";
		System.out.println(Arrays.toString(decrypt(str, key).getBytes()));
	}
}

 

 

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木小百99

听说打赏的人都发财了

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值