简体   繁体   中英

NodeJS SubtleCrypto - Multiple keyUsage for RSA Keys

I'm so sorry for the ambiguous title of this question, i'm not really sure how to phrase this.

I've generated a Public and Private key using SubtleCrypto in NodeJS like so:

const { publicKey, privateKey } = await  subtle.generateKey({
          name: 'RSA-OAEP',
          4096,
          new Uint8Array([1, 0, 1]),
          'SHA-256',
 }, true, ['encrypt', 'decrypt']);

And this works perfectly for one use case:
Public Key to Encrypt, Private Key to Decrypt.

However, the way I wish to implement RSA in my project is as such:

  1. Client asks Server for a Public Key
  2. Client encrypts payload using Public Key
  3. Server decrypts payload using Private Key
  4. Server encrypts response payload using Private Key
  5. Client decrypts response payload using Public Key

When I try to perform Step 4 , i encountered this error:

The requested operation is not valid for the provided key 

Is there a way to specify that each key could be used for Encrypt & Decrypt ?

Also if my implementation is completely wrong, i'm sorry for that.

Step 4, as described by you, is a signing operation. Signing is very different from encrypting data. For this to work, both, client and server would need their own keypair:

client: client public key & client private key
server: server public key & server private key
  1. Client uploads its client public key
  2. Client asks server for the server public key
  3. Client encrypts payload using the server public key
  4. Server decrypts payload using the server private key
  5. Server encrypts response payload using client public key
  6. Client decrypts response payload using client private key

Besides the possible huge computational workload of encrypting and decrypting large amount of data using RSA, what is the threat model here and what do you want to achieve? You should be very careful if you really want to deploy this into production, as you seem to be rather inexperienced with this topic (no offense here).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM