数据AES加密安全传输之后台JAVA加密解密

7 篇文章 1 订阅

AES是开发中常用的加密算法之一。AES的算法总是相同的, 但经常遇到前端加密结果,后台无法解密,这是因为加密设置的参数不一致导致的。如果要保持一致,必须使下列参数一致密钥长度、加密模式、填充方式、初始向量。

一、引入Java类

import javax.crypto.*;//AES加密解密
import sun.misc.*;//BASE64转码

二、加密

    /**
     * 加密
     */
    public static String encrypt(String content,String CRYPT_KEY,String IV_STRING) {
        byte[] encryptedBytes = new byte[0];
        try {
            byte[] byteContent = content.getBytes("UTF-8");
            //key
            byte[] enCodeFormat = CRYPT_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);
            encryptedBytes = cipher.doFinal(byteContent);
        } catch (Exception e) {
			System.out.println("AES encrypt Exception,content = {"+content+"},Exception = {"+e.getStackTrace()+"}");
        }
        return new BASE64Encoder().encode(encryptedBytes);
    }

三、解密

    /**
     * 解密
     */
    public static String decrypt(String content,String CRYPT_KEY,String IV_STRING) {
        try {
            // base64 解码
            byte[] encryptedBytes = new BASE64Decoder().decodeBuffer(content);
            //key
            byte[] enCodeFormat = CRYPT_KEY.getBytes();
            SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, "AES");
            //偏移量
            byte[] initParam = IV_STRING.getBytes();
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
            // 指定加密的算法、工作模式和填充方式
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
            byte[] result = cipher.doFinal(encryptedBytes);
            return new String(result, "UTF-8");
        } catch (Exception e) {
        	System.out.println( "AES decrypt Exception,content = {"+content+"},Exception = {"+e.getStackTrace()+"}");
        }
        return null;
    }

四、测试

    public static void main(String[] args) {
    	String CRYPT_KEY = "0123456789abcdef";//key
	    String IV_STRING = "0123456789abcdef";//偏移量
	    String content = "12345";//原文
	    System.out.println("原文:"+content);
	    String contenCrypt = encrypt(content.toString().replaceAll("(\r\n|\r|\n|\n\r)",""),CRYPT_KEY,IV_STRING);
		System.out.println("加密:"+contenCrypt);
		System.out.println("解密:"+decrypt(contenCrypt,CRYPT_KEY,IV_STRING));
		System.out.println("JS加密密文解密:"+decrypt("xWngUO1XD+BavHm1YkdTxA==",CRYPT_KEY,IV_STRING));
	}

五、输出结果
在这里插入图片描述
Java后台加密解密与上一篇文章《数据AES加密安全传输之前端JS加密解密》对应

参考:https://blog.csdn.net/coyote1994/article/details/52368921

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值