Untitled
user_5549443
plain_text
a year ago
1.7 kB
3
Indexable
// 1. Using the built-in window.crypto API: You can use the window.crypto object to generate a key, encrypt a message with it, and send the encrypted message to a recipient. Here's an example: ```javascript // Generate a key const key = window.crypto.subtle.generateKey( { name: "AES-GCM", length: 256, }, true, // extractable ["encrypt", "decrypt"] ); // Encrypt a message with the key const plaintext = "This is a secret message"; const encoded = new TextEncoder().encode(plaintext); const iv = window.crypto.getRandomValues(new Uint8Array(12)); const ciphertext = window.crypto.subtle.encrypt( { name: "AES-GCM", iv: iv, }, key, encoded ); // Send the encrypted message to a recipient // ... ``` 2. Using a library like CryptoJS: CryptoJS is a popular library for performing cryptographic operations in JavaScript. You can use it to encrypt a message and then send the encrypted message to a recipient. Here's an example: ```javascript // Include CryptoJS library <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script> // Encrypt a message with a key and send the encrypted message to a recipient const key = "your-secret-key"; const plaintext = "This is a secret message"; const encrypted = CryptoJS.AES.encrypt(plaintext, key).toString(); // Send the encrypted message to a recipient // ... ``` It's important to note that encryption alone may not provide complete security for transmitting sensitive information over the internet. It's also important to consider using secure communication protocols (e.g., HTTPS) and key management practices to ensure the security of the encrypted messages.
Editor is loading...