aes.js 1.6 KB

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