当前位置:   article > 正文

crypto-js,加密使用_cryptojs

cryptojs

1安装  "crypto-js": "^4.1.1",

2页面引入使用

  1. import CryptoJS from 'crypto-js'
  2. //第二种只用crypto-js和base64
  3. // 使用AesUtil.genAesKey()生成,需和后端配置保持一致
  4. const aesKey = "123456789";
  5. // 使用DesUtil.genDesKey()生成,需和后端配置保持一致
  6. const desKey = "123456789";
  7. /**
  8. * aes 加密方法,同java:AesUtil.encryptToBase64(text, aesKey);
  9. */
  10. export const encryptAES = (data, key) => {
  11. if (!key) {
  12. key = aesKey;
  13. }
  14. const dataBytes = CryptoJS.enc.Utf8.parse(data);
  15. const keyBytes = CryptoJS.enc.Utf8.parse(key);
  16. const encrypted = CryptoJS.AES.encrypt(dataBytes, keyBytes, {
  17. iv: keyBytes,
  18. mode: CryptoJS.mode.CBC,
  19. padding: CryptoJS.pad.Pkcs7
  20. });
  21. return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
  22. }
  23. /**
  24. * aes 解密方法,同java:AesUtil.decryptFormBase64ToString(encrypt, aesKey);
  25. */
  26. export const decryptAES = (data, key) => {
  27. if (!key) {
  28. key = aesKey;
  29. }
  30. const keyBy
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/873082
推荐阅读
相关标签
  

闽ICP备14008679号