Uniapp development uses des to encrypt and decrypt data

npm install crypto-js or download the js file of crypto-js to import

Encapsulate a des.js
download address:
https://down.chinaz.com/soft/40271.htm

insert image description here
After encapsulation, call the method in the des.js file on the page to encrypt and decrypt

// An highlighted block
// des.js
import CryptoJS from './crypto-js.js'
// 秘钥
export function getKey(){
    
    
	return  CryptoJS.enc.Utf8.parse('48CDAB')
}
// 偏移量
export function getIv(){
    
    
	return CryptoJS.enc.Utf8.parse('0672DE')
}
/*加密*/
export function encrypt(data) {
    
    
  const message = CryptoJS.enc.Utf8.parse(data);
  var ciphertext = CryptoJS.DES.encrypt(message, getKey(), {
    
    
    iv: getIv(),
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  return ciphertext.toString();
}
/*解密*/
export function decrypt(data) {
    
    
  var bytes = CryptoJS.DES.decrypt(data.toString(), getKey(), {
    
    
    iv: getIv(),
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  return bytes.toString(CryptoJS.enc.Utf8);
}


Import des.js

//引入des
	import {
    
    decrypt,encrypt} from '@/utils/des.js'

use

// 页面调用 解密
this.hostIP = decrypt(json["siphost"]);

おすすめ

転載: blog.csdn.net/iOS_MingXing/article/details/126290296