zip first

This commit is contained in:
Jesse-Ma
2023-03-23 11:34:41 +08:00
parent b31563aaab
commit b017e7bda1
3 changed files with 54 additions and 54 deletions

View File

@@ -48,8 +48,12 @@ export function saveNote(noteForm, secret) {
}); });
} }
let bufferArrary = eval("[" + note.text + "]"); // let bufferArrary = eval("[" + note.text + "]");
let array = Uint8Array.from(bufferArrary); // 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 blob = new Blob([array], { type: "application/octet-stream" });
let form = new FormData(); let form = new FormData();
form.append("file", blob, noteForm.key); form.append("file", blob, noteForm.key);

View File

@@ -11,14 +11,14 @@ const wasmFlate = window.wasm_bindgen;
export function wrap(text, secretKey) { export function wrap(text, secretKey) {
text = "FLAGNOTE#" + text; text = "FLAGNOTE#" + text;
let result = aesEncrypt(text, secretKey); let ui8ary = noteZip(text);
result = noteZip(result); let result = aesEncrypt(convertUint8ArrayToWordArray(ui8ary), secretKey);
return result; return result;
} }
export function unwrap(storeText, secretKey) { export function unwrap(text, secretKey) {
let result = noteUnzip(storeText); let wdary = aesDecrypt(text, secretKey);
result = aesDecrypt(result, secretKey); let result = noteUnzip(convertWordArrayToUint8Array(wdary));
if (result.startsWith("FLAGNOTE#")) { if (result.startsWith("FLAGNOTE#")) {
return result.substring(9); return result.substring(9);
} }
@@ -37,7 +37,7 @@ export function aesEncrypt(word, keyWord) {
let key = CryptoJS.enc.Utf8.parse(keyWord); let key = CryptoJS.enc.Utf8.parse(keyWord);
let encrypted = CryptoJS.AES.encrypt(word, key, { let encrypted = CryptoJS.AES.encrypt(word, key, {
mode: CryptoJS.mode.ECB, mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7, padding: CryptoJS.pad.ZeroPadding,
}); });
return encrypted.toString(); return encrypted.toString();
} }
@@ -48,28 +48,24 @@ export function aesDecrypt(word, keyWord) {
let key = CryptoJS.enc.Utf8.parse(keyWord); let key = CryptoJS.enc.Utf8.parse(keyWord);
let decrypt = CryptoJS.AES.decrypt(word, key, { let decrypt = CryptoJS.AES.decrypt(word, key, {
mode: CryptoJS.mode.ECB, mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7, padding: CryptoJS.pad.ZeroPadding,
}); });
return CryptoJS.enc.Utf8.stringify(decrypt).toString(); return decrypt;
} }
//base64 encode //base64 encode
export function encode(text) { // export function encode(text) {
return Buffer.from(text, 'utf-8').toString('base64'); // return Buffer.from(text, 'utf-8').toString('base64');
} // }
//base64 decode //base64 decode
export function decode(text) { // export function decode(text) {
return Buffer.from(text,'base64').toString('utf-8'); // return Buffer.from(text,'base64').toString('utf-8');
} // }
export function noteUnzip(text) { export function noteUnzip(wdary) {
let charData = text.split(",").map(function (x) { let data = unzip(wdary);
return parseInt(x); let text = new TextDecoder().decode(data);
});
let binData = new Uint8Array(charData);
let data = unzip(binData);
text = String.fromCharCode.apply(null, new Uint8Array(data));
return text; return text;
} }
@@ -96,35 +92,35 @@ export function unzip(data){
return pako.ungzip(data); return pako.ungzip(data);
} }
// function convertWordArrayToUint8Array(wordArray) { function convertWordArrayToUint8Array(wordArray) {
// var len = wordArray.words.length, var len = wordArray.words.length,
// u8_array = new Uint8Array(len << 2), u8_array = new Uint8Array(len << 2),
// offset = 0, word, i offset = 0, word, i
// ; ;
// for (i=0; i<len; i++) { for (i=0; i<len; i++) {
// word = wordArray.words[i]; word = wordArray.words[i];
// u8_array[offset++] = word >> 24; u8_array[offset++] = word >> 24;
// u8_array[offset++] = (word >> 16) & 0xff; u8_array[offset++] = (word >> 16) & 0xff;
// u8_array[offset++] = (word >> 8) & 0xff; u8_array[offset++] = (word >> 8) & 0xff;
// u8_array[offset++] = word & 0xff; u8_array[offset++] = word & 0xff;
// } }
// return u8_array; return u8_array;
// } }
// function convertUint8ArrayToWordArray(u8Array) { function convertUint8ArrayToWordArray(u8Array) {
// var words = [], i = 0, len = u8Array.length; var words = [], i = 0, len = u8Array.length;
// while (i < len) { while (i < len) {
// words.push( words.push(
// (u8Array[i++] << 24) | (u8Array[i++] << 24) |
// (u8Array[i++] << 16) | (u8Array[i++] << 16) |
// (u8Array[i++] << 8) | (u8Array[i++] << 8) |
// (u8Array[i++]) (u8Array[i++])
// ); );
// } }
// return { return {
// sigBytes: words.length * 4, sigBytes: words.length * 4,
// words: words words: words
// }; };
// } }

View File

@@ -589,8 +589,8 @@ export default {
return; return;
} }
var bytes = new Uint8Array(e.target.result); let decoder = new TextDecoder();
let bytesString = bytes.join(","); let bytesString = decoder.decode(e.target.result);
that.noteForm.text = unwrap(bytesString, that.secret.secretKey); that.noteForm.text = unwrap(bytesString, that.secret.secretKey);
//that.noteForm.escapeText = getEscapeText(that.noteForm.text); //that.noteForm.escapeText = getEscapeText(that.noteForm.text);