Blame view

merchant-web-master/src/utils/jiami.js 2.39 KB
b12ba7ef   杨鑫   '最新'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  // 文本加密
  import { sm4 } from 'sm-crypto';
  const DEFAULT_KEY ="cda4442f102f6396eea76902e37ad7cb";
  const DEFAULT_IV = "8bd8a83221742111c7532b7275a7fe9c";
  export const encrypt = (str) => {
      if(!str) {
          return ''
      }
      const encryptStr = sm4.encrypt(str, DEFAULT_KEY, {
          iv: DEFAULT_IV,
          mode: 'cbc',
          padding: 'pkcs#7'
          })
      return encryptStr
  }
  
  // 文本解密
  export const decrypt = (str) => {
      if(!str) {
          return ''
      }
  //     if(str.indexOf('ENC(') === -1) {
  //         return str
  //     }
  //     const str_ = str.replace('ENC(', '').replace(')', '')
      try {        
          const decryptStr = sm4.decrypt(str, DEFAULT_KEY, {
              iv: DEFAULT_IV,
              mode: 'cbc',
              padding: 'pkcs#7'
          })
          return decryptStr
      } catch (error) {
          return '解密失败!'
      }
  }
  // 批量加密
  export const decryptall = (obj,isjmlist) => {
  
  
      let list  = []
      let objnew = {}
      for (let key in obj) {
          let item = {
              typeis:'2',//是否加密 1加密 2不加密
              keyname:key,//变量名
              value:obj[key],//值
          }
          if(isjmlist.length> 0) {
  
              // let found = isjmlist.find(function(element) {
              //     return key == element;
              // });
              console.log(isjmlist.indexOf(key) != -1)
              if(isjmlist.indexOf(key) != -1) {
                  item.typeis = '1'
              }
          } else {
              item.typeis = '1'
          }
  
          list.push(item)
      }
  
  
      list.forEach(element => {
          if(element.typeis == '1' && element.value) {
              console.error(element)
              element.value = encrypt(element.value)
          }
          objnew[element.keyname] = element.value
      });
  
      return objnew
  }
  // 批量解密
  export const encryptall = (list,isjmlist) => {
  
  
      let newlist =  list.map(element => {
          let c1 =  Object.keys(element)
          c1.forEach(key => {
              if(isjmlist.length> 0) {
                  console.log(isjmlist.indexOf(key) != -1)
                  if(isjmlist.indexOf(key) != -1) {
                      element[key] = decrypt(element[key])
                  }
              } else {
                  element[key] = decrypt(element[key])
              }
          });
          return element
      });
  
      return newlist
  }