0

Before I encrypted my data I've put it into this format:

let a = new TextEncoder().encode(JSON.stringify(data))

After encryption, I've encoded it for storing that way:

let b = new Uint8Array(a)

and finally converted it into base64.

Now I want to decrypt my data with sjcl library function sjcl.decrypt.

  • I've my privateKey as JSON string.
  • I've my cipher as Uint8Array.
  • I've put the cipher into it's original codec after encryption with: new TextDecoder().decode(cipher)

What now? sjcl throws, json decode: this isn't json!. Understood, because my encrypted data is an ArrayBuffer.

So my question: is it possible to decrypt my case with sjcl? When looking into sjcl source it seems, that decrypt needs a JSON. But I have no JSON.

Edit the encryption process is not possible.

Thank you!

------------ UPDATE ------------

That's how the keyPair is generated:

await window.crypto.subtle.generateKey({
        name: "RSA-OAEP",
        modulusLength: 2048,
        publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
        hash: { name: "SHA-256" },
    }, true, ["encrypt", "decrypt"]);

Then the key is exported like that (I receive that key):

await window.crypto.subtle.exportKey(
        "jwk",
        keyPair.privateKey
    );

Ecryption with public key:

await window.crypto.subtle.encrypt({ name: "RSA-OAEP" }, publicKey, data);

In my application I have no access to WebCrypto Api. Tried out sjcl library, but it seems rsa encryptio/decryption is not possible. Tested with jsrsasign library, but it does not support jwk exported keys. Can I convert jwk to pkcs8?

Neneil
  • 105
  • 12
  • Please post the full code for encryption and decryption. – Topaco Nov 16 '21 at 14:24
  • Thx @Topaco encryption was made with WebCrypto API. It's not possible to post this code here. But do you know if it is possible to decrypt non json? When I take a look into https://github.com/bitwiseshiftleft/sjcl/blob/df109ef10a4ced62bfebbbe7af449efa907ba901/core/convenience.js `decode: function (str) {...}` it looks like they expect a special formatted json. I assume "their" format after encryption. – Neneil Nov 16 '21 at 14:38
  • Encryption works with bytes/bits, what they represent (arbitrary data, strings, special formatted JSON) does not matter for encryption. This is also true for sjcl in general, in particular it may of course depend on your code (maybe you use a method that expects a special formatted JSON etc.). However, without the encryption and decryption code, it is probably not possible to answer the question properly. – Topaco Nov 16 '21 at 15:04
  • Okay, I found out or let's say I didn't find a function in sjcl to decrypt with `RSA-OAEP`. Could you @Topaco recommend a library for react native context? – Neneil Nov 16 '21 at 15:45
  • 1
    [Here](https://gist.github.com/jo/8619441) you can find a list of JavaScript crypto libraries. RSA with OAEP is supported by several libraries e.g. [WebCrypto](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt) or [Forge](https://github.com/digitalbazaar/forge#rsa). Which of these are an option in the react native context, I can't say. Note that OAEP has a set of parameters (OAEP and MGF1 digest, label, s. RFC8017) and not all implementations allow the independent choice of these parameters. Thus, the encryption code must be known to determine the OAEP parameters used. – Topaco Nov 16 '21 at 22:09
  • @Topaco I added more information of the encryption process. Does that help for further investigation? Thx! – Neneil Nov 17 '21 at 08:48
  • You need an RSA library that allows to use SHA256 for both digests (OAEP and MGF1) and an empty label. Defaults according to RFC8017 are SHA1 for both digests and an empty label. – Topaco Nov 17 '21 at 11:38

0 Answers0