SoFunction
Updated on 2025-04-04

Vue Normal Encryption and Usage Examples of National Secret SM2, SM3 and SM4

Related websites

sm-crypto:/package/sm-crypto

introduce

1. SM2 is an asymmetric encryption algorithm

It is a public key cryptography algorithm standard based on elliptic curve cipher. Its key length is 256 bit, including digital signature, key exchange and public key encryption, and is used to replace international algorithms such as RSA/DH/ECDSA/ECDH. It can meet the application needs of electronic certification service systems and other applications and was released by the State Cryptography Administration on December 17, 2010.

2.SM3 is a password hash algorithm

It is used to replace international algorithms such as MD5/SHA-1/SHA-2, and is suitable for digital signatures and verification, message authentication code generation and verification, and random number generation, and can meet the application needs of electronic authentication service systems and other applications. It was released on December 17, 2010. It is an algorithm improved and implemented based on SHA-256. It adopts the Merkle-Damgard structure, the message packet length is 512bit and the output digest value length is 256bit.

3.SM4 is a packet password algorithm

The SM4 algorithm is a packet cipher algorithm. Its packet length is 128bit and the key length is 128bit. The encryption algorithm and the key expansion algorithm both use 32 rounds of nonlinear iteration structure, and the encryption operation is performed in units of words (32 bits). Each iteration operation is a transformation function F. The SM4 algorithm has the same structure, except that the wheel key is used opposite, where the decryption wheel key is the reverse order of the encryption wheel key

Normal encryption

Installation dependencies

npm install crypto-js

Create a new utils/file

/*
 * Password encryption tool
 */
import CryptoJS from 'crypto-js'
// Default KEY and iv if not givenconst KEY = .("12345678901234567890");
const IV = .('12345678901234567890');
/**
  * AES encryption: string key iv returns base64
  */
export const Encrypt = (word, keyStr, ivStr) => {
    let key = KEY;
    let iv = IV;
    if (keyStr) {
        key = .(keyStr);
        iv = .(ivStr);
    }
    let srcs = .(word);
    var encrypted = (srcs, key, {
        iv: iv,
        mode: ,
        padding: 
    });
    return .();
}
/**
  * AES decryption: string key iv returns base64
  *
  * @return {string}
  */
export const Decrypt = (word, keyStr, ivStr) => {
    let key  = KEY;
    let iv = IV;
    if (keyStr) {
        key = .(keyStr);
        iv = .(ivStr);
    }
    let base64 = .(word);
    let src = .(base64);
    let decrypt = (src, key, {
        iv: iv,
        mode: ,
        padding: 
    });
    let decryptedStr = (.Utf8);
    return ();
}
/**
  * Secondary splicing: Secondary processing of passwords after decryption before encryption, adding string constants
  *
  * @return {string}
  */
 export const passwordAddStr = 'testAdd'

Use - Encryption

import {Encrypt, passwordAddStr} from '@/util/'

let password = '123123qwe' + passwordAddStr
password = Encrypt(password)

Use - Decrypt

import {Decrypt, passwordAddStr} from '@/util/'
let password
password = Decrypt(password).replace(passwordAddStr, '')

SM2 usage

Installation dependencies

npm install sm-crypto

Create a new utils/file

/**
  * txt: The field to be encrypted
  *
  * @return {string}
  */
const sm2 = require('sm-crypto').sm2
export const  encrypt = (txt) => {
    const cipherMode = 1 ;// 1 - C1C3C2, 0 - C1C2C3, default is 1    const publicKey = "Key" // Generally defined by the backend    let encryptData = (txt, publicKey, cipherMode ) // Encryption results    return '04' + encryptData
}

Use - Encryption

import {encrypt} from '@/util/'

let password = '1234'
password = encrypt(password)

Use - Decrypt

Decrypted by the backend

SM3 usage

Installation dependencies

npm install sm-crypto

Create a new utils/file

/**
  * txt: The field to be encrypted
  *
  * @return {string}
  */
const sm3 = require('sm-crypto').sm3
export const  encrypt = (txt) => {
  let encryptData = sm3(txt) // Encryption results  return encryptData
}

Use - Encryption

import {encrypt} from '@/util/'

let password = '1234'
password = encrypt(password)

Use - Decrypt

Not decrypted

Use sm4

Installation dependencies

npm install sm-crypto

Create a new utils/file

/**
  * txt: The field to be encrypted
  * key: secret key
  *
  * @return {string}
  */
const sm4 = require('sm-crypto').sm4
export const  encrypt = (txt, key) => {
  let encryptData = (txt, key) // Encryption results  return encryptData
}

Use - Encryption

import {encrypt} from '@/util/'

let password = '1234'
password = encrypt(password)

Summarize

This is the article about vue ordinary encryption and the use of national secrets SM2, SM3 and SM4. For more related content about vue ordinary encryption and national secrets, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!