SoFunction
Updated on 2025-04-10

Detailed explanation of how crypto-js symmetric encryption and decryption is used (vue and Java side)

1. Vue front end

1.1. Install crypto-js

npm install crypto-js

1.2. Create encryption and decryption

import CryptoJS from 'crypto-js'

/**
  * AES encryption processing (CBC mode)
  */
export function encryptCBC(word, keyStr, ivStr) {
  // 16-digit random code  keyStr = keyStr ? keyStr : "Ej7SdjOBvlv2PubN";
  ivStr = ivStr ? ivStr : "Ej7SdjOBvlv2PubN";
  let key = .(keyStr);
  let iv = .(ivStr);
  var encryptedData = (word, key, 
	  {
	  	iv: iv,
	    mode: ,
	    padding: .Pkcs7
	  }
  );
  return ();
}
/**
  * AES decryption processing (CBC mode)
  */
export function decryptCBC(word, keyStr, ivStr) {
  word = (word + '').replace(/\n*$/g, '').replace(/\n/g, ''); // This line, replace the newline with empty
  // Key  keyStr = keyStr ? keyStr : "Ej7SdjOBvlv2PubN";

  // Vector  ivStr = ivStr ? ivStr : "Ej7SdjOBvlv2PubN";
  var key = .(keyStr);
  let iv = .(ivStr);
  var decrypt = (word, key, 
	  {
	    iv: iv,
	    mode: ,
	    padding: .Pkcs7
	  }
  )
  return (.Utf8)
}

1.3. Use (vue file or js)

import { encryptCBC } from '@/js/util/AES'

export function test1(){
	var a1 = encryptCBC("Zhang San");
	('Encrypted data:',a1 )
}

2. How to use the Java backend

import ;

import ;
import ;
import ;
import ;
import .Base64;
public class AESUtil {
    // AES key algorithm    private static final String KEY_ALGORITHM = "AES";
    // Encryption/decryption algorithm/working mode/fill method    private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";

    private static final String KEY="Ej7SdjOBvlv2PubN";

    //CBC encryption offset    private static final String IVCODE="Ej7SdjOBvlv2PubN";
    
    /**
      * AES encryption
      */
    public static String encrypt(String data) {
        try {
            (new BouncyCastleProvider());
            SecretKeySpec secretKeySpec = new SecretKeySpec((), KEY_ALGORITHM);
            Cipher cipher = (CIPHER_ALGORITHM, "BC");
            IvParameterSpec iv = new IvParameterSpec(());
            (Cipher.ENCRYPT_MODE, secretKeySpec,iv);
            byte[] encryptedBytes = (());
            return ().encodeToString(encryptedBytes);
        } catch (Exception e) {
            ();
            return null;
        }
    }
    /**
      * AES decryption
      */
    public static String decrypt(String encryptedData) {
        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec((), KEY_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(());
            (new BouncyCastleProvider());
            byte[] encryptedBytes = .parseBase64Binary(encryptedData);
            Cipher cipher = (CIPHER_ALGORITHM, "BC");
            (Cipher.DECRYPT_MODE, secretKeySpec, iv);
            byte[] decryptedBytes = (encryptedBytes);
            return new String(decryptedBytes);
        } catch (Exception e) {
            ();
            return null;
        }
    }

    // verify    public static void main(String[] args) throws Exception {
        String data = "Zhang San";
        String encryptedData = encrypt(data); // Encrypt data        String decryptedData = decrypt(encryptedData); // Decrypt the data        ("Encrypted data: " + encryptedData);
        ("Decrypted data: " + decryptedData);
    }
}

Summarize

This is the article about the usage of crypto-js symmetric encryption and decryption (vue and Java terminals). For more related contents of crypto-js symmetric encryption and decryption, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!