Front-end national secret encryption and decryption using methods SM2, SM3, SM4

   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.

     Don't talk nonsense and go straight to the dry goods

 How to install National Secret SM

There may be version problems in the project. Use this downgrade to install (the same applies to installation problems later)

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

1. SM2 is an asymmetric encryption algorithm

It is a public key cryptographic algorithm standard based on elliptic curve cryptography. Its secret key length is 256bit, 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 requirements of the electronic authentication service system, etc., and was released by the State Encryption Administration on December 17, 2010.

What SM2 adopts is one of ECC 256 bits, its security strength is higher than RSA 2048 bits, and its operation speed is faster than RSA.


    SM2 introduces usage method 

const sm2 = require('sm-crypto').sm2
const cipherMode = 1 // 1 - C1C3C2,0 - C1C2C3,默认为1 
****后端的话可以约定但后端默认加密方法好像是0 - C1C2C3****
在解密时 使用的密钥和密位一般是传输过来的,密文传输得来的话开头会由04 
需要删除,同时密钥可能存在必须小写的情况 key.slice(2).toLocaleLowerCase() 可以使用这个方法

let encryptData = sm2.doEncrypt(msgString, publicKey, cipherMode) // 加密结果

let decryptData = sm2.doDecrypt(encryptData, privateKey, cipherMode) // 解密结果

下面的一般特殊情况才会用到
encryptData = sm2.doEncrypt(msgArray, publicKey, cipherMode) // 加密结果,输入数组

decryptData = sm2.doDecrypt(encryptData, privateKey, cipherMode, {output: 'array'}) // 解密结果,输出数组

2. SM3 is a cryptographic hash algorithm (used for data digest to ensure integrity)

  It is used to replace international algorithms such as MD5/SHA-1/SHA-2. It is suitable for digital signature and verification, message authentication code generation and verification, and random number generation. It can meet the application requirements of electronic authentication service systems and other applications. It was launched in December 2010 Released on the 17th.

It is an algorithm improved and implemented on the basis of SHA-256. It adopts the Merkle-Damgard structure, the length of the message group is 512bit, and the length of the output digest value is 256bit.

Introduce how to use 
  

const sm3 = require('sm-crypto').sm3
    this.loginForm = sm3(this.loginForm) //获得摘要后的数据

3. SM4 is a block encryption algorithm (symmetrical encryption method)

  Similar to SM1, it is a block symmetric cipher algorithm independently designed by my country to replace international algorithms such as DES/AES. The SM4 algorithm and the AES algorithm have the same key length and packet length, both of which are 128bit. Released on March 21, 2012, it is applicable to the requirements of using block ciphers in cryptographic applications.
encryption

const sm4 = require('sm-crypto').sm4
const msg = '你好,我是空空.' // 可以为 utf8 串或字节数组
const key = 'facca330123456789abcdas3210' // 可以为 16 进制串或字节数组,要求为 128 比特

前后端约定一种下面的加密方法
let encryptData = sm4.encrypt(msg, key) // 加密,默认输出 16 进制字符串,默认使用 pkcs#7 填充(传 pkcs#5 也会走 pkcs#7 填充) 
let encryptData = sm4.encrypt(msg, key, {padding: 'none'}) // 加密,不使用 padding
let encryptData = sm4.encrypt(msg, key, {padding: 'none', output: 'array'}) // 加密,不使用 padding,输出为字节数组
let encryptData = sm4.encrypt(msg, key, {mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'}) // 加密,cbc 模式


decrypt

const sm4 = require('sm-crypto').sm4
const encryptData = '这里放加密后的数据' // 可以为 16 进制串或字节数组
const key = 'abcdeffedcba98765432100123456789' // 可以为 16 进制串或字节数组,要求为 128 比特

用约定的解密方法解密

let decryptData = sm4.decrypt(encryptData, key) // 解密,默认输出 utf8 字符串,默认使用 pkcs#7 填充(传 pkcs#5 也会走 pkcs#7 填充)
let decryptData = sm4.decrypt(encryptData, key, {padding: 'none'}) // 解密,不使用 padding
let decryptData = sm4.decrypt(encryptData, key, {padding: 'none', output: 'array'}) // 解密,不使用 padding,输出为字节数组
let decryptData = sm4.decrypt(encryptData, key, {mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'}) // 解密,cbc 模式

4.baser64

  Generally, the above-mentioned ones will be mixed with baser64 to use a piece of
  baser64 installation

npm install --save js-base64 

npm install --save js-base64  --legacy-peer-deps  


The introduction method 
is added in main.js 

import {Base64} from 'js-base64'
Vue.prototype.$Base64 = Base64;


Instructions 

 let password='asdahsjdj'
 let encPass=this.$Base64.encode(password);//加密
 let decPass=this.$Base64.decode(encPass);//解密

At the beginning, in order to find out how to use the front-end, my brain hurt from searching, and in order to cooperate with the back-end encryption and decryption test, my brain hurt even more.

When using the method, it is important to maintain the consistency of the encryption and decryption key format at the front and back ends

Attach the npm URL : https://www.npmjs.com/package/sm-crypto?activeTab=explore

Guess you like

Origin blog.csdn.net/KO_____KO/article/details/127126908