EncryptUtil.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import crypto from 'crypto'
  2. // MD5加密
  3. function EncryptMD5 (data) {
  4. const encryptMD5 = crypto.createHash('md5')
  5. encryptMD5.update(data)
  6. return encryptMD5.digest('hex')
  7. }
  8. // DES加密、解密,用来加密信息字段,无需App端解密
  9. // 私有方法
  10. function cipheriv (en, code, data) {
  11. const buf1 = en.update(data, code)
  12. const buf2 = en.final()
  13. const r = Buffer.alloc(buf1.length + buf2.length)
  14. buf1.copy(r)
  15. buf2.copy(r, buf1.length)
  16. return r
  17. }
  18. // DES加密
  19. function DESEncrypt (data, key, vi) {
  20. return cipheriv(crypto.createCipheriv('des', key, vi), 'utf8', data).toString('base64')
  21. }
  22. // DES解密
  23. function DESDecrypt (data, key, vi) {
  24. return cipheriv(crypto.createDecipheriv('des', key, vi), 'base64', data).toString('utf8')
  25. }
  26. // AES加密
  27. function AESEncrypt (data, secretKey) {
  28. const cipher = crypto.createCipheriv('aes-128-ecb', secretKey)
  29. return cipher.update(data, 'utf8', 'hex') + cipher.final('hex')
  30. }
  31. // AES 解密
  32. function AESDecrypt (data, secretKey) {
  33. const cipher = crypto.creatDecipher('aes-128-ecb', secretKey)
  34. return cipher.update(data, 'hex', 'utf8') + cipher.final('utf8')
  35. }
  36. export {
  37. EncryptMD5,
  38. DESEncrypt,
  39. DESDecrypt,
  40. AESEncrypt,
  41. AESDecrypt
  42. }