JAVA-对称加密AES加解密示例

这几天在解决报文中敏感字段传输问题时,由于对AES对称加密不熟悉,遇到了不少问题。特此记录,多的不说,直接上代码!代码中做了向量的异或(generateIv),如无特殊需要,可以不用。

private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
    private static final byte[] IV_BYTE = new byte[]{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

    /**
     * aes解密-256位
     */
    public static String aesDecrypt(String encryptContent, String password) throws InvalidEncryptedKeyException {
        if (StringUtils.isBlank(password)) {
            throw new InvalidEncryptedKeyException("password is not null");
        }
        if (password.length() != 16) {
            throw new InvalidEncryptedKeyException("password must be is 16 bytes");
        }
        try {
            //两个参数,第一个为私钥字节数组, 第二个为加密方式 AES或者DES
            Key keySpec = new SecretKeySpec(password.getBytes(), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(generateIv(password.getBytes(), IV_BYTE));
            //实例化加密类,参数为加密方式,要写全
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
            return new String(cipher.doFinal(Base64.decode(encryptContent)));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * aes加密-256位
     */
    public static String aesEncrypt(String content, String password) throws InvalidEncryptedKeyException {
        if (StringUtils.isBlank(password)) {
            throw new InvalidEncryptedKeyException("password is not null");
        }
        if (password.length() != 16) {
            throw new InvalidEncryptedKeyException("password must be is 16 bytes");
        }
        try {
            //两个参数,第一个为私钥字节数组, 第二个为加密方式 AES或者DES
            Key keySpec = new SecretKeySpec(password.getBytes(), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(generateIv(password.getBytes(), IV_BYTE));
            //实例化加密类,参数为加密方式,要写全
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
            //初始化,此方法可以采用三种方式,按服务器要求来添加。
            // (1)无第三个参数
            // (2)第三个参数为SecureRandom random = new SecureRandom();中random对象,随机数。(AES不可采用这种方法)
            // (3)采用此代码中的IVParameterSpec
            return Base64.encode(cipher.doFinal(content.getBytes("UTF-8")));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static byte[] generateIv(byte[] keys, byte[] iv) {
        byte[] tmpIv = new byte[iv.length];

        int i;
        for (i = 0; i < iv.length && i < keys.length; ++i) {
            tmpIv[i] = (byte) (iv[i] ^ keys[i]);
        }

        while (i < iv.length) {
            tmpIv[i] = iv[i];
            ++i;
        }

        return tmpIv;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值