小程序
小游戏
企业微信
微信支付
扫描小程序码分享
我项目上要求使用des加密算法 加密的数据为数组类型 使用crypto.js谷歌开发的加密算法库只支持字符串类型的数据加密 我不知道该怎么办了
1 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
var key = '2018122511233000'; // 时间戳 let message = '0100020004020000'; // 卡号 function hex1int(hex) { var len = hex.length, a = new Array(len), code; var code1 = '' var code2 = '' for (var i = 0; i < len; i++) { // code1 = '' // code2 = '' // code = '' code1 = hex.charCodeAt(i); if (48 <= code || code < 58) { code1 = (code1 - 48) * 16; } i++; code2 = hex.charCodeAt(i); if (48 <= code2 || code2 < 58) { code2 = (code2 - 48); } code = code2; a[i] = code; } console.log("转16进制结果:" + a); that.setData({ byte16: a }) console.log(that.data.byte16); } hex1int(message); // 转16进制 function hex2int(hex) { var len = hex, lens = 2; console.log(len); for (var i = 0; i < len.length; i += lens) { console.log("截取后:" + len.substring(i, i + lens)); let str = len.substring(i, i + lens); // console.log(str); // string let num = parseInt(str); console.log(num); // number that.data.des.push("0x" + num); console.log(that.data.des); var turnNum = function (nums) { for (let i = 0; i < nums.length; i++) { nums[i] = parseInt(nums[i]) } return nums; } console.log(turnNum(that.data.des)); } let str = JSON.stringify(that.data.des); console.log(str); console.log(encryptByDESModeEBC(JSON.stringify(that.data.byte16),str)); } hex2int(key); // 加密方法 传入明文的uint8数组 // function encryptUint8Arry(uint8array) { // var wordArray = CryptoJS.enc.Uint8Array.parse(uint8array); // console.log(wordArray); // var encrypted = CryptoJS.DES.encrypt(wordArray, keyWA, { // iv: ivWA, // mode: CryptoJS.mode.CBC, // padding: CryptoJS.pad.Pkcs7 // }); // var bv = CryptoJS.enc.Uint8Array.stringify(encrypted.ciphertext); // return bv; // } // console.log(encryptUint8Arry(that.data.des)); // console.log(encryptByDESModeEBC(that.data.des,key)) //DES ECB模式加密 function encryptByDESModeEBC(message, key) { var keyHex = CryptoJS.enc.Utf8.parse(key); var encrypted = CryptoJS.DES.encrypt(message, keyHex, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); return encrypted.ciphertext.toString(); }
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
var key = '2018122511233000'; // 时间戳 let message = '0100020004020000'; // 卡号 function hex1int(hex) { var len = hex.length, a = new Array(len), code; var code1 = '' var code2 = '' for (var i = 0; i < len; i++) { // code1 = '' // code2 = '' // code = '' code1 = hex.charCodeAt(i); if (48 <= code || code < 58) { code1 = (code1 - 48) * 16; } i++; code2 = hex.charCodeAt(i); if (48 <= code2 || code2 < 58) { code2 = (code2 - 48); } code = code2; a[i] = code; } console.log("转16进制结果:" + a); that.setData({ byte16: a }) console.log(that.data.byte16); } hex1int(message); // 转16进制 function hex2int(hex) { var len = hex, lens = 2; console.log(len); for (var i = 0; i < len.length; i += lens) { console.log("截取后:" + len.substring(i, i + lens)); let str = len.substring(i, i + lens); // console.log(str); // string let num = parseInt(str); console.log(num); // number that.data.des.push("0x" + num); console.log(that.data.des); var turnNum = function (nums) { for (let i = 0; i < nums.length; i++) { nums[i] = parseInt(nums[i]) } return nums; } console.log(turnNum(that.data.des)); } let str = JSON.stringify(that.data.des); console.log(str); console.log(encryptByDESModeEBC(JSON.stringify(that.data.byte16),str)); } hex2int(key); // 加密方法 传入明文的uint8数组 // function encryptUint8Arry(uint8array) { // var wordArray = CryptoJS.enc.Uint8Array.parse(uint8array); // console.log(wordArray); // var encrypted = CryptoJS.DES.encrypt(wordArray, keyWA, { // iv: ivWA, // mode: CryptoJS.mode.CBC, // padding: CryptoJS.pad.Pkcs7 // }); // var bv = CryptoJS.enc.Uint8Array.stringify(encrypted.ciphertext); // return bv; // } // console.log(encryptUint8Arry(that.data.des)); // console.log(encryptByDESModeEBC(that.data.des,key)) //DES ECB模式加密 function encryptByDESModeEBC(message, key) { var keyHex = CryptoJS.enc.Utf8.parse(key); var encrypted = CryptoJS.DES.encrypt(message, keyHex, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); return encrypted.ciphertext.toString(); }