AES加密编码格式用base64

1.今天我有个朋友说他们公司用上面这个加密跟编码的,但是他解析出来老是乱码,然后我就看了看帮他改了改,记录一下,顺便有朋友也遇到这样问题的时候可以看看,代码如下:
**
 * 项目名称 FileOutputStreamClass
 * 包名 otouzi.com.fileoutputstreamclass
 * 类名 AESBase
 * 时间 2018/8/17
 */

public class AESBase {

private static final String IV_STRING = "shoushoutao*.+Ap";
private static final String KEY = "shoushoutao*.+Ap";

public static String encryptAES(String content) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
byte[] byteContent = content.getBytes("UTF-8");
// 注意,为了能与 iOS 统一
        // 这里的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 生成
        byte[] enCodeFormat = KEY.getBytes();
        SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");

byte[] initParam = IV_STRING.getBytes();
        IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);

// 指定加密的算法、工作模式和填充方式
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);

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


// 同样对加密后数据进行 base64 编码
        return Base64.encodeToString(encryptedBytes, Base64.NO_WRAP);//Base64.NO_WRAP
    }



public static String decrypt(byte[] content) throws Exception {
try {
byte[] encrypted1 = Base64.decode(content, Base64.NO_WRAP);
// 创建AES秘钥
            SecretKeySpec key = new SecretKeySpec(KEY.getBytes("UTF-8"), "AES");
// 创建密码器
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
// 初始化解密器
            byte[] initParam = IV_STRING.getBytes("UTF-8");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);

            cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
// 解密
            byte[] original = cipher.doFinal(encrypted1);
return new String(original, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
return null;
        }
    }

/**
     * 将二进制转换成16进制
     *
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuilder sb = new StringBuilder();
for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
return sb.toString();
    }

}
————————————————
版权声明:本文为CSDN博主「梦想启蒙」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u011553874/article/details/89709507

就是这么简单然后就处理了,在android中测试运力不能调用base64的库,这个需要注意一下,剩下的
也就没有啥大问题了。有问题留言

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值