编程知识 cdmana.com

Front end utilization jsencrypt.js RSA encrypts

It's a complete copy , Original address https://www.jianshu.com/p/27e...

Write it at the front

It's useful recently jsencrypt To set the login password rsa Encryption processing , recorded , For future reference .

What is? RSA encryption

RSA encryption algorithm It's a kind of Asymmetric encryption algorithm ,RSA Encryption uses " a pair " secret key . They are public key and private key , This public key and private key are actually a set of numbers ! Its binary bit length can be 1024 Bits or 2048 position . The longer the length, the stronger the encryption , So far, the maximum length that can be cracked in public is 768 A key , Just above 768 position , Relatively safe . So so far , This encryption algorithm has been widely used .

RSA encryption and decryption

  • Use Public key Encrypted data , utilize Private key To decrypt
  • Use Private key Encrypted data , utilize Public key To decrypt

RSA Secret key generation

Mac System built in OpenSSL( Open source encryption library ), So you can use the command directly on the terminal .

Windows The system can use git Command line tools

Mac System built in OpenSSL( Open source encryption library ), So you can use the command directly on the terminal .

Windows The system can use git Command line tools

 $ openssl genrsa -out private.pem 1024
    Generating RSA private key, 1024 bit long modulus (2 primes)
    ...+++++
    .............................+++++
    e is 65537 (0x010001)
  • Extract the public key from the private key
$ openssl rsa -in private.pem -pubout -out public.pem
  writing RSA key
  • And that creates private.pem and public.pem Two documents , You can use the terminal to view
$ cat private.pem
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDNNorgFngK1zjHOnQlIUh5NjOxZIiEPZ8Knu6B/IyY0LBRToo1
TZC7/nK6j8on/2sBdv5nFuTwlOpW9UL8C4yZJdjTwYXn5X+wZZsz1RXNI5zjhSXu
GeYzF7WhxusKo6zrR6b0IMNg2W016PWU3UkjOXxoaIGkMN77oIorPP5bHQIDAQAB
AoGABOdOvjgLOkcWRjxxVgnLj4nqBk0erfpC+J//lv+P5H7oF6lGyCtIUBWubCLP
c9E4n1pWjeQQKGeGiflmVlt4So2UPQJD/fvpmT0lswaud+ObbUtFIo4CApHMXdTB
jIC/nDSdFut2Yd32N8OH/QYnzAS1tarLGjk3x+Dg5nY3VEECQQDvM7GLXT2df85I
X+FBX9YiwUPXqciUJp3XdBOngsyENOFu0C3/cBTxvaiKkMXVPqMjOdoCAY+hz/k1
xPUVBpZ5AkEA25/Objru9LI1XSj8M1gJoIUpiR+mJysN7Q7wWbSK6DI+Hz95NQ5r
kAzG89lwMW3dLycH8VPGsWMuxjA7NG0QxQJBAIxDxdKxJFZdAXuTLaWGKy1KIxwt
pT6qvlf+6x+JJaBI2gB+9toYwU9YJaLLbhazmjonzFzsyWrbZ4lOK2De8hECQQCl
uJRgAQBGjCJQRZjodUnuYgzRd5w8efRsKJWcWutmAmN12MNxEYyAieOmJTDPW4NH
DUClDP4k5B5rVgGWsaWxAkA4m0bHwiPqO4/Yz6eyl2jYvljtmqr7KZFXrlsBUrIm
XXaTuMdsOmLlp/u078XFw0N+RaUWxbE6ATH7mTGjB2nV
-----END RSA PRIVATE KEY-----
$ cat public.pem
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNNorgFngK1zjHOnQlIUh5NjOx
ZIiEPZ8Knu6B/IyY0LBRToo1TZC7/nK6j8on/2sBdv5nFuTwlOpW9UL8C4yZJdjT
wYXn5X+wZZsz1RXNI5zjhSXuGeYzF7WhxusKo6zrR6b0IMNg2W016PWU3UkjOXxo
aIGkMN77oIorPP5bHQIDAQAB
-----END PUBLIC KEY-----

jsencrypt Introduce

jsencrypt It's based on rsa Encrypted js library

Usage method

  • install
npm install jsencrypt 
  • introduce
import JSEncrypt from 'jsencrypt'
  • rsa encryption
var encryptor = new JSEncrypt()  //  Create an encrypted object instance 
  // Before ssl Generated public key , Be careful not to have spaces when copying 
  var pubKey = '-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC1QQRl0HlrVv6kGqhgonD6A9SU6ZJpnEN+Q0blT/ue6Ndt97WRfxtSAs0QoquTreaDtfC4RRX4o+CU6BTuHLUm+eSvxZS9TzbwoYZq7ObbQAZAY+SYDgAA5PHf1wNN20dGMFFgVS/y0ZWvv1UNa2laEz0I8Vmr5ZlzIn88GkmSiQIDAQAB-----END PUBLIC KEY-----'
  encryptor.setPublicKey(pubKey)// Setting public key 
  var rsaPassWord = encryptor.encrypt(' Content to encrypt ')  //  Encrypt the content 
  • rsa Decrypt
var decrypt = new JSEncrypt()// Create decryption object instance 
  // Before ssl Generated secret key 
  var priKey  = '-----BEGIN RSA PRIVATE KEY-----MIICXAIBAAKBgQC1QQRl0HlrVv6kGqhgonD6A9SU6ZJpnEN+Q0blT/ue6Ndt97WRfxtSAs0QoquTreaDtfC4RRX4o+CU6BTuHLUm+eSvxZS9TzbwoYZq7ObbQAZAY+SYDgAA5PHf1wNN20dGMFFgVS/y0ZWvv1UNa2laEz0I8Vmr5ZlzIn88GkmSiQIDAQABAoGBAKYDKP4AFlXkVlMEP5hS8FtuSrUhwgKNJ5xsDnFV8sc3yKlmKp1a6DETc7N66t/Wdb3JVPPSAy+7GaYJc7IsBRZgVqhrjiYiTO3ZvJv3nwAT5snCoZrDqlFzNhR8zvUiyAfGD1pExBKLZKNH826dpfoKD2fYlBVOjz6i6dTKBvCJAkEA/GtL6q1JgGhGLOUenFveqOHJKUydBAk/3jLZksQqIaVxoB+jRQNOZjeSO9er0fxgI2kh0NnfXEvH+v326WxjBwJBALfTRar040v71GJq1m8eFxADIiPDNh5JD2yb71FtYzH9J5/d8SUHI/CUFoROOhxr3DpagmrnTn28H0088vubKe8CQDKMOhOwx/tS5lqvN0YQj7I6JNKEaR0ZzRRuEmv1pIpAW1S5gTScyOJnVn1tXxcZ9xagQwlT2ArfkhiNKxjrf5kCQAwBSDN5+r4jnCMxRv/Kv0bUbY5YWVhw/QjixiZTNn81QTk3jWAVr0su4KmTUkg44xEMiCfjI0Ui3Ah3SocUAxECQAmHCjy8WPjhJN8y0MXSX05OyPTtysrdFzm1pwZNm/tWnhW7GvYQpvE/iAcNrNNb5k17fCImJLH5gbdvJJmCWRk=-----END RSA PRIVATE KEY----'
  decrypt.setPrivateKey(priKey)// Set secret key 
  var uncrypted = decrypt.decrypt(encrypted)// The content encrypted with the public key before decryption 

The current application scenario is when users register or log in , Encrypt a password with a public key , And send it to the backstage , The background uses the private key to decrypt the encrypted content , Then check the password or save it to the database .

版权声明
本文为[Dayu'er]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224152603277y.html

Scroll to Top