Base64遇到的坑

在做AES加密之后再进行base64输出时遇到的坑,因为编码不一致的问题导致最后的输出结果不一致

pom.xml

		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.12.0</version>
		</dependency>
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.15</version>
		</dependency>

AES加密

 /**
     * 算法
     */
    private static final String ALGORITHMSTR = "AES/CBC/PKCS5Padding";


    /**
     * base 64 encode
     *
     * @param bytes 待编码的byte[]
     * @return 编码后的base 64 code
     */
    private static byte[] base64Encode(byte[] bytes) {
        return Base64.encodeBase64(bytes);
    }

    /**
     * base 64 decode
     *
     * @param base64Code 待解码的base 64 code
     * @return 解码后的byte[]
     * @throws Exception 抛出异常
     */
    private static byte[] base64Decode(String base64Code){
        //return StringUtils.isEmpty(base64Code) ? null : Base64.decodeBase64(base64Code);
        return StringUtils.isEmpty(base64Code) ? null : Base64.decodeBase64(base64Code.getBytes(StandardCharsets.UTF_8));
    }


    /**
     * AES加密
     *
     * @param content    待加密的内容
     * @return 加密后的byte[]
     */
    private static byte[] aesEncryptToBytes(String content) throws Exception {

        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
        IvParameterSpec iv = new IvParameterSpec(KEY.getBytes(StandardCharsets.UTF_8));
        SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(StandardCharsets.UTF_8), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);

        return cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
    }


    /**
     * AES加密为base 64 code
     *
     * @param content    待加密的内容
     * @return 加密后的base 64 code
     */
    public static String aesEncrypt(String content) throws Exception {
        byte[] bytes = base64Encode(aesEncryptToBytes(content));
        String s = new String(bytes, StandardCharsets.UTF_8);
        System.out.println(s);
        return s;
    }

    /**
     * AES解密
     *
     * @param encryptBytes 待解密的byte[]
     * @return 解密后的String
     */
    private static String aesDecryptByBytes(byte[] encryptBytes) throws Exception {

        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
        IvParameterSpec iv = new IvParameterSpec(KEY.getBytes(StandardCharsets.UTF_8));
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY.getBytes(StandardCharsets.UTF_8), "AES"), iv);
        byte[] decryptBytes = cipher.doFinal(encryptBytes);

        return new String(decryptBytes, StandardCharsets.UTF_8);
    }


    /**
     * 将base 64 code AES解密
     *
     * @param encryptStr 待解密的base 64 code
     * @param decryptKey 解密密钥
     * @return 解密后的string
     */
    public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
        return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(decryptKey));
    }

问题

 private static byte[] aesEncryptToBytes(String content) throws Exception {

        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
        IvParameterSpec iv = new IvParameterSpec(KEY.getBytes(StandardCharsets.UTF_8));
        SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(StandardCharsets.UTF_8), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);

        return cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
    }

之前在这里的**cipher.doFinal(content.getBytes(StandardCharsets.UTF_8))**语句里没有指定utf8字符集,

导致在打包jar包后和在idea中运行得到的加密结果不同

分析

没有具体查证,仅自我分析。

因为在idea中运行,虽然没有指定utf8字符集,但可能idea本身的指定utf8被使用了,但打包为jar包在其它平台上运行后,就会使用其它平台的默认字符集,使得加密的结果不同

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值