0

I am currently using jsencrypt to encrypt text using a public key. This is what I have.

file.jsx

import JsEncrypt  from 'jsencrypt';
const textEncrypt = 'sensitive data';
const encrypt = new JsEncrypt.JSEncrypt();
encrypt.setPublicKey(key);
const encryptedText = encrypt.encrypt(textEncrypt);
...

Is there another library that can do this? I was reading about crypto-js but I am not sure if it does above.

import JsEncrypt from 'jsencrypt';
import Cryptico from "cryptico";

function jsencryptMethod() {
    const encrypt = new JsEncrypt.JSEncrypt();
    encrypt.setPublicKey('mykey');
    return encrypt.encrypt('DE52**0978');
}

function crypticoMethod(){
    return Cryptico.encrypt('DE52**0978', 'mykey');
}

it('should be equal', async () => {
    expect(jsencryptMethod()).toEqual(crypticoMethod());
});

Error: expect(received).toEqual(expected) // deep equality

Expected: {"status": "Invalid public key"}
Received: false
Angelina
  • 2,175
  • 10
  • 42
  • 82
  • 1
    JSEncrypt is a JavaScript library for RSA, i.e. for asymmetric encryption. `encrypt.encrypt(...)` actually returns the ciphertext (not just a boolean). You can find a complete example [here](https://github.com/travist/jsencrypt#how-to-use-this-library). CryptoJS is a JavaScript library for various cryptographic algorithms, especially symmetric encryption, but it doesn't support asymmetric encryption, so it does _not_ support RSA. The recommendation of libraries is off-topic. [Here](https://gist.github.com/jo/8619441) is a link with a list of JavaScript libraries. – Topaco Apr 22 '20 at 08:42
  • @Topaco I think cryptico might be able to do the job. What do you think? const encryptedText = Cryptico.encrypt(textEncrypt, key); – Angelina Apr 24 '20 at 19:53
  • Cryptico implements a hybrid encryption, i.e. a combination of asymmetric and symmetric encryption, here RSA and AES. It's therefore no replacement for a _pure_ RSA implementation. Whether it still fits for your purposes depends on your overall requirements. One library that supports pure RSA is [forge](https://github.com/digitalbazaar/forge#rsa) for PKCS#1 v1.5 padding and OAEP, another [WebCryptoAPI](https://github.com/diafygi/webcrypto-examples#rsa-oaep) for OAEP. – Topaco Apr 25 '20 at 07:00

0 Answers0