Front end projects for field use encrypted jsencrypt

For field use jsencrypt distal encrypted item.

Use: ① From obtains the public key provided ② ③ object instance ④ public key required to encrypt data and then returns.

As for a simple package

/**
 * npm install jsencrypt -S
 * @Param getPublicKey get to the public, the general returned by the backend, we can send ajax request to get the public key is returned when you click on the submit button
 * @Param data we need to encrypt data, after passing through the object returned by instantiating a new piece of encrypted, then send it as a parameter to the rear end to
 * @returns {PromiseLike<ArrayBuffer>}
 */
function encryptData(getPublicKey, data) {

    // instantiate an object of 
    the let the encrypt = new new JsEncrypt ();

    // set the public 
    encrypt.setPublicKey (getPublicKey);

    // returns the encrypted data 
    return encrypt.encrypt (Data);
}

Write a simple demo below to vue project as an example

<template>

</template>

<script>
    import JsEncrypt from 'jsencrypt'
    import axios from 'axios'
    export default {
        data() {
          return {
              username: 'hello',
              password: ''
          }
        },
        methods: {
            encryptData(getPublicKey, data) {

                // instantiate an object of 
                the let the encrypt = new new JsEncrypt ();

                // set the public 
                encrypt.setPublicKey (getPublicKey);

                // returns the encrypted data 
                return encrypt.encrypt (Data);
            },
            submit() {
                axios({
                    method: 'post',
                    url: '',
                }). the then (RES => {
                     // RES returned to the rear end of the key, as follows 
            //
the let RES = '+ VHPOwxi0re MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCa / GhWApLl6WQMoTKazQsPp66AD8CKt2TaPrwv dDFdQ5jP9xRbalGhLhEBlDq20oOCuIwWAI7 + / + Qe7DqIE0pMrTi yWKzuRp0E s0QTTvjYIBd + / + Wj9R007WT ELkqibehYIvJ4sIEM / 64vGG / FaPsK9RsZiGShOAQIDAQAB'
                    let password = encryptData(res, password);
                    axios({
                        method: 'post',
                        url: '',
                        data: {
                            username: this.username,
                            password: password
                        }
                    }).then(ress => {
                        console.log(ress);
                    })
                })
            }
        }
    }
</script>

<style>

</style>

Guess you like

Origin www.cnblogs.com/troublehuan/p/12082265.html