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

@@ -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<len; i++) {
// word = wordArray.words[i];
// u8_array[offset++] = word >> 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<len; i++) {
word = wordArray.words[i];
u8_array[offset++] = word >> 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
};
}