本文分享如何使用原生 JavaScript(Web Crypto API)實作 RSA 與 AES 混合加密解密。核心思路是:用 RSA-OAEP 加密一組隨機產生的 AES 金鑰,再用該 AES 金鑰(搭配 AES-CBC)加密實際資料,兼顧了非對稱加密的安全金鑰交換與對稱加密的高效資料處理。
<!doctype html> <html> <head> <meta charset='UTF-8'> </head> <body> <div class='test'></div> <script type="text/javascript"> function encrypt(data, keyJSON){ var data = new TextEncoder("UTF-8").encode(data); var randomsKeys = geneRandomHexStr(64); // 128 bit keys var encryptedKey = hexStringToUint8Array(randomsKeys); var aesAlgo = {name: 'aes-cbc', iv: hexStringToUint8Array("000102030405060708090a0b0c0d0e0f")}; return crypto.subtle.importKey("jwk", keyJSON, {name: "rsa-oaep", hash: {name: "sha-256"}},true, ['encrypt']) .then(function(publicKey){ return crypto.subtle.encrypt({name: "rsa-oaep"}, publicKey, encryptedKey); }).then(function(res){ encryptedKey = bytesToHexString(res) // use aes to encrypt data // import aes key return crypto.subtle.importKey('raw', hexStringToUint8Array(randomsKeys) , aesAlgo, false, ['encrypt', 'decrypt']); }).then(function(result){ // use aes to encode return crypto.subtle.encrypt(aesAlgo, result, data); }).then(function(encryptedData){ return Promise.resolve({ 'encrypted': bytesToHexString(encryptedData), 'encryptedKey': encryptedKey, }); }); } function decrypt(data, keyJSON){ // use local private key to decrypt var encryptedKey = new hexStringToUint8Array(data.encryptedKey); var encryptedData = new hexStringToUint8Array(data.encrypted); var aesAlgo = {name: 'aes-cbc', iv: hexStringToUint8Array("000102030405060708090a0b0c0d0e0f")}; // decrypt key return crypto.subtle.importKey('jwk', keyJSON, {name: "rsa-oaep", hash: {name: "sha-256"}}, true, ['decrypt']).then(function(privateKey){ return crypto.subtle.decrypt({name: 'rsa-oaep'}, privateKey, encryptedKey); }).then(function(decryptedKey){ // import aes key return crypto.subtle.importKey('raw', decryptedKey, aesAlgo, false, ['encrypt', 'decrypt']); }).catch(function(){ console.error("decrypt error"); }).then(function(result){ // decode encrypted data return crypto.subtle.decrypt(aesAlgo, result, encryptedData); }).then(function(data){ return Promise.resolve(new TextDecoder("UTF-8").decode(new Uint8Array(data))); }) } function createNewUserKey(){ var algorithmKeyGen = { name: "RSA-OAEP", hash: {name: "sha-256"}, modulusLength: 2048, publicExponent: new Uint8Array([0x01, 0x00, 0x01]), }; return crypto.subtle.generateKey(algorithmKeyGen, true, ['encrypt', 'decrypt']).then(function(result) { keyPairs = result; return Promise.all([crypto.subtle.exportKey("jwk", keyPairs.publicKey), crypto.subtle.exportKey("jwk", keyPairs.privateKey)]); }) } function hexStringToUint8Array(hexString) { if (hexString.length % 2 != 0) throw "Invalid hexString"; var arrayBuffer = new Uint8Array(hexString.length / 2); for (var i = 0; i < hexString.length; i += 2) { var byteValue = parseInt(hexString.substr(i, 2), 16); arrayBuffer[i/2] = byteValue; } return arrayBuffer; } function bytesToHexString(bytes) { if (!bytes) return null; bytes = new Uint8Array(bytes); var hexBytes = []; for (var i = 0; i < bytes.length; ++i) { var byteString = bytes[i].toString(16); if (byteString.length < 2) byteString = "0" + byteString; hexBytes.push(byteString); } return hexBytes.join(""); } function geneRandomHexStr(length){ var text = ""; var possible = "0123456789abcdef"; for( var i=0; i < length; i++ ) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; } createNewUserKey().then(function(keyPairs){ encrypt("this is origin text", keyPairs[0]).then(function(res){ console.log('public', JSON.stringify(keyPairs[0])); console.log('private', JSON.stringify(keyPairs[1])); decrypt(res, keyPairs[1]).then(function(decrypted){ console.log('decrypted', decrypted); }); }); }); </script> </body> </html> 這套流程先產生 RSA 金鑰對,加密時以公鑰包裝 AES 金鑰、再用 AES 加密明文;解密時先用私鑰還原 AES 金鑰,再解出明文。實務上要留意,範例中的 AES 初始向量(iv)為固定值,正式環境應改用隨機 iv 並妥善保存,以確保加密強度。以上就是本文的全部內容,希望對大家有所幫助。