|
| 1 | +/** |
| 2 | + * Cookie encryption from KOS frontend |
| 3 | + * |
| 4 | + * How to use: |
| 5 | + * 1. Open kos.cvut.cz in browser |
| 6 | + * 2. Login |
| 7 | + * 3. Open developer console (F12) |
| 8 | + * 4. Open Application tab -> Cookies -> https://kos.cvut.cz |
| 9 | + * 5. Open Console |
| 10 | + * 6. Paste this code in console |
| 11 | + * 7. Run `await encryptCookie('<YOUR_COOKIE_VALUE>')` with copy-pasted cookie values for cookies: |
| 12 | + * - JSESSIONID |
| 13 | + * - XSRF-TOKEN |
| 14 | + * 8. Copy the output values and use them as encrypted cookies in kos-loader |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * RSA Public Key in PEM format |
| 19 | + */ |
| 20 | +const PUB_KEY_PEM = ` |
| 21 | +-----BEGIN PUBLIC KEY----- |
| 22 | +MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2BGOAQqMQ0Uda3jweNM0 |
| 23 | +uMiWVHDy0vipzSyvYiScmv0eU5bLVMClH532r2AeCk8rDfk6t3mUrtsL89P8TVqF |
| 24 | +gB6a8xnojybOeVuMjKCHgXB+pTwfEzgROK35oXgXD158ClZ9aEtEtoUXrTSYa2Z6 |
| 25 | +iqXh/pzhNC16z+Z2O4mZffBt5X6Fhd+uF/NDQJijh8mnLLBaT20E/MNxkehzL6QO |
| 26 | +ycQUEEemP1FG0v7Ipn9z1V0zz82Mqjdm5UEfaDKm3PbIB9Y+em2abnwmOqcvAl82 |
| 27 | +fmW18zjW9882EwFcSpj9auT5GC4fdnEITJjc+8lo702EPrZcKXytNR/lDmFHOvN5 |
| 28 | +5wIDAQAB |
| 29 | +-----END PUBLIC KEY----- |
| 30 | +` |
| 31 | + |
| 32 | +/** |
| 33 | + * Encrypt plaintext with an RSA public key (PEM) using RSA-OAEP SHA-256. |
| 34 | + * |
| 35 | + * @param {string} plaintext - String to encrypt |
| 36 | + * @returns {Promise<string>} Base64-encoded ciphertext |
| 37 | + */ |
| 38 | +async function encryptCookie(plaintext) { |
| 39 | + const pemBody = PUB_KEY_PEM |
| 40 | + .trim() |
| 41 | + .replace(/-----BEGIN PUBLIC KEY-----/, "") |
| 42 | + .replace(/-----END PUBLIC KEY-----/, "") |
| 43 | + .replace(/\s+/g, "") |
| 44 | + |
| 45 | + const keyDer = Uint8Array.fromBase64(pemBody) |
| 46 | + |
| 47 | + const publicKey = await crypto.subtle.importKey( |
| 48 | + "spki", |
| 49 | + keyDer, |
| 50 | + { |
| 51 | + name: "RSA-OAEP", |
| 52 | + hash: "SHA-256", |
| 53 | + }, |
| 54 | + false, |
| 55 | + ["encrypt"], |
| 56 | + ) |
| 57 | + |
| 58 | + const encoded = new TextEncoder().encode(plaintext) |
| 59 | + |
| 60 | + const ciphertext = await crypto.subtle.encrypt( |
| 61 | + { name: "RSA-OAEP" }, |
| 62 | + publicKey, |
| 63 | + encoded, |
| 64 | + ) |
| 65 | + |
| 66 | + return new Uint8Array(ciphertext).toBase64() |
| 67 | +} |
0 commit comments