2.4.2好像使用加密存储encryptjs,发布的时候有问题

好像是require的问题
https://docs.cocos.com/creator/manual/zh/advanced-topics/data-storage.html?h=setitem
麻烦看一下

https://www.jianshu.com/p/1158b2100513

ERROR: Uncaught Error: Cannot find module ‘readline’ 打包原生会报这个错呢 require(“readline”)的时候

楼主 你用链接里面下载的那个文件可以嘛?

对直接把下载的文件夹放在项目里面,
不要使用nodejs的encryptjs, 如果安装了先卸载

https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js
拿下来 导入为插件 我们就是这么用的 没什么问题

console.error(“md5:”,CryptoJS.MD5(“df”).toString())

因为encryptjs不仅支持浏览器环境 也支持nodejs环境,源码里面 里面require了nodejs的模块,而creator里面只是浏览器环境,所以会找不到nodejs模块readline和fs,所以要把引用nodejs相关的都注释掉就可以了,修改如下:
'use strict'; // var readline = require('readline'); // var fs=require('fs'); var rl = null; //Electron doesnt support stdin, so dont setup CLI if its not available. // try { // rl = readline.createInterface({ // input: process.stdin, // output: process.stdout // }); // } catch (e) { // rl = null; // console.log('Command line is not supported on this platform', e); // } encryptjs = { version: '1.0.0' };

参考地址:在creator中用encryptjs加密数据

如果大家是 Typescript 项目 需要写个声明文件 我这边

顺便 贴一个生成的 声明文件吧:

encryptjs.d.ts

`/** Declaration file generated by dts-gen */

export const version: string;

export function decrypt(ciphertext: any, password: any, nBits: any): any;

export function encrypt(plaintext: any, password: any, nBits: any): any;

export function getTextEncryptAndSaveToJSONFile(filePath: any, password: any, nBits: any): void;

export function getTextEncryptAndSaveToTextFile(filePath: any, password: any, nBits: any): void;

export function init(): void;

export function writeCipherTextToJSON(file: any, obj: any, options: any, callback: any): any;`

使用 :
`import {decrypt, encrypt} from “…/libs/encryptjs”;

const {ccclass, property} = cc._decorator;

@ccclass
export default class Helloworld extends cc.Component {

@property(cc.Label)
label: cc.Label = null;

@property
text: string = 'hello';

start () {
    // init logic
    this.label.string = this.text;
    var dataString = `sssssss`;//要加密的字符串
    let encryptedStr = encrypt(dataString,"123456",256);
    console.log("xxxx----:", JSON.stringify(encryptedStr));
    console.log("xxxx----:", JSON.stringify(encryptedStr));
    var cipherText = encryptedStr;//要解密的文本
    var dataString2= decrypt(cipherText,"123456",256);//得到解密之后的文本
    console.log("xxxx----:", JSON.stringify(dataString2));
    console.log("xxxx----:", JSON.stringify(dataString2));
}

}

`

你好,在TS里怎么引用encryptjs,加了声明文件,调用encrypt还是报错,

encryptdemo.zip (997.0 KB)

import { decrypt, encrypt } from “./libs/encryptjs”;

上面是我 重新 做了一个demo 给你。版本 2.4.8 理论上 2.1.3 以后 都可以用

我去下载了 最新的版本 简单的整合 和修改了一下 修改如上面我说的

这次 我把。这个 代码修改到里面了 就是


这个 我直接合并里面了 如果你不需要这些 可以自己尝试修改 自己去下载最新的即可

下载地址 encryptjs - npm

1赞

2…x可以,3.4里还是不行,还是感谢大佬

这个类库就俩文件 我用ts 重写了一下 下面 分别是重写后的文件 和 cocos creator 3.4.1 版本的项目 可以用的

encryptjs.zip (7.9 KB)

encryptdemo3.zip (1.4 MB)

如果 你想学习其他解决方案 可以自己参考一下 这个链接

2赞

感谢大佬 :pray: :pray: :pray: :pray: ,可以了