| 67 | } |
| 68 | |
| 69 | async function gen_rsa_oaep() { |
| 70 | let message = 'Hello World'; |
| 71 | let enc = new TextEncoder(); |
| 72 | const encoded = enc.encode(message); |
| 73 | |
| 74 | const kp = await crypto.subtle.generateKey( |
| 75 | { |
| 76 | name: 'RSA-OAEP', |
| 77 | modulusLength: 4096, |
| 78 | publicExponent: new Uint8Array([1, 0, 1]), |
| 79 | hash: 'SHA-256', |
| 80 | }, |
| 81 | true, |
| 82 | ['encrypt', 'decrypt'], |
| 83 | ); |
| 84 | |
| 85 | try { |
| 86 | const ciphertext = await crypto.subtle.encrypt( |
| 87 | { |
| 88 | name: 'RSA-OAEP', |
| 89 | }, |
| 90 | kp.publicKey, |
| 91 | encoded, |
| 92 | ); |
| 93 | let decrypted = await crypto.subtle.decrypt( |
| 94 | { |
| 95 | name: 'RSA-OAEP', |
| 96 | }, |
| 97 | kp.privateKey, |
| 98 | ciphertext, |
| 99 | ); |
| 100 | |
| 101 | let dec = new TextDecoder(); |
| 102 | const decryptedValue = dec.decode(decrypted); |
| 103 | |
| 104 | console.log('decryptedValue', decryptedValue, decryptedValue === message); |
| 105 | } catch (error) { |
| 106 | console.log('gen: error', error); |
| 107 | } |
| 108 | } |