### Browser Integration Example Source: https://context7.com/xxtea/xxtea-js/llms.txt Complete HTML example showing XXTEA integration in a web page for encrypting form data before submission. It encrypts a message using a provided key and displays the encrypted output, also verifying decryption. ```html XXTEA Encryption Demo
``` -------------------------------- ### Initialize Mocha and Run Tests Source: https://github.com/xxtea/xxtea-js/blob/master/test/index.html Sets up Mocha for BDD testing and initiates the test runner. It also includes event listeners to capture test results and failures for reporting. ```javascript mocha.setup('bdd') function assert(expr, msg) { if (!expr) throw new Error(msg || 'failed'); } onload = function(){ //mocha.checkLeaks(); //mocha.globals(['xxtea']); var runner = mocha.run(); var failedTests = []; runner.on('end', function(){ window.mochaResults = runner.stats; window.mochaResults.reports = failedTests; }); runner.on('fail', logFailure); function logFailure(test, err){ var flattenTitles = function(test){ var titles = []; while (test.parent.title){ titles.push(test.parent.title); test = test.parent; } return titles.reverse(); }; failedTests.push({ name: test.title, result: false, message: err.message, stack: err.stack, titles: flattenTitles(test) }); }; }; ``` -------------------------------- ### Encrypt and Decrypt String with XXTEA in HTML Source: https://github.com/xxtea/xxtea-js/blob/master/README.md Include the xxtea.min.js script and use XXTEA.encryptToBase64 and XXTEA.decryptFromBase64 for string encryption and decryption. Ensure the original string matches the decrypted string. ```html XXTEA test ``` -------------------------------- ### XXTEA.encryptToBase64 Source: https://context7.com/xxtea/xxtea-js/llms.txt Encrypts a string using XXTEA and returns a Base64-encoded string. This is the recommended method for general use. ```APIDOC ## XXTEA.encryptToBase64(data, key) ### Description Encrypts a string using the XXTEA algorithm and returns the result as a Base64-encoded string. This is the recommended method for most use cases as Base64 output is safe for transmission over text-based protocols and storage in databases or JSON. ### Method `XXTEA.encryptToBase64` ### Parameters #### Arguments - **data** (string) - Required - The plaintext string to encrypt. - **key** (string) - Required - The encryption key. ### Request Example ```javascript var plaintext = "Hello World! \u4f60\u597d\uff0c\u4e2d\u56fd\uff01"; var key = "1234567890"; var encrypted = XXTEA.encryptToBase64(plaintext, key); console.log(encrypted); // Output: "D4t0rVXUDl3bnWdERhqJmFIanfn/6zAxAY9jD6n9MSMQNoD8TOS4rHHcGuE=" ``` ### Response #### Success Response - **string** - The Base64-encoded encrypted string. #### Response Example ``` "D4t0rVXUDl3bnWdERhqJmFIanfn/6zAxAY9jD6n9MSMQNoD8TOS4rHHcGuE=" ``` ``` -------------------------------- ### Encrypt String to Raw Binary with XXTEA Source: https://context7.com/xxtea/xxtea-js/llms.txt Use this method to obtain the raw binary encrypted string. This is useful for custom encoding implementations or when binary output is preferred. Handles null input by returning null. ```javascript // Raw binary encryption var plaintext = "Hello World!"; var key = "secretkey123"; var encryptedBinary = XXTEA.encrypt(plaintext, key); // Returns raw binary string (not human-readable) ``` ```javascript // Manual Base64 encoding equivalent to encryptToBase64 var encrypted = XXTEA.encrypt("test data", "mykey"); var base64 = btoa(encrypted); // Equivalent to: XXTEA.encryptToBase64("test data", "mykey") ``` ```javascript // Handle null/undefined input gracefully var result = XXTEA.encrypt(null, key); console.log(result); // Output: null ``` -------------------------------- ### Encrypt String to Base64 with XXTEA Source: https://context7.com/xxtea/xxtea-js/llms.txt Use this method for general encryption needs. It returns a Base64-encoded string suitable for transmission and storage. Handles empty or null input by returning it unchanged. ```javascript // Basic encryption to Base64 var plaintext = "Hello World! 你好,中国!"; var key = "1234567890"; var encrypted = XXTEA.encryptToBase64(plaintext, key); console.log(encrypted); // Output: "D4t0rVXUDl3bnWdERhqJmFIanfn/6zAxAY9jD6n9MSMQNoD8TOS4rHHcGuE=" ``` ```javascript // Encrypting sensitive data var userData = JSON.stringify({ username: "john", token: "abc123" }); var secretKey = "my-secret-key-16"; var encryptedData = XXTEA.encryptToBase64(userData, secretKey); console.log(encryptedData); // Output: Base64 encoded encrypted string ``` ```javascript // Handle empty or null input (returns unchanged) var result = XXTEA.encryptToBase64("", key); console.log(result); // Output: "" ``` -------------------------------- ### Encrypt and Decrypt Data with XXTEA in JavaScript Source: https://github.com/xxtea/xxtea-js/blob/master/dist/test.html Encrypts a string to Base64 and then decrypts it back using XXTEA. Ensure the key is consistent for both operations. ```javascript var str = "asd123"; var key = "1234567890"; var encrypt_data = XXTEA.encryptToBase64(str, key); console.log(encrypt_data); var decrypt_data = XXTEA.decryptFromBase64(encrypt_data, key); console.assert(str === decrypt_data); ``` -------------------------------- ### XXTEA.encrypt Source: https://context7.com/xxtea/xxtea-js/llms.txt Encrypts a string using XXTEA and returns the raw binary result as a string. Use this for custom encoding or when binary output is preferred. ```APIDOC ## XXTEA.encrypt(data, key) ### Description Encrypts a string using the XXTEA algorithm and returns the raw binary result as a string. Use this when you need the raw encrypted bytes, such as when implementing custom encoding or when binary output is preferred. ### Method `XXTEA.encrypt` ### Parameters #### Arguments - **data** (string) - Required - The plaintext string to encrypt. - **key** (string) - Required - The encryption key. ### Request Example ```javascript var plaintext = "Hello World!"; var key = "secretkey123"; var encryptedBinary = XXTEA.encrypt(plaintext, key); // Returns raw binary string (not human-readable) // Manual Base64 encoding equivalent to encryptToBase64 var encrypted = XXTEA.encrypt("test data", "mykey"); var base64 = btoa(encrypted); // Equivalent to: XXTEA.encryptToBase64("test data", "mykey") ``` ### Response #### Success Response - **string** - The raw binary encrypted string. #### Response Example ``` "\u001a\u000e\u001c\u000e\u001a\u000e\u001a\u000e\u001a\u000e\u001a\u000e" ``` ``` -------------------------------- ### XXTEA.decrypt Source: https://context7.com/xxtea/xxtea-js/llms.txt Decrypts raw binary XXTEA encrypted data back to the original plaintext string. This is the counterpart to `encrypt()`. ```APIDOC ## XXTEA.decrypt(data, key) ### Description Decrypts raw binary XXTEA encrypted data back to the original plaintext string. This is the counterpart to `encrypt()` and should be used when working with raw binary encrypted data. ### Method `XXTEA.decrypt` ### Parameters #### Arguments - **data** (string) - Required - The raw binary encrypted string to decrypt. - **key** (string) - Required - The encryption key used during encryption. ### Request Example ```javascript var key = "secretkey123"; var encrypted = XXTEA.encrypt("Hello World!", key); var decrypted = XXTEA.decrypt(encrypted, key); console.log(decrypted); // Output: "Hello World!" // Manual Base64 decoding equivalent to decryptFromBase64 var base64Data = "D4t0rVXUDl3bnWdERhqJmFIanfn/6zAxAY9jD6n9MSMQNoD8TOS4rHHcGuE="; var binaryData = atob(base64Data); var decrypted = XXTEA.decrypt(binaryData, "1234567890"); // Equivalent to: XXTEA.decryptFromBase64(base64Data, "1234567890") ``` ### Response #### Success Response - **string** - The original plaintext string. #### Response Example ``` "Hello World!" ``` ``` -------------------------------- ### Decrypt Raw Binary String with XXTEA Source: https://context7.com/xxtea/xxtea-js/llms.txt Decrypts raw binary XXTEA encrypted data back to its original plaintext. This is the counterpart to `encrypt()` and is used when working with raw binary encrypted data. Provides manual Base64 decoding equivalent to `decryptFromBase64`. ```javascript // Raw binary decryption var key = "secretkey123"; var encrypted = XXTEA.encrypt("Hello World!", key); var decrypted = XXTEA.decrypt(encrypted, key); console.log(decrypted); // Output: "Hello World!" ``` ```javascript // Manual Base64 decoding equivalent to decryptFromBase64 var base64Data = "D4t0rVXUDl3bnWdERhqJmFIanfn/6zAxAY9jD6n9MSMQNoD8TOS4rHHcGuE="; var binaryData = atob(base64Data); var decrypted = XXTEA.decrypt(binaryData, "1234567890"); // Equivalent to: XXTEA.decryptFromBase64(base64Data, "1234567890") ``` -------------------------------- ### XXTEA.decryptFromBase64 Source: https://context7.com/xxtea/xxtea-js/llms.txt Decrypts a Base64-encoded XXTEA encrypted string back to its original plaintext. The key must match the key used during encryption. ```APIDOC ## XXTEA.decryptFromBase64(data, key) ### Description Decrypts a Base64-encoded XXTEA encrypted string back to its original plaintext. The key must match the key used during encryption for successful decryption. ### Method `XXTEA.decryptFromBase64` ### Parameters #### Arguments - **data** (string) - Required - The Base64-encoded encrypted string to decrypt. - **key** (string) - Required - The encryption key used during encryption. ### Request Example ```javascript var encrypted = "D4t0rVXUDl3bnWdERhqJmFIanfn/6zAxAY9jD6n9MSMQNoD8TOS4rHHcGuE="; var key = "1234567890"; var decrypted = XXTEA.decryptFromBase64(encrypted, key); console.log(decrypted); // Output: "Hello World! \u4f60\u597d\uff0c\u4e2d\u56fd\ud83c\udde8\ud83c\uddf3\uff01" ``` ### Response #### Success Response - **string** - The original plaintext string. #### Response Example ``` "Hello World! \u4f60\u597d\uff0c\u4e2d\u56fd\ud83c\udde8\ud83c\uddf3\uff01" ``` ``` -------------------------------- ### Decrypt Base64 String with XXTEA Source: https://context7.com/xxtea/xxtea-js/llms.txt Decrypts a Base64-encoded string back to its original plaintext. Ensure the key matches the one used during encryption. Supports round-trip encryption/decryption and decryption of JSON data. ```javascript // Basic decryption from Base64 var encrypted = "D4t0rVXUDl3bnWdERhqJmFIanfn/6zAxAY9jD6n9MSMQNoD8TOS4rHHcGuE="; var key = "1234567890"; var decrypted = XXTEA.decryptFromBase64(encrypted, key); console.log(decrypted); // Output: "Hello World! 你好,中国🇨🇳!" ``` ```javascript // Round-trip encryption/decryption var original = "Secret message with émojis 🔐"; var key = "encryption-key"; var encrypted = XXTEA.encryptToBase64(original, key); var decrypted = XXTEA.decryptFromBase64(encrypted, key); console.assert(original === decrypted); // true ``` ```javascript // Decrypt JSON data var encryptedJson = XXTEA.encryptToBase64('{"id":1,"name":"test"}', "key"); var decryptedJson = JSON.parse(XXTEA.decryptFromBase64(encryptedJson, "key")); console.log(decryptedJson.name); // Output: "test" ``` -------------------------------- ### Encode String to UTF-8 with XXTEA Source: https://context7.com/xxtea/xxtea-js/llms.txt Encodes a JavaScript string into UTF-8 format. This utility function is exposed for manual UTF-8 encoding needs. ASCII strings are passed through unchanged, and Unicode characters, including emojis, are properly encoded. ```javascript // Encode Unicode string to UTF-8 var unicodeStr = "你好,世界!"; var utf8Encoded = XXTEA.utf8Encode(unicodeStr); // Returns UTF-8 encoded binary string ``` ```javascript // ASCII strings pass through unchanged var asciiStr = "Hello World"; var result = XXTEA.utf8Encode(asciiStr); console.log(result === asciiStr); // Output: true ``` ```javascript // Handling emoji and special characters var emojiStr = "Hello 🌍🚀"; var encoded = XXTEA.utf8Encode(emojiStr); // Properly encodes 4-byte UTF-8 sequences for emoji ``` -------------------------------- ### XXTEA.utf8Encode Source: https://context7.com/xxtea/xxtea-js/llms.txt Encodes a JavaScript string to UTF-8 format. This utility function is used internally but is also exposed for manual UTF-8 encoding. ```APIDOC ## XXTEA.utf8Encode(str) ### Description Encodes a JavaScript string to UTF-8 format. This utility function is used internally but is also exposed for cases where manual UTF-8 encoding is needed. ### Method `XXTEA.utf8Encode` ### Parameters #### Arguments - **str** (string) - Required - The string to encode to UTF-8. ### Request Example ```javascript // Encode Unicode string to UTF-8 var unicodeStr = "\u4f60\u597d\uff0c\u4e16\u754c\uff01"; var utf8Encoded = XXTEA.utf8Encode(unicodeStr); // Returns UTF-8 encoded binary string // ASCII strings pass through unchanged var asciiStr = "Hello World"; var result = XXTEA.utf8Encode(asciiStr); console.log(result === asciiStr); // Output: true // Handling emoji and special characters var emojiStr = "Hello \ud83c\udf0e\ud83d\ude80"; var encoded = XXTEA.utf8Encode(emojiStr); // Properly encodes 4-byte UTF-8 sequences for emoji ``` ### Response #### Success Response - **string** - The UTF-8 encoded string. #### Response Example ``` "\xe4\xbd\xa0\xe5\xa5\xbd\xef\xbc\x8c\xe4\xb8\x96\u754c\xef\xbc\x81" ``` ``` -------------------------------- ### Decode UTF-8 to JavaScript String Source: https://context7.com/xxtea/xxtea-js/llms.txt Decodes a UTF-8 encoded binary string back to a JavaScript string. The optional `n` parameter specifies the maximum number of UTF-16 code units to decode. ```javascript // Decode UTF-8 back to JavaScript string var utf8Data = XXTEA.utf8Encode("你好,世界!"); var decoded = XXTEA.utf8Decode(utf8Data); console.log(decoded); // Output: "你好,世界!" ``` ```javascript // Partial decoding with length limit var longString = "This is a longer string for testing"; var encoded = XXTEA.utf8Encode(longString); var partial = XXTEA.utf8Decode(encoded, 10); console.log(partial); // Output: "This is a " ``` ```javascript // Full round-trip with Unicode var original = "Mixed content: ABC 中文 🎉"; var roundTrip = XXTEA.utf8Decode(XXTEA.utf8Encode(original)); console.assert(original === roundTrip); // true ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.