The front end uses national secret sm2 and sm4 for encryption and decryption

National secret SM encryption

  1. National secret SM: National secret algorithm, that is, national commercial encryption algorithm. It is a cryptographic algorithm standard and its application specification recognized and published by the State Cryptography Administration, some of which have become international standards. Such as the SM series of ciphers, SM stands for commercial secrets, that is, commercial ciphers, which refer to cryptographic techniques that are used for commerce and do not involve state secrets.

Install SM encryption dependencies

npm install --save sm-crypto
或
npm install --save sm-crypto --legacy-peer-deps   

SM2

encapsulation

  1. Encapsulate the encryption and decryption method of sm2, and name the file sm2.js
    // 引入
    const sm2 = require('sm-crypto').sm2
    const cipherMode = 0 // 1 - C1C3C2,0 - C1C2C3,默认为1
    
    // 后端会生成密钥对
    // publicKey:公钥 后端提供
    // privateKey:私钥 后端提供
    const publicKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    const privateKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    
    // 加密
    // value:需要加密的内容
    export function encrypt (value) {
          
          
    	// 给后端传值时需要在加密的密文前面加04 ,这样后端才能解密正确不报错
    	return '04' + sm2.doEncrypt(value, publicKey, cipherMode)
    }
    
    // 解密
    // value:需要解密的密文
    export function decrypt (value) {
          
          
    	// 后端传输过来的密文开头的两个字符通常也为04,因此解密时需要删除
    	return sm2.doDecrypt(value.slice(2, value.length), privateKey, cipherMode)
    }
    

use

	// 引入sm2.js,注意文件路径不要出错
	import {
    
     encrypt, decrypt } from './sm2'

	// data:需要加密的数据
	// encryptData:加密后的密文
	// 若有需要则将js对象转换为字符串后进行加密:JSON.stringify(data)
	const encryptData = encrypt(JSON.stringify(data))

	// data:需要解密的密文
	// decryptData:解密后的数据
	// 若解密结果为json字符串,则可以通过JSON.parse()方法将解密结果转化为json对象
	const decryptData = decrypt(data)

SM4

encapsulation

  1. Encapsulate the encryption and decryption method of sm4, and name the file sm4.js

    const SM4 = require("gm-crypt").sm4;
    
    const pwdKey = "xxxx"; //密钥 前后端一致,后端提供
    let sm4Config = {
          
          
          key: pwdKey,
          mode: "ecb",  // 加密的方式有两种,ecb和cbc两种,看后端如何定义的,cbc需要iv参数,ecb不用
          iv: '1234567891011121', // 初始向量,cbc模式的第二个参数,也需要跟后端配置的一致
          cipherType: "base64"
        };
    
    const sm4Util = new SM4(sm4Config); // new一个sm4函数,将上面的sm4Config作为参数传递进去。
        
    /* 
     * 加密工具函数
     * @param {String} text 待加密文本
     */
    export function encrypt(text) {
          
          
      return sm4Util.encrypt(text, pwdKey);
    }
    
    /*
     * 解密工具函数
     * @param {String} text 待解密密文
     */
     export function decrypt(text) {
          
          
      return sm4Util.decrypt(text, pwdKey);
    }
    

use

	// 引入asm4.js,注意文件路径不要出错
	import {
    
     encrypt,decrypt } from "./sm4"

	// data:需要加密的数据
	// encryptData:加密后的密文
	// 若有需要则将js对象转换为字符串后进行加密:JSON.stringify(data)
	const encryptData = encrypt(JSON.stringify(data))

	// data:需要解密的密文
	// decryptData:解密后的数据
	// 若解密结果为json字符串,则可以通过JSON.parse()方法将解密结果转化为json对象
	const decryptData = decrypt(data)

Guess you like

Origin blog.csdn.net/lhh_gxx/article/details/128663641