i'm try to use pidCrypt RSA encryption library with React js but it's return "pidCrypt is not defined" error

429 views Asked by At

when I try it with Html and vanilla javascript it's just working fine

<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>

but when I use it with React its give me pidCrypt is not defined error

when I import it and use rsaEncrypt function in my component its give error pidCrypt is not defined

..../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;

** I use it in my React component this way;**

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;

I try to disable ESLint and it didn't solve the problem

0

There are 0 answers