如果不是跟c++对接,很多加密算法搞成java的人是很不容易接触到的,因为只需要调用jar就可以了。但是node侧就需要自己实现一些了
1 ECB算法
下面的算法正常貌似没有什么问题,但实际在解密的时候,经常出现有些c++加密的数据,在js侧却解不出来。

const CryptoJS = require('crypto-js');
const mode = CryptoJS.mode.ECB
const padding = CryptoJS.pad.NoPadding
/*** 解密方法* @param: str 需要解密的字符* @param: key 密钥* @param: iv 密钥偏移量*/
function decrypt(str, key, iv) {const keyStr = key ? encParse(key) : encParse(defaultKey);const ivStr = iv ? encParse(iv) : encParse(defaultIv);// 判断str是否为base64,如果不是就要转base64,是了就不能再转const flag = isBase64(str);if (!flag) {// 转为base64之前要先转16进制str = CryptoJS.enc.Hex.parse(str);// 只有base64格式的字符才能被解密str = CryptoJS.enc.Base64.stringify(str);}const encryptedStr = CryptoJS.AES.decrypt(str, keyStr, {// iv: ivStr,mode: mode,padding: padding});return encryptedStr.toString(CryptoJS.enc.Utf8);
}
/*** 处理密钥字符格式* @param: key 需要转格式的字符*/
function encParse(key) {return CryptoJS.enc.Utf8.parse(key);// return CryptoJS.enc.Latin1.parse(key);
}

但在C++后台对接的时候,9条数据,有两条解密不了提示,这是什么原因呢?

VM2109 WAService.js:2 Error: Malformed UTF-8 dataat Object.stringify (tripledes.js:450)at WordArray.init.toString (tripledes.js:205)at Object.decrypt (aes_util.js? [sm]:100)at Ze.jiemi (index.js? [sm]:53)at Object.o.safeCallback (VM2109 WAService.js:2)at VM2109 WAService.js:2at s (VM2109 WAService.js:2)at VM2109 WAService.js:2at v (VM2109 WAService.js:2)at VM2109 WAService.js:2(env: Windows,mp,1.06.2206090; lib: 2.17.0)

使用AES在线,是可以解密的

使用crypto-js不行,这个js库还是有关系,这个js端无法处理,只能从源头开始处理,将·回车键(换行符)去掉后,再加密后,传到前端,问题就可以解决了
今天想到一种更好的解决办法,灵感来自微信小程序解密手机号的cbc算法。

function decrypt(str, key, iv) {iv = iv || "";var clearEncoding = 'utf8';var cipherEncoding = 'base64';var cipherChunks = [];var decipher = crypto.createDecipheriv('aes-256-ecb', key, iv);decipher.setAutoPadding(false);cipherChunks.push(decipher.update(str, cipherEncoding, clearEncoding));cipherChunks.push(decipher.final(clearEncoding));let decoded = cipherChunks.join('');return decoded.toString(CryptoJS.enc.Utf8)}

2 CBC算法
这个是微信小程序官方解密手机号的写法,依然是要安装crypto-js,看来crypto-js以来crypto

var crypto = require('crypto')function WXBizDataCrypt(appId, sessionKey) {this.appId = appIdthis.sessionKey = sessionKey
}WXBizDataCrypt.prototype.decryptData = function (encryptedData, iv) {// base64 decodevar sessionKey = new Buffer(this.sessionKey, 'base64')encryptedData = new Buffer(encryptedData, 'base64')iv = new Buffer(iv, 'base64')try {// 解密var decipher = crypto.createDecipheriv('aes-128-cbc', sessionKey, iv)// 设置自动 padding 为 true,删除填充补位decipher.setAutoPadding(true)var decoded = decipher.update(encryptedData, 'binary', 'utf8')decoded += decipher.final('utf8')decoded = JSON.parse(decoded)} catch (err) {throw new Error('Illegal Buffer')}if (decoded.watermark.appid !== this.appId) {throw new Error('Illegal Buffer')}return decoded
}module.exports = WXBizDataCrypt

3 3DES 2倍长
nodejs实现3des(2倍长)加密方式,与DES加密工具一致
这里稍作了改动,它的程序实际应用有点小问题

const crypto = require('crypto');
const tools = require('./cgw');const algorithm = { ecb: 'des-ecb' }function encrypt(plaintext, keytext) {let key = new Uint8Array(keytext);let iv = new Uint8Array("");let txt = new Uint8Array(plaintext)let cipher = crypto.createCipheriv(algorithm.ecb, key, iv);cipher.setAutoPadding(true);let ciph = cipher.update(txt);return ciph;
}function decrypt(encrypttext, keytext) {let key = new Uint8Array(keytext);let iv = new Uint8Array("");let txt = new Uint8Array(encrypttext);let decipher = crypto.createDecipheriv(algorithm.ecb, key, iv);decipher.setAutoPadding(true);decipher.update(txt);let ciph = decipher.update(txt);return ciph;
}/*** 补00* @param {*} p */
function fillZero(p){let nAddCount = (16-p.length%16)/2;for (let i=0; i<nAddCount; i++){p += '00';}return p;
}/*** 规则是先加80,后面凑齐16的整数倍补00* 3DES双倍长* @param {*} src * @param {*} key */
function encrypt3Des(src, key) {src = tools.StringToAscii(src)+'80'src = fillZero(src)let temp = null;let temp1 = null;temp1 = encrypt(tools.HexStringToBytes(src), tools.HexStringToBytes(key.substring(0, 16)));temp = decrypt(temp1, tools.HexStringToBytes(key.substring(16, 32)));temp1 = encrypt(temp, tools.HexStringToBytes(key.substring(0, 16)));return tools.BytesToHexString(temp1);
}function decrypt3Des(src, key) {let temp = null;let temp1 = null;temp1 = decrypt(tools.HexStringToBytes(src), tools.HexStringToBytes(key.substring(0, 16)));temp = encrypt(temp1, tools.HexStringToBytes(key.substring(16, 32)));temp1 = decrypt(temp, tools.HexStringToBytes(key.substring(0, 16)));return tools.BytesToHexString(temp1);
}export default {encrypt3Des,decrypt3Des
}
var bytesToHexString = function (src) {if (src == null || src.length <= 0) {return null;}let d = "";for (let i = 0; i < src.length; i++) {let v = src[i] & 0xFF;let hv = intTo16value(v);d += hv;}let strs = []// 字符串每8位进行分隔for (let j=0; j<d.length/16; j++){strs.push(d.slice(j*16,(j+1)*16))}// return strs.reverse().join('')
}

crypto-js的使用相关推荐

  1. 微信小程序使用crypto.js加密解密

    微信小程序中使用crypto.js crypto.js是用来进行AES加密的 注意AES在使用时有7个配置项,前后端加解密记着统一参数,测试时注意配置项的选择是否一致. 测试工具: AES加密测试工具 ...

  2. 【crypto】基于crypto.js的web前端加解密系统实现

    文章目录 一.概念介绍 1.1 crypto.js介绍 1.2 加密 / 哈希 / 编码 1.2.1 加密/解密 1.2.2 散列/哈希 1.2.3 BASE64编解码 二.加解密思想(以md5为例) ...

  3. angular使用crypto.js加密

    MD5加密 过程: 1.下载 npm install ts-md5 --save 2.在需要的组件引入 import { Md5 } from 'ts-md5'; 3.使用 console.log(M ...

  4. 前端使用crypto.js进行加密学习记录

    因为我的需求是加密可逆,具有一定的安全性(对安全性要求不高),所以使用DES或AES即可,我用的是AES js下载 下载 Nodejs后端:对用户密码进行加密 - WJY- - 博客园Nodejs后端 ...

  5. C# 中用DES 对称Key,IV 加密,前端crypto.js 解密

    1.服务器端代码 #region ========加密======== /// <summary> /// 加密 /// </summary> /// <param na ...

  6. 【安全与协议】使用crypto.js进行加密详解

    JavaScript Crypto-JS 前言与工具 → 前言 使用 Crypto-JS 可以非常方便地在 JavaScript 进行 MD5.SHA1.SHA2.SHA3.RIPEMD-160 哈希 ...

  7. 前端用crypto.js进行加密和解密

    import CryptoJS from 'crypto-js' function getAesString(data,key,iv){//加密var key = CryptoJS.enc.Utf8. ...

  8. python aes padding_使用PKCS7Padding在python和Node.js之间进行AES加密

    已解决 密码学Python 使用PKCS7Padding在python和Node.js之间进行AES加密10 我试图用Node.js中的pkcs7填充来解密消息没有成功. 此消息已加密并从Python ...

  9. aes js 加盐值 解密_cryptoJS AES 加解密简单使用

    简单记录一下,前端利用 cryptoJS 如何加解密的.主要是关于 AES 加解密. 需求描述:需要对 url 中的参数进行 AES 解密,然后再把该参数进行 MD5 加密通过接口传递. AES AE ...

  10. html+css+js 做一个加解密小网页

    孩子第一次做网页,比较菜,勿喷 本来是想做一个ctf的知识站和加解密站当作业交上去,但是时间太短了,又要准备高数和大雾简直人都要没了,就只写了base64,凯撒,和栅栏密码 html <!DOC ...

最新文章

  1. 关于redis的几件小事(三)redis的数据类型与使用场景
  2. vaspkit使用_VASPKIT校正气体分子自由能
  3. 【错误记录】Mac 中 IntelliJ IDEA 运行 Python 程序报错 ( “Python“ 因为出现问题而无法打开 )
  4. CSS中position属性(abusolute | relative | static | fixed)区别
  5. [设计模式] ------ 策略模式
  6. 虚拟专题:知识图谱 | 知识图谱多跳问答推理研究进展、挑战与展望
  7. hp-ux ftp启动_您可以做12项免费的事情来快速启动UX设计事业
  8. shell第四次练习
  9. redis-shake数据同步迁移工具
  10. 区块链 solidity io密集
  11. 开启Github之旅
  12. ADSL共享上网方式大总结(图)
  13. “李记餐厅”微信点餐小程序的设计与实现
  14. Linux查看日志的几种方式
  15. 最长重复子字符串:123321||12321
  16. 如何恢复计算机隐藏的文件夹,隐藏文件夹,小编教你电脑隐藏文件夹怎么恢复...
  17. 自制hdmi线一头改vga图_杀鸡取卵 | 破拆电脑VGA电缆获取收音机天线零件:双目铁氧体磁芯...
  18. 四层负载均衡 动静分离和资源分离 Rewrite rewrite伪静态实例
  19. 计算机存储器的有关术语,关于计算机存储器,不正确的描述是()。
  20. 大数据所见即所得,人机共同进化之端倪

热门文章

  1. 网络安全-终端防护设备
  2. 机器学习系列:大规模机器学习(Large Scale Machine Learning)
  3. java 打印日历
  4. 有关三门问题的答案解释
  5. Python:奇数之和
  6. android 2048的简单实现
  7. 用计算机指挥挖掘机炒菜,幽默段子:我能用计算机控制挖掘机炒菜……
  8. windows主机跟linux主机对比
  9. 锁存器、触发器、寄存器
  10. 医用温度传感器的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告