1

当我用 Html 和 vanilla javascript 尝试它时,它工作正常

<body>
      <form>
        <input id="pin" type="text" name="">
        <input id="submit" type="submit" name="">
      </form>

      <!-- Encryption  -->
      <script src="./rsa/pidcrypt.js"></script>
      <script src="./rsa/pidcrypt_util.js"></script>
      <script src="./rsa/asn1.js"></script>
      <script src="./rsa/jsbn.js"></script>
      <script src="./rsa/rng.js"></script>
      <script src="./rsa/prng4.js"></script>
      <script src="./rsa/rsa.js"></script>

     <script type="text/javascript">
        function rsaEncrypt(uuid, clear_ipin, publicKey) {
          var plainText = uuid + clear_ipin;

          var key = pidCryptUtil.decodeBase64(publicKey);
          //new RSA instance
          var rsa = new pidCrypt.RSA();
          //RSA encryption
          //ASN1 parsing
          var asn =                 pidCrypt.ASN1.decode(pidCryptUtil.toByteArray(key));
          var tree = asn.toHexTree();
          //setting the public key for encryption
          rsa.setPublicKeyFromASN(tree);
          crypted = rsa.encrypt(plainText);
          return pidCryptUtil.fragment(pidCryptUtil.encodeBase64(pidCryptUtil.convertFromHex(crypted)), 64);
       }

        var uuid = '6af1d2fc-32e8-4742-a71f-ea4d807b65a7';
        var publicKey='WerereJKoZIhvcNAQEBBQADSw5wSAJBANx4gKYS9v3CrWWsxdP7xDxFvl+Is/0kc1dvMI1yNWDXI3A5dI41277MUOv7gmw66SnRsHX/KAM08PRe0+Sa0vMCAwEAAQ==';
        var iPINField = document.getElementById("pin").value;
        var submit = document.getElementById("submit");


        submit.addEventListener('click', (e) => {
          e.preventDefault();
      const encryption = rsaEncrypt(uuid, iPINField, publicKey);

          console.log('encripted = ' + encryption)

        });
      </script>
    </body>

但是当我将它与 React 一起使用时,它给我pidCrypt is not defined错误

当我导入它并rsaEncrypt function在我的组件中使用它时,它给出的错误pidCrypt 没有定义

..../component/encryption.js
    /* eslint-disable */
    require('../component/rsa/pidcrypt')
    require('../component/rsa/pidcrypt_util');
    require('../component/rsa/asn1');
    require('../component/rsa/jsbn');
    require('../component/rsa/rng');
    require('../component/rsa/prng4');
    require('../component/rsa/rsa');


    function rsaEncrypt (uuid, clear_ipin, publicKey) {
                var plainText = uuid + clear_ipin;

                var key = pidCryptUtil.decodeBase64(publicKey);
                console.log(key)

                //new RSA instance
                var rsa = new pidCrypt.RSA();

                //RSA encryption
                //ASN1 parsing
                var asn = pidCrypt.ASN1.decode(pidCryptUtil.toByteArray(key));

                var tree = asn.toHexTree();
                //setting the public key for encryption
                rsa.setPublicKeyFromASN(tree);
                crypted = rsa.encrypt(plainText);
                console.log('crypted text = ' + crypted)

                return pidCryptUtil.fragment(pidCryptUtil.encodeBase64(pidCryptUtil.convertFromHex(crypted)), 64);
            }
       

    export default rsaEncrypt;

** 我以这种方式在我的 React 组件中使用它;**

import React, { Component } from 'react';
    import rsaEncrypt from '../component/encrption';

          
    class Blance extends Component {
        state ={
            balance:{ pan:'', ipin: '', expDate:''}
          }
          
          handleSubmit = async (e) =>{
        try{
           e.preventDefault();
           const url = "my api url";
           const clear_ipin = this.state.balance.ipin;
           const publicKey = 'MFwwDQY76jhgyADSwAwSAJBANx4gKYSMv3CrWWsxdPfxDxFvl+Is/0kc1dvMI1yNWDXI3AgdI4127KMUghty4545HX/KAM0IPRe0+Sa0vMCAwEAAQ==';
           const uuid = uuidv4();

           const encryptediPIN = rsaEncrypt(uuid, clear_ipin, publicKey);
           
           console.log('encryptediPIN = ' + encryptediPIN)

           
           

       } catch(error){console.log('sumbit error ' + error)}
    }


    }
export default Blance;

我尝试disable ESLint并没有解决问题

4

0 回答 0