mirror of
https://github.com/zhitaocai/CocosCreator-Build-Encrypt.git
synced 2026-06-03 10:00:46 +08:00
图片和文本加密串最后加上10个随机数,解密时裁减掉
This commit is contained in:
@@ -93,6 +93,9 @@ if (CC_JSB) {
|
||||
function downloadText(item) {
|
||||
var url = item.url;
|
||||
var result = jsb.fileUtils.getStringFromFile(url);
|
||||
if (result) {
|
||||
result.substring(0, result.length - 10);
|
||||
}
|
||||
if (typeof result === "string" && result) {
|
||||
return result;
|
||||
} else {
|
||||
@@ -120,9 +123,10 @@ if (CC_JSB) {
|
||||
function downloadSrcText(item) {
|
||||
var url = item.url;
|
||||
var result = jsb.fileUtils.getStringFromFile(url);
|
||||
|
||||
if (result) {
|
||||
result.substring(0, result.length - 10);
|
||||
}
|
||||
if (typeof result === "string" && result) {
|
||||
// return jsb.reflection.callStaticMethod("x/y/z/zz", "d", "(Ljava/lang/String;)Ljava/lang/String;", result);
|
||||
return Base64.decode(result);
|
||||
} else {
|
||||
return new Error("Download text failed: " + url);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { RandomUtil } from "../utils/RandomUtil";
|
||||
import { TaskConfig } from "./TaskConfig";
|
||||
import { TaskInterface } from "./TaskInterface";
|
||||
|
||||
@@ -102,7 +103,6 @@ export class ImageEncryptTask implements TaskInterface {
|
||||
if (imgBuffer.toString().startsWith("data")) {
|
||||
return;
|
||||
}
|
||||
|
||||
let imgBase64String: string = "";
|
||||
switch (imgObj.type) {
|
||||
case ImageType.PNG:
|
||||
@@ -122,6 +122,8 @@ export class ImageEncryptTask implements TaskInterface {
|
||||
break;
|
||||
}
|
||||
imgBase64String += imgBuffer.toString("base64");
|
||||
// 最后加上10位随机数
|
||||
imgBase64String += RandomUtil.randomString(10);
|
||||
fs.writeFileSync(imgObj.filePath, imgBase64String);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import fs from "fs";
|
||||
import path from "path";
|
||||
import { TaskConfig } from "./TaskConfig";
|
||||
import { TaskInterface } from "./TaskInterface";
|
||||
import { RandomUtil } from "../utils/RandomUtil";
|
||||
|
||||
enum TextType {
|
||||
TXT,
|
||||
@@ -73,9 +74,12 @@ export class TextEncryptTask implements TaskInterface {
|
||||
*/
|
||||
private _encryptText(imgs: TextObject[]) {
|
||||
imgs.forEach((imgObj: TextObject) => {
|
||||
let imgBuffer: Buffer = fs.readFileSync(imgObj.filePath);
|
||||
let imgBase64String: string = imgBuffer.toString("base64");
|
||||
fs.writeFileSync(imgObj.filePath, imgBase64String);
|
||||
let buffer: Buffer = fs.readFileSync(imgObj.filePath);
|
||||
let encodeText: string = buffer.toString("base64");
|
||||
encodeText += RandomUtil.randomString(10);
|
||||
fs.writeFileSync(imgObj.filePath, encodeText);
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
10
src/utils/RandomUtil.ts
Normal file
10
src/utils/RandomUtil.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export class RandomUtil {
|
||||
private static _keys = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
||||
static randomString(length: number): string {
|
||||
let str = "";
|
||||
for (let i = 0; i < length; i++) {
|
||||
str += this._keys[Math.floor((this._keys.length - 1) * Math.random())];
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user