safebash / opencrypto

OpenCrypto is a lightweight JavaScript library built on top of WebCryptography API
MIT License
74 stars 23 forks source link

How to transport and exchange sharedKey between programming languages #4

Closed mxie1563 closed 4 years ago

mxie1563 commented 5 years ago

I tried to use this module with JS-JOSE(https://github.com/square/js-jose). The RSA operation is not compatible. You can check here (https://codepen.io/mxie1563/pen/VoKpLw), it failed loading the private key for decryption. Can you offer your opinion on this one?

PeterBielak commented 5 years ago

Hi Michael, thank you for reporting the issue.

To answer your original question. A symmetric key consists of random bytes generated by a cryptographically secure pseudo-random number generator. You can represent these data as base64 encoded string. There is no specific format defined in which to store these keys.

You can encrypt the symmetric key using an asymmetric algorithm, either elliptic-curves or RSA.

The example below demonstrates how you can achieve this using RSA.

const crypt = new OpenCrypto()
crypt.getRSAKeyPair().then(keyPair => {
  crypt.getSharedKey().then(sharedKey => {
    crypt.encryptKey(keyPair.publicKey, sharedKey).then(encryptedSharedKey => {
      crypt.decryptKey(keyPair.privateKey, encryptedSharedKey).then(decryptedSharedKey => {
        console.log(decryptedSharedKey)
      })
    })
  })
})

Regarding the second question, I have been very busy recently and did not look into it yet. Nonetheless, I very appreciate your interest in OpenCrypto and will try to address the issue as soon as possible. Please let me know if you have any further questions.

Best Regards, Peter Bielak