• Vue使用CryptoJS实现前后端密码加密


    引用:https://www.jianshu.com/p/f9284c3f732c
    https://blog.csdn.net/qq_46673413/article/details/136901184

    一般管理系统前端传递密码,没有被加密过,就有安全问题,需要对前端进行加密,,

    crypto.js

    是一个javascript库,提供了一系列密码学函数和工具,用于加密,解密,生成摘要等任务,,他支持多种加密算法,包括常见的对称加密算法(如:AES,DES) 和非对称机密算法 RSA

    同时,cryptoJs还包括了ECB和CBC两种模式,其中ECB模式: Electronic codebook 电码本,,在ECB模式中,每个明文分组都被单独加密,,且每个明文分组都被加密为相同的密文分组,也就是说,如果两个明文分组相同,那么他们的密文分组也相同,,CBC模式: cipher block chaining(密文分组链接模式),在cbc模式中,每个明文分组都是与前一个密文分组进行xor运算,,然后进行加密,,因此,密文分组是相互连接的,如果两个明文分组相同,那么他们的密文分组也会不同,

    这里,我们使用了AES对称加密算法,并使用了CBC模式实现登录密码的加密,实现步骤如下:

    前端使用CryptoJS
    npm install crypto-js
    
    • 1
    import CryptoJS from 'crypto-js';
    
    • 1

    加密方法:

    //设置秘钥和秘钥偏移量
    const SECRET_KEY = CryptoJS.enc.Utf8.parse("1234567890123456");
    const SECRET_IV = CryptoJS.enc.Utf8.parse("1234567890123456");
    /**
     * 加密方法
     * @param word
     * @returns {string}
     */
    function encrypt(word) {
      let srcs = CryptoJS.enc.Utf8.parse(word);
      let encrypted = CryptoJS.AES.encrypt(srcs, SECRET_KEY, {
          iv: SECRET_IV ,
          mode: CryptoJS.mode.CBC,
          padding: CryptoJS.pad.ZeroPadding
      })
      return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    解密方法:

    function decrypt(word) {
      let base64  = CryptoJS.enc.Base64.parse(word);
      let srcs = CryptoJS.enc.Base64.stringify(base64);
      const decrypt = CryptoJS.AES.decrypt(srcs, SECRET_KEY, {
        iv: SECRET_IV ,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.ZeroPadding
      });
      const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
      return decryptedStr;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    java

    java解密实现:

    package com.ruoyi.framework.config.utils;
    
    import javax.crypto.Cipher;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    import java.util.Base64;
    
    /**
     * @author cc
     * @date 2024-04-25 10:39
     **/
    
    public class CryptoUtil {
    
        private final static String IV = "1234567890123456";//需要前端与后端配置一致
        private final static String KEY = "1234567890123456";
    
    
        /**
         * 解密算法,使用默认的IV,KEY
         * @param content
         * @return
         */
        public static String decrypt(String content){
            return decrypt(content,KEY,IV);
        }
    
    
        public static String encrypt(String content,String key,String iv){
            try{
                // "算法/模式/补码方式"NoPadding PkcsPadding
                Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
                int blockSize = cipher.getBlockSize();
                byte[] dataBytes = content.getBytes();
                int plaintextLength = dataBytes.length;
                if (plaintextLength % blockSize != 0) {
                    plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
                }
                byte[] plaintext = new byte[plaintextLength];
                System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
                SecretKeySpec keyspec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
                IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes("UTF-8"));
                cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
                byte[] encrypted = cipher.doFinal(plaintext);
    
    
                return  Base64.getEncoder().encodeToString(encrypted);
            }catch (Exception e) {
                throw new RuntimeException("加密算法异常 CryptoUtil encrypt()加密方法,异常信息:" + e.getMessage());
            }
    
        }
    
    
        /**
         * 解密方法
         * @param content
         * @param key
         * @param iv
         * @return
         */
        public static String decrypt(String content, String key, String iv){
            try {
                byte[] encrypted1 = Base64.getDecoder().decode(content);
                Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
                SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
                IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());
                cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
                byte[] original = cipher.doFinal(encrypted1);
                return new String(original).trim();
            } catch (Exception e) {
                throw new RuntimeException("加密算法异常 CryptoUtil decrypt()解密方法,异常信息:" + e.getMessage());
            }
        }
    
    
        public static void main(String[] args) {
            String decrypt = CryptoUtil.decrypt("8lu4XRkZoVfn658E39KtNg==");
            System.out.println("decrypt = " + decrypt);
        }
    
    }
    
    
    • 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

    不知道为什么必需使用,NoPadding 才不会报错,,应该是jdk有问题

    将sprintsecurity的passwordEncoder换掉,
    或者不用换,用之前的passwordEncoder ,,在对比密码之前将 前端加密的密码 ,解码还原回去

  • 相关阅读:
    micro-app-1-渲染篇
    SRAM之ECC检测机制
    优雅的用户体验:微信小程序中的多步骤表单引导
    QT::QString 很全的使用
    nuc flycontroler dimension
    Vue中methods实现原理
    基于Java+SpringBoot+Thymeleaf+Mysql新冠疫苗预约系统设计与实现
    CCNA笔记
    python之value_counts()介绍
    vue2和vue3详细区别
  • 原文地址:https://blog.csdn.net/qq_36022463/article/details/138184500