_includes/encrypted.html in no-style-please2-0.9.2 vs _includes/encrypted.html in no-style-please2-0.9.3

- old
+ new

@@ -33,15 +33,21 @@ <script> - - {% assign HEXKEY = '' | rand_bytes:32 %} - const keyData = "{{ '' | encrypt_key:page,HEXKEY ,encid}}" - const contentEnc = '{{ content | encrypt_content_v2:HEXKEY }}' +function uint8ArrayToHex(uint8Array) { + return Array.from(uint8Array) + .map(byte => byte.toString(16).padStart(2, '0')) // 转换每个字节为2位16进制 + .join(''); // 连接成字符串 +} + + + + {%- assign HEXKEY = '' | rand_bytes:32 %} + const contentEnc = '{{ content | encrypt_content_v2:HEXKEY }}'; !function(){ const substl = crypto.subtle const preFix = '{{encid}}'; @@ -60,58 +66,94 @@ name:"PBKDF2",hash: "SHA-256",iterations: 12345,salt: new TextEncoder().encode(salt) } return await substl.deriveBits(pbkdf2,key,256); } - async function decryptRaw(msg64,key){ - const base64str = msg64.substring(3) - const bfMsg = base64js.decode(base64str) - const bfIv = bfMsg.subarray(0,16) - const bfCipher = bfMsg.subarray(16) + async function decryptRaw(bf,key,outV){ + if(bf.length < 8){ + throw 'err' + } + let count = 0; + for (let i = 0; i < 4; i ++) { + count |= ((bf[i] ^ bf[i + 4] ^ i) << ((3 - i) * 8)) + } + if(outV){ + outV.count = count + } + if (bf.length < count) { + return + } + + let bfIv = bf.slice(4,20) + let bfCipher = bf.slice(20,count ) + var aeskey = { - name:"AES-CBC", + name:"AES-CTR", } var keyObj = await substl.importKey('raw',key,aeskey,false,['decrypt']) - var aesDec= {name: "AES-CBC",iv:bfIv} - var bfDec + var aesDec= {name: "AES-CTR",counter:bfIv,length:64}; try{ - bfDec = await substl.decrypt(aesDec,keyObj,bfCipher) + let bfDec = await substl.decrypt(aesDec,keyObj,bfCipher) + return new Uint8Array(bfDec) }catch (error) { - console.log(error) throw error } - - return new Uint8Array(bfDec) + } + + async function decryptBase64Msg(msg64,key){ + const base64str = msg64; + const bfMsg = base64js.decode(base64str) + return await decryptRaw(bfMsg,key) + + } async function checkKey(key){ - {% assign TestData = '' | rand_bytes:12 %} - var testData = '{{ TestData }}'; - var testDataEnc = '{{ TestData | encrypt_content_v2:HEXKEY }}'; + const keyData = "{{ '' | encrypt_key:page,HEXKEY ,encid}}"; + {% assign TestData = '' | rand_bytes: 200 %} + const testData = '{{ TestData }}'; + const testDataEnc = '{{ TestData | encrypt_content_v2:HEXKEY }}'; + const bfTestData = base64js.decode(testDataEnc) - let arrKeys = keyData.split('#') + + const bfKeyData = base64js.decode(keyData) let keyBf = null - for (let i = 0; i < arrKeys.length; i++) { - const element = arrKeys[i]; - try{ - - let d = await decryptRaw(element,key) - let dec = await decryptRaw(testDataEnc,d) - if((new TextDecoder).decode(dec) == testData ){ - keyBf = d; - break; + let C = 0; + let sum = 0; + while (C ++ < 400 ) { + if (sum >= bfKeyData.length ) { + break + } + let bfSub = bfKeyData.slice(sum) + + try { + let outV = {count:0} + let d = await decryptRaw(bfSub,key,outV) + + sum += outV.count + if (d) { + let dec = await decryptRaw(bfTestData,d) + let s = new TextDecoder().decode(dec) + if(s == testData){ + keyBf = d; + break; + } + }else{ + } - }catch (e){ + + + } catch (error) { + } } - + return keyBf } async function decrypt (key0,isCached){ // const key = Uint8Array([...]); // 32 bytes key - var key = '' if(isCached){ key = readKey() } else{ @@ -119,24 +161,16 @@ key = await genKey(keyS) } if (key.length == 0) { return } - - var aeskey = { - name:"AES-CBC", - length:256 - } - - var keyObj = await substl.importKey('raw',key,aeskey,false,['decrypt']) - var aesDec= {name: "AES-CBC",iv:bfIv} try { let keyBf = await checkKey(key) if(!keyBf){ throw 'error psw' } - var bfDec = await decryptRaw(contentEnc,keyBf) + var bfDec = await decryptBase64Msg(contentEnc,keyBf) var plain = new TextDecoder().decode(bfDec); setKey(key) document.getElementById("encrypted").style.display = 'none' // / show decrypted @@ -183,9 +217,10 @@ clearKey() } document.getElementById("ClearBtn1").onclick = function(){ localStorage.clear(); + document.getElementById('passwordinput').value = '' } document.getElementById("ClearBtn2").onclick = function(){ localStorage.clear(); } \ No newline at end of file