1
This commit is contained in:
159
nx/utils/crypto.js
Normal file
159
nx/utils/crypto.js
Normal file
@@ -0,0 +1,159 @@
|
||||
import CryptoJS from 'crypto-js';
|
||||
import { SM4 } from 'gm-crypto';
|
||||
import { apiCrypto } from '@/nx/config'
|
||||
|
||||
|
||||
/**
|
||||
* api报文加密
|
||||
*/
|
||||
export class ApiEncryption {
|
||||
constructor() {
|
||||
this.defaultEncType = apiCrypto.type
|
||||
this.aesKey = apiCrypto.aesKey
|
||||
this.desKey = apiCrypto.desKey
|
||||
this.sm4Key = apiCrypto.sm4Key
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密数据
|
||||
* @param data 待加密的数据
|
||||
* @returns
|
||||
*/
|
||||
encrypt(data) {
|
||||
let encryptData
|
||||
switch (this.defaultEncType) {
|
||||
case 'aes':
|
||||
encryptData = this.encryptAES(data, this.aesKey)
|
||||
break
|
||||
case 'des':
|
||||
encryptData = this.encryptDES(data, this.desKey)
|
||||
break
|
||||
case 'sm4':
|
||||
encryptData = this.encryptSM4(data, this.sm4Key)
|
||||
break
|
||||
default:
|
||||
encryptData = this.encryptAES(data, this.aesKey)
|
||||
break
|
||||
}
|
||||
return encryptData
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密
|
||||
* @param data 待解密的数据
|
||||
* @returns
|
||||
*/
|
||||
decrypt(data) {
|
||||
let decryptData
|
||||
switch (this.defaultEncType) {
|
||||
case 'aes':
|
||||
decryptData = this.decryptAES(data, this.aesKey)
|
||||
break
|
||||
case 'des':
|
||||
decryptData = this.decryptDES(data, this.desKey)
|
||||
break
|
||||
case 'sm4':
|
||||
decryptData = this.decryptSM4(data, this.sm4Key)
|
||||
break
|
||||
default:
|
||||
decryptData = this.decryptAES(data, this.aesKey)
|
||||
break
|
||||
}
|
||||
return decryptData
|
||||
}
|
||||
|
||||
/**
|
||||
* aes加密
|
||||
* @param data 待加密数据
|
||||
* @param key aes密钥
|
||||
* @returns
|
||||
*/
|
||||
encryptAES(data, key) {
|
||||
const dataBytes = CryptoJS.enc.Utf8.parse(data)
|
||||
const keyBytes = CryptoJS.enc.Utf8.parse(key)
|
||||
const encrypted = CryptoJS.AES.encrypt(dataBytes, keyBytes, {
|
||||
iv: keyBytes,
|
||||
mode: CryptoJS.mode.CBC,
|
||||
padding: CryptoJS.pad.Pkcs7
|
||||
})
|
||||
return CryptoJS.enc.Base64.stringify(encrypted.ciphertext)
|
||||
}
|
||||
|
||||
/**
|
||||
* aes解密
|
||||
* @param data 待解密数据
|
||||
* @param key 密钥key
|
||||
* @returns
|
||||
*/
|
||||
decryptAES(data, key) {
|
||||
const keyBytes = CryptoJS.enc.Utf8.parse(key)
|
||||
const decrypted = CryptoJS.AES.decrypt(data, keyBytes, {
|
||||
iv: keyBytes,
|
||||
mode: CryptoJS.mode.CBC,
|
||||
padding: CryptoJS.pad.Pkcs7
|
||||
})
|
||||
return CryptoJS.enc.Utf8.stringify(decrypted)
|
||||
}
|
||||
|
||||
/**
|
||||
* des加密
|
||||
* @param data 待加密数据
|
||||
* @param key des密钥
|
||||
* @returns
|
||||
*/
|
||||
encryptDES(data, key) {
|
||||
const keyBytes = CryptoJS.enc.Utf8.parse(key)
|
||||
const encrypted = CryptoJS.DES.encrypt(data, keyBytes, {
|
||||
mode: CryptoJS.mode.ECB,
|
||||
padding: CryptoJS.pad.Pkcs7
|
||||
})
|
||||
return encrypted.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* des解密
|
||||
* @param data 待解密数据
|
||||
* @param key des密钥
|
||||
* @returns
|
||||
*/
|
||||
decryptDES(data, key) {
|
||||
const keyBytes = CryptoJS.enc.Utf8.parse(key)
|
||||
//const cipherText = CryptoJS.enc.Base64.parse(data);
|
||||
const decrypted = CryptoJS.DES.decrypt(data, keyBytes, {
|
||||
mode: CryptoJS.mode.ECB,
|
||||
padding: CryptoJS.pad.Pkcs7
|
||||
})
|
||||
return decrypted.toString(CryptoJS.enc.Utf8)
|
||||
}
|
||||
|
||||
/**
|
||||
* sm4加密
|
||||
* @param data 待加密数据
|
||||
* @param key sm4密钥
|
||||
*/
|
||||
encryptSM4(data, key) {
|
||||
const keyBytes = CryptoJS.enc.Utf8.parse(key)
|
||||
const keyHex = CryptoJS.enc.Hex.stringify(keyBytes)
|
||||
//console.log(data);
|
||||
//console.log(keyHex);
|
||||
return SM4.encrypt(data, keyHex, {
|
||||
inputEncoding: 'utf8',
|
||||
outputEncoding: 'base64'
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* sm4解密
|
||||
* @param data 待解密数据
|
||||
* @param key sm4密钥
|
||||
*/
|
||||
decryptSM4(data, key) {
|
||||
const keyBytes = CryptoJS.enc.Utf8.parse(key)
|
||||
const keyHex = CryptoJS.enc.Hex.stringify(keyBytes)
|
||||
|
||||
return SM4.decrypt(data, keyHex, {
|
||||
inputEncoding: 'base64',
|
||||
outputEncoding: 'utf8'
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user