码迷,mamicode.com
首页 > 编程语言 > 详细

Nodejs与Java通用AES加解密

时间:2019-04-02 17:01:14      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:system   ring   tle   edit   tag   stack   mod   algorithm   col   

一、nodejs加解密工具类:

/**
* AES util
*/
const CryptoJS = require(‘crypto-js‘)
export const AES = {
    //加密
    encode: (params) => {
        let { data, param, key } = params
        let result = JSON.parse(JSON.stringify(data))
        param.forEach(ele => {
            let data = result[ele]
            key = CryptoJS.enc.Utf8.parse(key)
            let iv = key
            let encrypted = CryptoJS.AES.encrypt(data,key,{
                iv: iv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.ZeroPadding
            })
            result[ele] = encrypted.toString();
       })
       return result
    },
    decode: (params) => {
        let { data, param, key } = params
        let result = JSON.parse(JSON.stringify(data))
        param.forEach(ele => {
            let data = result[ele]
            key = CryptoJS.enc.Utf8.parse(key)
            let iv = key
            let decrypt = CryptoJS.AES.decrypt(data, key, {
                iv: iv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.ZeroPadding
            });
            let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
            result[ele] = decryptedStr.toString();
        })
        return result
    }
}
  • 使用方法:
    (1)加密:
let user = AES.encode({
    data: {//要加密的数据
        username:‘123456‘,
        password:‘123456‘
    },
    key: ‘1234567887654321‘,//加密的key
    param: [‘password‘]//要加密哪些属性
})

(2)解密:

let user = AES.decode({
    data: {//要解密的数据
        username:‘123456‘,
        password:‘lBTqrKS0kZixOFXeZ0HRng==‘
    },
    key: ‘1234567887654321‘,
    param: [‘user‘]//要解密哪些属性
})

二、Java解密:

private static String decryptAES(String data, String pass) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/NOPadding");
    SecretKeySpec keyspec = new SecretKeySpec(pass.getBytes(), KEY_ALGORITHM);
    IvParameterSpec ivspec = new IvParameterSpec(pass.getBytes());
    cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
    byte[] result=cipher.doFinal(Base64.decode(data.getBytes(CharsetUtil.UTF_8)));
    return new String(result, CharsetUtil.UTF_8);
}
  • 使用方法:
try {
    String s = decryptAES("lBTqrKS0kZixOFXeZ0HRng==", "1234567887654321");
    System.out.println(s);
} catch (Exception e) {
    e.printStackTrace();
}

三、Java加密:

private static String encryptAES(String data,String pass) throws Exception {
    byte[] raw = pass.getBytes("utf-8");
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec iv = new IvParameterSpec(pass.getBytes());
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
    byte[] encrypted = cipher.doFinal(data.getBytes());
    return new Base64().encode(encrypted);
}
  • 使用方法:
String e=encryptAES("123456","1234567887654321");
System.out.println(e);

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">





Nodejs与Java通用AES加解密

标签:system   ring   tle   edit   tag   stack   mod   algorithm   col   

原文地址:https://www.cnblogs.com/unuai/p/cd8ff80ca06154bbed19e16d1c800283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!