0

I used Cryptico library and it is working fine, but the public key from Cryptico is not compatible with OpenSSL (meaning I can not use it to encrypt data with PHP as an example). I am asking how can I generate a key pair client side with the public key being compatible with OpenSSL. The goal is being able to encrypt data with the public key on IOS, Android or PHP and decrypt it on Javascript (meaning it is compatible cross platforms).

1 Answers1

1

You may use jsbn library in the link below: http://www-cs-students.stanford.edu/~tjw/jsbn/ and you may see the demo here: http://www-cs-students.stanford.edu/~tjw/jsbn/rsa2.html This is the most popular library you may find and you have the chance to customize it based on your requirement. Also, you have another option with jsencrypt which is available here: https://github.com/travist/jsencrypt It is also compatible with openssl.

      // Encrypt with the public key...
      var encrypt = new JSEncrypt();
      encrypt.setPublicKey($('#pubkey').val());
      var encrypted = encrypt.encrypt($('#input').val());

      // Decrypt with the private key...
      var decrypt = new JSEncrypt();
      decrypt.setPrivateKey($('#privkey').val());
      var uncrypted = decrypt.decrypt(encrypted);

      // Now a simple check to see if the round-trip worked.
      if (uncrypted == $('#input').val()) {
        alert('It works!!!');
      }
      else {
        alert('Something went wrong....');
      }
  • 1
    Thank you! I can also use (var crypt = new JSEncrypt({default_key_size: 2048}); var PublicPrivateKey = {PublicKey: crypt.getPublicKey(), PrivateKey:crypt.getPrivateKey()};) to generate the key pair. – Mohammad Ameen Esafi Jun 10 '21 at 16:27