-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from vusion/task-2820863925175808-cw_cryptojs_…
…library-hanshijie-240312 feat: 新增des解密方法。
- Loading branch information
Showing
5 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
- name: decryptByDes | ||
description: Des解密 | ||
type: both | ||
belong: logic | ||
labels: [Runtime] |
44 changes: 44 additions & 0 deletions
44
packages/cw/cw_cryptojs_library/logics/decryptByDes/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* @param {string} data <true> 需要解密的内容 | ||
* @param {string} key <true> 加密的key | ||
* @param {string} iv <true> 加密的iv | ||
* @returns {string} result | ||
*/ | ||
import CryptoJS from 'crypto-js'; | ||
export default (data, key, iv) => { | ||
function hex2str(hex) { | ||
if (!hex) { | ||
return ''; | ||
} | ||
let trimedStr = hex.trim(); | ||
let rawStr = | ||
trimedStr.substr(0, 2).toLowerCase() === '0x' | ||
? trimedStr.substr(2) | ||
: trimedStr; | ||
let len = rawStr.length; | ||
if (len % 2 !== 0) { | ||
alert('非法格式的ASCII代码!'); | ||
return ''; | ||
} | ||
let curCharCode; | ||
let resultStr = []; | ||
for (let i = 0; i < len; i = i + 2) { | ||
curCharCode = parseInt(rawStr.substr(i, 2), 16); | ||
resultStr.push(String.fromCharCode(curCharCode)); | ||
} | ||
return resultStr.join(''); | ||
} | ||
if (data === undefined) { | ||
return data; | ||
} | ||
// console.log(CryptoJS); | ||
const keyHex = CryptoJS.enc.Utf8.parse(hex2str(key)); | ||
const ivHex = CryptoJS.enc.Utf8.parse(hex2str(iv)); | ||
const decrypted = CryptoJS.TripleDES.decrypt(data, keyHex, { | ||
iv: ivHex, | ||
mode: CryptoJS.mode.CBC, | ||
padding: CryptoJS.pad.Pkcs7, | ||
}); | ||
return decrypted.toString(CryptoJS.enc.Utf8); | ||
// TODO | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters