中品云站
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.7 KiB

2 years ago
import CryptoJS from 'crypto-js'
import md5 from 'js-md5'
// let Base64 = require('js-base64').Base64
// import { JSEncrypt } from 'jsencrypt'
var keyStr = 'qDfajQ*v@W1mCruZ'
export default {
/**
* @param {*需要加密的字符串 对象转化为json字符串再加密} word
* @param {*aes加密需要的key值这个key值后端同学会告诉你} keyStr
*/
encrypt(word) { // 加密
var key = CryptoJS.enc.Utf8.parse(keyStr)
var srcs = CryptoJS.enc.Utf8.parse(word)
var encrypted = CryptoJS.AES.encrypt(srcs, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
}) // 加密模式为ECB,补码方式为PKCS5Padding(也就是PKCS7)
return encrypted.toString()
},
decrypt(word) { // 解密
var key = CryptoJS.enc.Utf8.parse(keyStr)
// var srcs = CryptoJS.enc.Utf8.parse(word)
if (word) {
word = word.replace(/[\r\n]/g, "")
}
var decrypt = CryptoJS.AES.decrypt(word, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
})
return CryptoJS.enc.Utf8.stringify(decrypt).toString()
},
// decrypt (word) {
// var key = CryptoJS.enc.Utf8.parse(keyStr)
// var encryptedHexStr = CryptoJS.enc.Hex.parse(word);
// var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
// var decrypt = CryptoJS.AES.decrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
// return decrypt.toString(CryptoJS.enc.Utf8).toString();
// },
// encrypt(data) {
// // 新建JSEncrypt对象
// let encryptor = new JSEncrypt();
// // 设置公钥
// encryptor.setPublicKey(keyStr);
// // 加密数据
// return encryptor.encrypt(data);
// },
// // 解密
// decrypt(data) {
// // 新建JSEncrypt对象
// let decrypt = new JSEncrypt();
// // 设置私钥
// decrypt.setPrivateKey(keyStr);
// // 解密数据
// return decrypt.decrypt(secretWord);
// },
md5Salt(str) {
// console.log('hahahahaha',str, str + "Do&9hY%l8e", md5(str + 'Do&9hY%l8e'))
// str ='fd3fc615-3554-4ff5-8848-364577e8687d'
// console.log('hahahahaha', str, str + "kdq*&qflbn1gga?aDq", md5(str + 'kdq*&qflbn1gga?aDq'))
// console.log('\n\n------ begin: ------')
// console.log("md5(str + 'kdq*&qflbn1gga?aDq')", md5(str + 'kdq*&qflbn1gga?aDq'))
// console.log('------ end: ------\n\n')
return md5(str + 'kdq*&qflbn1gga?aDq')
},
md5NoSalt(str) {
return md5(str)
},
uuid() { // 获取uuid
var s = []
var hexDigits = '0123456789abcdef'
for (var i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
}
s[14] = '4'
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1)
s[8] = s[13] = s[18] = s[23] = '-'
var uuid = s.join('')
return uuid
}
}