1.npm i crypto-js

2.import CryptoJS from ‘crypto-js’

3.// des加密(这种方法没有密文)

export const encryptDes=(message, key)=>{
  var keyHex = CryptoJS.enc.Utf8.parse(key);
  var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  });
  return encrypted.toString();
}

// des加密(这种方法有密文)
export const encryptDes=(message, key)=>{
  var keyHex = CryptoJS.enc.Utf8.parse(key);
  var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  });
  return encrypted.ciphertext.toString();
}

4.// DES解密

export const decryptDes=(ciphertext, key)=>{
  const keyHex = CryptoJS.enc.Utf8.parse(key);
  // direct decrypt ciphertext
  const decrypted = CryptoJS.DES.decrypt({
    ciphertext: CryptoJS.enc.Base64.parse(ciphertext)
  }, keyHex, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
  return decrypted.toString(CryptoJS.enc.Utf8);
}

5.在相应的js文件里引入

列如:import { encryptDes,decryptDes } from ‘common/config/des’

Logo

前往低代码交流专区

更多推荐