Cordova Windows平台表单数据加解密与本地存储方案咨询
Hey there! I’ve got you covered for your Cordova Windows app encryption/storage needs since the simple-crypto plugin doesn’t support Windows. Here are a few solid alternatives and implementation steps:
Solutions for Encrypting/Decrypting & Storing Form Data in Cordova Windows Apps
1. Use the Web Crypto API (No Extra Plugins Needed)
Windows Cordova apps run on Edge WebView, which fully supports the native Web Crypto API. This is a lightweight, native alternative that avoids third-party plugin dependencies.
Key Steps:
- Generate an AES-GCM encryption key (ideal for symmetric encryption of form data)
- Convert form data to a usable format and encrypt it
- Decrypt stored data back to plaintext
- Use
cordova-plugin-fileto save/retrieve encrypted data to a private local file
Example Code:
Generate Encryption Key
async function generateEncryptionKey() { const key = await window.crypto.subtle.generateKey( { name: "AES-GCM", length: 256 }, true, // Allow key extraction for storage if needed ["encrypt", "decrypt"] ); return key; }
Encrypt Form Data
async function encryptFormData(plaintext, key) { const encoder = new TextEncoder(); const data = encoder.encode(plaintext); const iv = window.crypto.getRandomValues(new Uint8Array(12)); // GCM's recommended IV length const encryptedBuffer = await window.crypto.subtle.encrypt( { name: "AES-GCM", iv: iv }, key, data ); // Combine IV and encrypted data (we need the IV to decrypt later) return new Uint8Array([...iv, ...new Uint8Array(encryptedBuffer)]); }
Decrypt Stored Data
async function decryptStoredData(encryptedData, key) { // Split IV (first 12 bytes) from the encrypted content const iv = encryptedData.slice(0, 12); const encryptedContent = encryptedData.slice(12); const decryptedBuffer = await window.crypto.subtle.decrypt( { name: "AES-GCM", iv: iv }, key, encryptedContent ); const decoder = new TextDecoder(); return decoder.decode(decryptedBuffer); }
Save Encrypted Data to Local File
// First install the file plugin: `cordova plugin add cordova-plugin-file` async function saveEncryptedFile(encryptedData) { const appDir = await window.resolveLocalFileSystemURL(cordova.file.dataDirectory); const file = await appDir.getFile("encrypted-form-data.bin", { create: true, exclusive: false }); const writer = await file.createWriter(); writer.write(new Blob([encryptedData])); console.log("Form data saved securely!"); }
Retrieve & Decrypt Data from File
async function loadAndDecryptFile(key) { const filePath = cordova.file.dataDirectory + "encrypted-form-data.bin"; const fileEntry = await window.resolveLocalFileSystemURL(filePath); const file = await fileEntry.file(); const reader = new FileReader(); return new Promise((resolve, reject) => { reader.onload = async () => { const encryptedData = new Uint8Array(reader.result); const plaintextData = await decryptStoredData(encryptedData, key); resolve(plaintextData); }; reader.onerror = reject; reader.readAsArrayBuffer(file); }); }
2. Use CryptoJS with cordova-plugin-file
If you prefer a more straightforward, plugin-like API (similar to simple-crypto), CryptoJS is a popular JavaScript encryption library that works seamlessly in Cordova Windows apps.
Setup:
- Download CryptoJS and include it in your project (avoid CDNs for offline support)
- Install
cordova-plugin-fileas before
Example Code:
Encrypt & Save Data
// Assume CryptoJS is loaded via <script src="js/crypto-js.min.js"></script> function encryptWithCryptoJS(plaintext, secretKey) { const iv = CryptoJS.lib.WordArray.random(12); const encrypted = CryptoJS.AES.encrypt(plaintext, secretKey, { iv: iv }); // Return IV and encrypted data as a delimited base64 string return iv.toString(CryptoJS.enc.Base64) + ":" + encrypted.toString(); } async function saveCryptoJSData(encryptedString) { const appDir = await window.resolveLocalFileSystemURL(cordova.file.dataDirectory); const file = await appDir.getFile("form-data.txt", { create: true, exclusive: false }); const writer = await file.createWriter(); writer.write(encryptedString); }
Retrieve & Decrypt Data
function decryptWithCryptoJS(encryptedString, secretKey) { const [ivBase64, encryptedBase64] = encryptedString.split(":"); const iv = CryptoJS.enc.Base64.parse(ivBase64); const decrypted = CryptoJS.AES.decrypt(encryptedBase64, secretKey, { iv: iv }); return decrypted.toString(CryptoJS.enc.Utf8); } async function loadAndDecryptCryptoJSData(secretKey) { const filePath = cordova.file.dataDirectory + "form-data.txt"; const fileEntry = await window.resolveLocalFileSystemURL(filePath); const file = await fileEntry.file(); const reader = new FileReader(); return new Promise((resolve, reject) => { reader.onload = () => { const plaintextData = decryptWithCryptoJS(reader.result, secretKey); resolve(plaintextData); }; reader.onerror = reject; reader.readAsText(file); }); }
Critical Notes:
- Key Management: Never hardcode encryption keys in your app! Use Windows' built-in credential storage (via
cordova-plugin-windows-credentialsor direct Windows Runtime API calls) to store keys securely. - File Storage: Always use
cordova.file.dataDirectory—this is a private directory only accessible to your app, preventing unauthorized access. - Testing: Test encryption/decryption flows on physical Windows devices (not just emulators) to ensure full compatibility.
内容的提问来源于stack exchange,提问作者Ronit Roy




