preface

Front-end implementation of RSA encryption and decryption, DES decryption, the main use of jsencrypt, crypto-js package

Usage scenarios

  • Need to store some flag bits locally, security issues
  • Encryption of user login passwords

encryption

  • RSA encryption
NPM install jsencrypt Generate a pair of RSA public and private keys online import jsencrypt from 'jsencrypt'; Const PRIV_KEY = ` -- -- -- -- -- BEGIN PRIVATE KEY secret KEY content -- -- -- -- -- -- -- -- -- - END PRIVATE KEY -- -- -- -- -- `; Const PUB_KEY = ` -- -- -- -- -- BEGIN PUBLIC KEY -- -- -- -- -- the PUBLIC KEY content -- -- -- -- -- END PUBLIC KEY -- -- -- -- -- `; // Public key encrypt function encrypt(text) {const encrypt = new JSEncrypt(); encrypt.setPublicKey(PUB_KEY); const encrypted = encrypt.encrypt(text); return encrypted; } // Private key decrypt function decrypt(text) {const decrypt = new JSEncrypt(); decrypt.setPrivateKey(PRIV_KEY); const decrypted = decrypt.decrypt(text); return decrypted; } to view encrypted data is accurate, if you want to in the code, see if encryption is accurate, can online decryption comparison results, decrypt the url: http://www.bejson.com/enc/rsa/Copy the code
  • DES encryption
Import CryptoJS from 'crypto-js'; import CryptoJS from 'crypto-js'; Encrypt (" Cryptojs.enc.utf8.parse (' convention with key '), {mode: cryptojs.mode.ecb,}).tostring (); encrypt(" Cryptojs.enc.utf8.parse (' convention with key '), {mode: cryptojs.mode.ecb,}).tostring ();Copy the code