diff --git a/src/api/note.js b/src/api/note.js index b05f6b0..a5ff743 100644 --- a/src/api/note.js +++ b/src/api/note.js @@ -48,8 +48,12 @@ export function saveNote(noteForm, secret) { }); } - let bufferArrary = eval("[" + note.text + "]"); - let array = Uint8Array.from(bufferArrary); + // let bufferArrary = eval("[" + note.text + "]"); + // let array = Uint8Array.from(bufferArrary); + + let encoder = new TextEncoder() + let array = encoder.encode(note.text) + let blob = new Blob([array], { type: "application/octet-stream" }); let form = new FormData(); form.append("file", blob, noteForm.key); diff --git a/src/libs/secret.js b/src/libs/secret.js index bbbb494..51581b0 100644 --- a/src/libs/secret.js +++ b/src/libs/secret.js @@ -11,14 +11,14 @@ const wasmFlate = window.wasm_bindgen; export function wrap(text, secretKey) { text = "FLAGNOTE#" + text; - let result = aesEncrypt(text, secretKey); - result = noteZip(result); + let ui8ary = noteZip(text); + let result = aesEncrypt(convertUint8ArrayToWordArray(ui8ary), secretKey); return result; } -export function unwrap(storeText, secretKey) { - let result = noteUnzip(storeText); - result = aesDecrypt(result, secretKey); +export function unwrap(text, secretKey) { + let wdary = aesDecrypt(text, secretKey); + let result = noteUnzip(convertWordArrayToUint8Array(wdary)); if (result.startsWith("FLAGNOTE#")) { return result.substring(9); } @@ -37,7 +37,7 @@ export function aesEncrypt(word, keyWord) { let key = CryptoJS.enc.Utf8.parse(keyWord); let encrypted = CryptoJS.AES.encrypt(word, key, { mode: CryptoJS.mode.ECB, - padding: CryptoJS.pad.Pkcs7, + padding: CryptoJS.pad.ZeroPadding, }); return encrypted.toString(); } @@ -48,28 +48,24 @@ export function aesDecrypt(word, keyWord) { let key = CryptoJS.enc.Utf8.parse(keyWord); let decrypt = CryptoJS.AES.decrypt(word, key, { mode: CryptoJS.mode.ECB, - padding: CryptoJS.pad.Pkcs7, + padding: CryptoJS.pad.ZeroPadding, }); - return CryptoJS.enc.Utf8.stringify(decrypt).toString(); + return decrypt; } //base64 encode -export function encode(text) { - return Buffer.from(text, 'utf-8').toString('base64'); -} +// export function encode(text) { +// return Buffer.from(text, 'utf-8').toString('base64'); +// } //base64 decode -export function decode(text) { - return Buffer.from(text,'base64').toString('utf-8'); -} +// export function decode(text) { +// return Buffer.from(text,'base64').toString('utf-8'); +// } -export function noteUnzip(text) { - let charData = text.split(",").map(function (x) { - return parseInt(x); - }); - let binData = new Uint8Array(charData); - let data = unzip(binData); - text = String.fromCharCode.apply(null, new Uint8Array(data)); +export function noteUnzip(wdary) { + let data = unzip(wdary); + let text = new TextDecoder().decode(data); return text; } @@ -96,35 +92,35 @@ export function unzip(data){ return pako.ungzip(data); } -// function convertWordArrayToUint8Array(wordArray) { -// var len = wordArray.words.length, -// u8_array = new Uint8Array(len << 2), -// offset = 0, word, i -// ; -// for (i=0; i> 24; -// u8_array[offset++] = (word >> 16) & 0xff; -// u8_array[offset++] = (word >> 8) & 0xff; -// u8_array[offset++] = word & 0xff; -// } -// return u8_array; -// } +function convertWordArrayToUint8Array(wordArray) { + var len = wordArray.words.length, + u8_array = new Uint8Array(len << 2), + offset = 0, word, i + ; + for (i=0; i> 24; + u8_array[offset++] = (word >> 16) & 0xff; + u8_array[offset++] = (word >> 8) & 0xff; + u8_array[offset++] = word & 0xff; + } + return u8_array; +} -// function convertUint8ArrayToWordArray(u8Array) { -// var words = [], i = 0, len = u8Array.length; +function convertUint8ArrayToWordArray(u8Array) { + var words = [], i = 0, len = u8Array.length; -// while (i < len) { -// words.push( -// (u8Array[i++] << 24) | -// (u8Array[i++] << 16) | -// (u8Array[i++] << 8) | -// (u8Array[i++]) -// ); -// } + while (i < len) { + words.push( + (u8Array[i++] << 24) | + (u8Array[i++] << 16) | + (u8Array[i++] << 8) | + (u8Array[i++]) + ); + } -// return { -// sigBytes: words.length * 4, -// words: words -// }; -// } + return { + sigBytes: words.length * 4, + words: words + }; +} diff --git a/src/views/ViewNote.vue b/src/views/ViewNote.vue index e9b4d6d..4185a4a 100644 --- a/src/views/ViewNote.vue +++ b/src/views/ViewNote.vue @@ -589,8 +589,8 @@ export default { return; } - var bytes = new Uint8Array(e.target.result); - let bytesString = bytes.join(","); + let decoder = new TextDecoder(); + let bytesString = decoder.decode(e.target.result); that.noteForm.text = unwrap(bytesString, that.secret.secretKey); //that.noteForm.escapeText = getEscapeText(that.noteForm.text);