cryptojs加密和java解密:AES算法

试了一下午终于跑通了,一开始尝试RC4算法生成的密文在java中解密不出来,放弃了,改用AES。

js代码

import aes from 'crypto-js/aes';
import base from 'crypto-js/enc-base64';
	function encrypt(plaintext: string) {
		const iv = base.parse('ZGYyMzRzZDIzNDEyMzU2Zw==');
		const key = base.parse('ZGYyMzRzZGRzYWVxZWZ2ZA==');
		// 如果.toString()改成.ciphertext.toString(),则java解密时要使用HexUtil.decode,否则使用BASE64Util.decode
		return aes.encrypt(plaintext, key, {iv: iv}).toString();
	}

java代码

import com.google.api.client.repackaged.org.apache.commons.codec.binary.Base64;

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

/**
 * AES工具类,密钥必须是16位字符串
 */
public class AESUtils {

	/**偏移量,必须是16位字符串*/
    private static final String IV_STRING = "df234sd23412356g";

    /**
     * 默认的密钥
     */
    public static final String DEFAULT_KEY = "df234sddsaeqefvd";


    public static String encryptData(String content) {
        byte[] encryptedBytes = null;
        try {
            byte[] byteContent = content.getBytes("UTF-8");
            // 注意,为了能与 iOS 统一
            // 这里的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 生成
            byte[] enCodeFormat = DEFAULT_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);
            // 同样对加密后数据进行 base64 编码
            String encodeStr = BASE64Util.encode(encryptedBytes);
//            String encodeStr = HexUtil.encode(encryptedBytes);
            encodeStr = encodeStr.replace("%","%25").replace("&","%26").replace("+","%2B");
            return encodeStr;
//            return parseByte2HexStr(encryptedBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String decryptData(String content) {
        try {
        	content = content.replace("%25","%").replace("%26","&").replace("%2B","+");
            // base64 解码
            byte[] encryptedBytes = BASE64Util.decode(content);
//        	byte[] encryptedBytes = HexUtil.decode(content);
//        	byte[] encryptedBytes = parseHexStr2Byte(content);
            byte[] enCodeFormat = DEFAULT_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) {
            e.printStackTrace();
        }
        return null;
    }
    
    

    public static void main(String[] args) {
        byte[] bytes = IV_STRING.getBytes();
        String base64Str = Base64.encodeBase64String(bytes);
        // js 中的iv用这个base64Str
        System.out.println(base64Str);

        bytes = DEFAULT_KEY.getBytes();
         base64Str = Base64.encodeBase64String(bytes);
        // js 中的key用这个base64Str
        System.out.println(base64Str);
        System.out.println("用户加密"+AESUtils.encryptData("《湖北省能源发展“十四五”规划》提出,适度超前推进充电础基设施建设,到2025年全省充电桩达到50万个以上。"));
        System.out.println("用户解密"+AESUtils.decryptData("HYezy8lsT3ql6uAZxm/7vBCZAI0kxUpvny9tJLRbMJg/Mr4dCalEqC19z8Yl21tEuEdzGWTqTjIASOqx/FL+XimSZt1seFUpJrJ5E/2XorWwFZNMXKSURFdw8wV7savQJbVaTwZuJ4yML6ok1VwQ8y8yAqmr0TG38FLovyfEhQcVVOx0+UmQdsOLpERuYQDE9Pw216beyhml1BZNDhC91Q=="));
    }
    
    
}

上面用到的BASE64Util.decode为

	public static byte[] decode(String base64str) throws Exception { //
		byte[] b=base64str.getBytes();
        Base64 base64=new Base64();
        b=base64.decode(b);
		return b;
	}

HexUtil.decode为

    public static byte[] decode(String src) {
        int m = 0, n = 0;
        int byteLen = src.length() / 2;
        byte[] ret = new byte[byteLen];
        for (int i = 0; i < byteLen; i++) {
            m = i * 2 + 1;
            n = m + 1;
            int intVal = Integer.decode("0x" + src.substring(i * 2, m) + src.substring(m, n));
            ret[i] = Byte.valueOf((byte)intVal);
        }
        return ret;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值