2025-07-30 09:13:52 +08:00
|
|
|
|
import Base64 from 'crypto-js/enc-base64'
|
|
|
|
|
import UTF8 from 'crypto-js/enc-utf8'
|
|
|
|
|
import { JSEncrypt } from 'jsencrypt'
|
|
|
|
|
import md5 from 'crypto-js/md5'
|
|
|
|
|
import CryptoJS from 'crypto-js'
|
|
|
|
|
|
|
|
|
|
export function encodeByBase64(txt: string) {
|
|
|
|
|
return UTF8.parse(txt).toString(Base64)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function decodeByBase64(txt: string) {
|
|
|
|
|
return Base64.parse(txt).toString(UTF8)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function encryptByMd5(txt: string) {
|
|
|
|
|
return md5(txt).toString()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const publicKey
|
|
|
|
|
= 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAM51dgYtMyF+tTQt80sfFOpSV27a7t9u'
|
2025-08-04 17:10:11 +08:00
|
|
|
|
+ 'aUVeFrdGiVxscuizE7H8SMntYqfn9lp8a5GH5P1/GGehVjUD2gF/4kcCAwEAAQ=='
|
2025-07-30 09:13:52 +08:00
|
|
|
|
|
|
|
|
|
export function encryptByRsa(txt: string) {
|
|
|
|
|
const encryptor = new JSEncrypt()
|
|
|
|
|
encryptor.setPublicKey(publicKey) // 设置公钥
|
|
|
|
|
return encryptor.encrypt(txt) // 对数据进行加密
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* AES加密
|
|
|
|
|
* @param word 要加密的密码
|
|
|
|
|
* @param account 账号,用于生成加密密钥
|
|
|
|
|
* @returns 加密后的字符串
|
|
|
|
|
*/
|
|
|
|
|
export function encryptByAes(word: string, account: string) {
|
|
|
|
|
// 对账号做md5计算,然后取8-24位作为密钥(16个字符)
|
|
|
|
|
const accountMd5 = md5(account).toString()
|
|
|
|
|
const keyWord = accountMd5.substring(8, 24) // 取8-24位(索引8-23,共16位)
|
2025-08-04 17:10:11 +08:00
|
|
|
|
|
2025-07-30 09:13:52 +08:00
|
|
|
|
const key = CryptoJS.enc.Utf8.parse(keyWord)
|
|
|
|
|
const arcs = CryptoJS.enc.Utf8.parse(word)
|
|
|
|
|
const encrypted = CryptoJS.AES.encrypt(arcs, key, {
|
|
|
|
|
mode: CryptoJS.mode.ECB,
|
|
|
|
|
padding: CryptoJS.pad.Pkcs7,
|
|
|
|
|
})
|
|
|
|
|
return encrypted.toString()
|
|
|
|
|
}
|