aes.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import CryptoJS from "crypto-js";
  2. //秘钥
  3. const CRYPTOJSKEY= "szhdxfiCPTh3zDaW";
  4. // 加密
  5. export const encrypt = plaintText => {
  6. var plaintText = plaintText;
  7. var options = {
  8. mode: CryptoJS.mode.ECB,
  9. padding: CryptoJS.pad.Pkcs7
  10. };
  11. var key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY);
  12. var encryptedData = CryptoJS.AES.encrypt(plaintText, key, options);
  13. var encryptedBase64Str = encryptedData.toString().replace(/\//g, "_");
  14. encryptedBase64Str = encryptedBase64Str.replace(/\+/g,"-");
  15. return encryptedBase64Str;
  16. }
  17. //解密
  18. export const decrypt = encryptedBase64Str => {
  19. var vals = encryptedBase64Str.replace(/\-/g, '+').replace(/_/g, '/');
  20. var options = {
  21. mode: CryptoJS.mode.ECB,
  22. padding: CryptoJS.pad.Pkcs7
  23. };
  24. var key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY);
  25. var decryptedData = CryptoJS.AES.decrypt(vals, key, options);
  26. var decryptedStr = CryptoJS.enc.Utf8.stringify(decryptedData);
  27. return decryptedStr
  28. }
  29. //type:1 姓名、2 手机号、3 身份证
  30. export const tuomin = (value,type) => {
  31. if(!value) return;
  32. let res = '';
  33. if(type==1){
  34. let arr = Array.from(value)
  35. if (arr.length === 2) {
  36. res = arr[0] + '*'
  37. } else if (arr.length > 2) {
  38. for (let i = 1; i < arr.length - 1; i++) {
  39. arr[i] = '*'
  40. }
  41. res = arr.join("")
  42. }else {
  43. res = value
  44. }
  45. }else if(type==2){
  46. res = value.replace(/^(.{3})(?:\d+)(.{4})$/, "$1****$2");
  47. }else if(type==3){
  48. res = value.replace(/^(.{4})(?:\d+)(.{4})$/, "$1**********$2");
  49. }
  50. return res;
  51. }
  52. export default {
  53. encrypt,
  54. decrypt,
  55. tuomin
  56. }