crypt.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import cryptojs from 'crypto-js'
  2. var codecutil = {}
  3. export default codecutil //全局导出对象
  4. //MD5(Message-Digest Algorithm),信息摘要算法
  5. //一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用于确保信息传输完整一致
  6. codecutil.md5 = function (data) {
  7. return cryptojs.MD5(data).toString()
  8. }
  9. //SHA1(Secure Hash Algorithm 1),安全散列算法,
  10. //SHA-1可以生成一个被称为消息摘要的160位(20字节)散列值,散列值通常的呈现形式为40个十六进制数
  11. codecutil.sha1 = function (data) {
  12. return cryptojs.SHA1(data).toString()
  13. }
  14. //SHA256(Secure Hash Algorithm 1),安全散列算法,
  15. //对于任意长度的消息,SHA256都会产生一个256位的哈希值,称作消息摘要。这个摘要相当于是个长度为32个字节的数组,共256位,通常由一个长度为64的十六进制字符串来表示
  16. codecutil.sha256 = function (data) {
  17. return cryptojs.SHA256(data).toString()
  18. }
  19. //解析密钥
  20. codecutil.aesDeKey = function (key) {
  21. return cryptojs.enc.Utf8.parse(key)
  22. }
  23. //aes加密
  24. codecutil.aesEncode = function (data, key) {
  25. if (data && key) {
  26. let aesKey = cryptojs.enc.Utf8.parse(key) //将密钥按照utf8格式化
  27. let content = cryptojs.enc.Utf8.parse(data)//将明文按照utf8格式化
  28. //加密后已经是base64编码格式的数据
  29. const ciphertext = cryptojs.AES.encrypt(content, aesKey, { iv: aesKey, mode: cryptojs.mode.CBC, padding: cryptojs.pad.Pkcs7 }).toString()
  30. return ciphertext
  31. } else {
  32. return ''
  33. }
  34. }
  35. //aes解密
  36. codecutil.aesDecode = function (data, key) {
  37. if (data && key) {
  38. var ciphertext = codecutil.base64Decode(data)
  39. // var bytes = cryptojs.AES.decrypt(ciphertext, key, { mode: cryptojs.mode.ECB, padding: cryptojs.pad.Pkcs7 })
  40. var bytes = cryptojs.AES.decrypt(ciphertext, key, { mode: cryptojs.mode.CBC, padding: cryptojs.pad.Pkcs7 })
  41. var plaintext = bytes.toString(cryptojs.enc.Utf8)
  42. return plaintext
  43. } else {
  44. return ''
  45. }
  46. }
  47. //base64加密
  48. codecutil.base64Encode = function (data) {
  49. if (data) {
  50. var bytes = cryptojs.enc.Utf8.parse(data)
  51. var plaintext = bytes.toString(cryptojs.enc.Base64)
  52. plaintext = plaintext.toString()
  53. // plaintext = plaintext.replace(/\//g, '_')
  54. // plaintext = plaintext.replace(/\+/g, '-')
  55. // plaintext = plaintext.replace(/=/g, '')
  56. return plaintext
  57. } else {
  58. return ''
  59. }
  60. }
  61. //base64解密
  62. codecutil.base64Decode = function (data) {
  63. if (data) {
  64. var ciphertext = data.toString()
  65. ciphertext = ciphertext.replace(/_/g, '/')
  66. ciphertext = ciphertext.replace(/-/g, '+')
  67. for (var i = 0; i < 4 - ciphertext.length % 4; i++) {
  68. ciphertext = ciphertext + '='
  69. }
  70. var bytes = cryptojs.enc.Base64.parse(ciphertext)
  71. var plaintext = bytes.toString(cryptojs.enc.Utf8)
  72. return plaintext
  73. } else {
  74. return ''
  75. }
  76. }
  77. codecutil.hexEncode = function (data) {
  78. if (data) {
  79. var bytes = cryptojs.enc.Utf8.parse(data)
  80. var plaintext = bytes.toString(cryptojs.enc.Hex)
  81. return plaintext
  82. } else {
  83. return ''
  84. }
  85. }
  86. codecutil.hexDecode = function (data) {
  87. if (data) {
  88. var bytes = cryptojs.enc.Hex.parse(data)
  89. var plaintext = bytes.toString(cryptojs.enc.Utf8)
  90. return plaintext
  91. } else {
  92. return ''
  93. }
  94. }