nfldb_m2
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| nfldb_m2 [2026/05/31 14:35] โ admin | nfldb_m2 [2026/06/02 14:18] (current) โ admin | ||
|---|---|---|---|
| Line 20: | Line 20: | ||
| schedule sql | schedule sql | ||
| $sql = " | $sql = " | ||
| + | | ||
| + | encryption | ||
| + | | ||
| + | network first for start_url | ||
| + | if (event.request.mode === ' | ||
| + | event.respondWith( | ||
| + | // 1. Try the network first | ||
| + | fetch(event.request) | ||
| + | .then((response) => { | ||
| + | // Clone the response so we can put one in cache and return the other | ||
| + | const responseToCache = response.clone(); | ||
| + | | ||
| + | caches.open(CACHE_NAME).then((cache) => { | ||
| + | cache.put(event.request, | ||
| + | }); | ||
| + | | ||
| + | return response; | ||
| + | }) | ||
| + | .catch(() => { | ||
| + | // 2. Fallback to cache if network fails (offline) | ||
| + | return caches.match(event.request); | ||
| + | }) | ||
| + | ); | ||
| + | } | ||
| + | | ||
| + | javascriptasync function encryptData(plaintext, | ||
| + | // 1. Convert hex key to byte array | ||
| + | const keyBuffer = new Uint8Array(hexKey.match(/ | ||
| + | | ||
| + | // 2. Import the raw key into Web Crypto API | ||
| + | const cryptoKey = await crypto.subtle.importKey( | ||
| + | " | ||
| + | ); | ||
| + | |||
| + | // 3. Generate a random 16-byte initialization vector (IV) | ||
| + | const iv = crypto.getRandomValues(new Uint8Array(16)); | ||
| + | const encoder = new TextEncoder(); | ||
| + | const encodedPlaintext = encoder.encode(plaintext); | ||
| + | |||
| + | // 4. Encrypt the data | ||
| + | const ciphertextBuffer = await crypto.subtle.encrypt( | ||
| + | { name: " | ||
| + | ); | ||
| + | |||
| + | // 5. Convert IV and Ciphertext to Base64 | ||
| + | const ivBase64 = btoa(String.fromCharCode(...iv)); | ||
| + | const ciphertextBase64 = btoa(String.fromCharCode(...new Uint8Array(ciphertextBuffer))); | ||
| + | |||
| + | // 6. Combine them with a colon separator for transmission | ||
| + | return `${ivBase64}: | ||
| + | } | ||
| + | |||
| + | // Execution Example: | ||
| + | encryptData(" | ||
| + | .then(encryptedString => console.log(" | ||
| + | Use code with caution.๐ PHP Decryption (Server)The PHP script accepts the composite Base64 string, splits it back into the individual IV and ciphertext segments, and decrypts it using the built-in OpenSSL library.php<? | ||
| + | |||
| + | function decryptData($encryptedString, | ||
| + | // 1. Convert hex key back to binary | ||
| + | $key = hex2bin($hexKey); | ||
| + | |||
| + | // 2. Split the incoming payload into IV and Ciphertext components | ||
| + | $parts = explode(':', | ||
| + | if (count($parts) !== 2) { | ||
| + | return false; // Invalid payload structure | ||
| + | } | ||
| + | |||
| + | $iv = base64_decode($parts[0]); | ||
| + | $ciphertext = base64_decode($parts[1]); | ||
| + | |||
| + | // 3. Decrypt using native OpenSSL | ||
| + | $decrypted = openssl_decrypt( | ||
| + | $ciphertext, | ||
| + | ' | ||
| + | $key, | ||
| + | OPENSSL_RAW_DATA, | ||
| + | $iv | ||
| + | ); | ||
| + | |||
| + | return $decrypted; | ||
| + | } | ||
| + | |||
| + | // Execution Example: | ||
| + | $hexKey = " | ||
| + | $receivedPayload = $_POST[' | ||
| + | |||
| + | $decryptedMessage = decryptData($receivedPayload, | ||
| + | |||
| + | if ($decryptedMessage !== false) { | ||
| + | echo " | ||
| + | } else { | ||
| + | echo " | ||
| + | } | ||
nfldb_m2.1780238141.txt.gz ยท Last modified: 2026/05/31 14:35 by admin