• 开发技术-前后端(vue+java)加密传输数据


    直接贴代码:

    前端 js—AESutils.js

    import CryptoJS from 'crypto-js'
    export default {
      /**
       * [encrypt 加密]
       * @return {[type]} [加密后的字符串]
       */
      encrypt (content) {
        let key = CryptoJS.enc.Utf8.parse('12345678abcdefgh')
        let encryptResult = CryptoJS.AES.encrypt(content, key, {
          mode: CryptoJS.mode.ECB,
          padding: CryptoJS.pad.Pkcs7
        })
        let hexStr = encryptResult.ciphertext.toString()
        return hexStr
      },
      /**
       * [decrypt 解密]
       * @return {[type]} [解密后的字符串]
       */
      decrypt (content) {
        let key = CryptoJS.enc.Utf8.parse('12345678abcdefgh')
        let bytes = CryptoJS.AES.decrypt(content, key, {
          mode: CryptoJS.mode.ECB,
          padding: CryptoJS.pad.Pkcs7,
          format: CryptoJS.format.Hex
        })
        let decryptResult = bytes.toString(CryptoJS.enc.Utf8)
        return decryptResult.toString()
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    两处需要注意

    • import CryptoJS from ‘crypto-js’ 需要在 package.json 中引入,没有的话 IDE 可能会提示你安装
    • let key = CryptoJS.enc.Utf8.parse(‘12345678abcdefgh’) 有两处地方用到,里面的 12345678abcdefgh 可以随意定义,但是必须是16位长度

    在需要的 Vue 中引入

    import AESUtils from '@/thirdjs/AESUtils'
    ------
          let orginPW = this.ruleForm.password
          console.log('原始密码-orginPW:::', orginPW)
          let newPW = AESUtils.encrypt(orginPW)
          console.log('加密后-newPW:::', newPW)
          let oldPW = AESUtils.decrypt(newPW)
          console.log('解密后-oldPW:::', oldPW)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    后端代码:

    工具类:

    import org.apache.commons.codec.binary.Hex;
    import org.apache.commons.lang.StringUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    
    /**
     * @Author: c01
     * @CreateTime: 2022/9/15 16:13
     * @Description: 加解密工具
     */
    public class AesUtil {
        private static final Logger logger = LoggerFactory.getLogger(AesUtil.class);
    
        // AES key要求是16位的
        private static final String KEY = "12345678abcdefgh";
    
        private static final String KEY_ALGORITHM = "AES";
        private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";//默认的加密算法
    
        /**
         * AES 加密操作
         *
         * @param content 待加密内容
         * @return 返回Base64转码后的加密数据
         */
        public static String encrypt(String content) {
            try {
                if (StringUtils.isBlank(content)) {
                    return content;
                }
                // 创建密码器
                Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
    
                byte[] byteContent = content.getBytes("utf-8");
    
                // 初始化为加密模式的密码器
                cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(KEY));
    
                // 加密
                byte[] result = cipher.doFinal(byteContent);
    
                //转为16进制
                return Hex.encodeHexString(result);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
    
            return null;
        }
    
        /**
         * AES 解密操作
         *
         * @param content
         * @return
         */
        public static String decrypt(String content) {
            logger.info("需要解密的字符串为---" + content);
            if (StringUtils.isBlank(content)) {
                return content;
            }
    
            try {
                //实例化
                Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
    
                //使用密钥初始化,设置为解密模式
                cipher.init(Cipher.DECRYPT_MODE, getSecretKey(KEY));
    
                //执行操作
                byte[] result = cipher.doFinal(Hex.decodeHex(content.toCharArray()));
    
                return new String(result, "utf-8");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return null;
        }
    
        /**
         * 生成加密秘钥
         *
         * @return
         */
        private static SecretKeySpec getSecretKey(final String password) {
    
            return new SecretKeySpec(password.getBytes(), KEY_ALGORITHM);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94

    测试类:

    /**
     * @Author: c01
     * @CreateTime: 2022/9/15 17:05
     * @Description: 测试类
     */
    
    public class AesMain {
        public static void main(String[] args) {
            String cont = "d9495399bbb20d84c9641a69955792fb";
    
            String decrypt = AesUtil.decrypt(cont);
            System.out.println(decrypt);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述
    在这里插入图片描述
    我这里使用了base64编码了一下,base64是编码工具并不是加密算法,看有些资料认为只使用base64就能加密传输数据,也是离谱。
    扩展:java对称加密与非对称加密

  • 相关阅读:
    unity scripting backend mono vs il2cpp
    基于开源模型搭建实时人脸识别系统(六):人脸识别(人脸特征提取)
    BIM、建筑机器人、隧道工程施工关键技术
    【数据结构】栈和队列
    Node直接执行ts文件
    05-流式操作:使用 Flux 和 Mono 构建响应式数据流
    cesium判断一个点是否在一个范围内
    thinkphp6 入门教程合集(更新中)
    C#系统托盘功能实现
    Python 画二分类的AUC
  • 原文地址:https://blog.csdn.net/JustDI0209/article/details/126879488