This commit is contained in:
Jesse-Ma
2023-03-06 15:55:37 +08:00
parent 5a7b53b815
commit 8242885c50
3 changed files with 67 additions and 15 deletions

View File

@@ -1,5 +1,7 @@
import CryptoJS from "crypto-js";
import pako from "pako";
import {gzip_encode_raw,gzip_decode_raw} from 'wasm-flate';
/**
* @word 要加密的内容
@@ -9,12 +11,12 @@ import pako from "pako";
export function wrap(text, secretKey) {
text = "FLAGNOTE#" + text;
let result = aesEncrypt(text, secretKey);
result = zip(result);
result = noteZip(result);
return result;
}
export function unwrap(storeText, secretKey) {
let result = unzip(storeText);
let result = noteUnzip(storeText);
result = aesDecrypt(result, secretKey);
if (result.startsWith("FLAGNOTE#")) {
return result.substring(9);
@@ -41,6 +43,7 @@ export function aesEncrypt(word, keyWord) {
//解密
export function aesDecrypt(word, keyWord) {
let key = CryptoJS.enc.Utf8.parse(keyWord);
let decrypt = CryptoJS.AES.decrypt(word, key, {
mode: CryptoJS.mode.ECB,
@@ -49,28 +52,76 @@ export function aesDecrypt(word, keyWord) {
return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}
//base64 encode
export function encode(text) {
return btoa(encodeURIComponent(text));
return Buffer.from(text, 'utf-8').toString('base64');
}
//base64 decode
export function decode(text) {
return decodeURIComponent(atob(text));
return Buffer.from(text,'base64').toString('utf-8');
}
export function unzip(text) {
export function noteUnzip(text) {
let charData = text.split(",").map(function (x) {
return parseInt(x);
});
let binData = new Uint8Array(charData);
let data = pako.ungzip(binData);
//text = String.fromCharCode.apply(null, new Uint8Array(data));
text = new Uint8Array(data).reduce(function (data, byte) {
return data + String.fromCharCode(byte);
}, "");
let data = unzip(binData);
text = String.fromCharCode.apply(null, new Uint8Array(data));
return text;
}
export function zip(text) {
text = pako.gzip(text, { to: "string" });
export function noteZip(text) {
let encoder = new TextEncoder();
let data = encoder.encode(text);
text = zip(data);
return text;
}
function zip(data){
if(gzip_encode_raw){
return gzip_encode_raw(data);
}
return pako.gzip(data);
}
function unzip(data){
if(gzip_decode_raw){
return gzip_decode_raw(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 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++])
// );
// }
// return {
// sigBytes: words.length * 4,
// words: words
// };
// }