2024年大数据最新cocosCreator 之 crypto-es数据加密_cocos使用node-rsa(3),大数据开发性能优化推荐书

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

/*
加密前的数据: Hello Encrypt
加密后的数据: U2FsdGVkX1/cqGU/G7xGVONeYS4R6sRjZYkMeAmUcdQ=
解密后的数据: Hello Encrypt
*/


更多内容可参考: [npm crypto-es]( )


  

## 封装




---


在此感谢[oops-framework]( )开源框架的作者,对加密相关进行了封装。


主要文件实现在:[EncryptUtil.ts]( )



import CryptoES from “crypto-es”;

export class EncryptUtil {
private static _key: string = “”;
private static _iv: CryptoES.lib.WordArray = null;

// 初始化加密库
static initCrypto(key: string, iv: string) {
    this._key = key;
    this._iv = CryptoES.enc.Hex.parse(iv);
}

// MD5加密
static md5(msg: string) {
    return CryptoES.MD5(msg).toString();
}

// AES加密
static aesEncrypt(msg: string, key?: string, iv?: string): string {
    return CryptoES.AES.encrypt(
        msg,
        this._key,
        {
            iv: this._iv,
            format: this.JsonFormatter
        },
    ).toString();
}

// AES解密
static aesDecrypt(str: string, key?: string, iv?: string): string {
    const decrypted = CryptoES.AES.decrypt(
        str,
        this._key,
        {
            iv: this._iv,
            format: this.JsonFormatter
        },
    );
    return decrypted.toString(CryptoES.enc.Utf8);
}

private static JsonFormatter = {
    stringify: function (cipherParams: any) {
        const jsonObj: any = { ct: cipherParams.ciphertext.toString(CryptoES.enc.Base64) };
        if (cipherParams.iv) {
            jsonObj.iv = cipherParams.iv.toString();
        }
        if (cipherParams.salt) {
            jsonObj.s = cipherParams.salt.toString();
        }
        return JSON.stringify(jsonObj);
    },
    parse: function (jsonStr: any) {
        const jsonObj = JSON.parse(jsonStr);
        const cipherParams = CryptoES.lib.CipherParams.create(
            { ciphertext: CryptoES.enc.Base64.parse(jsonObj.ct) },
        );
        if (jsonObj.iv) {
            cipherParams.iv = CryptoES.enc.Hex.parse(jsonObj.iv)
        }
        if (jsonObj.s) {
            cipherParams.salt = CryptoES.enc.Hex.parse(jsonObj.s)
        }
        return cipherParams;
    },
};

}


简单的示例:



import { EncryptUtil } from ‘./EncryptUtil’;

const key = “encrypt”;
const value = “Thank you oops-framework”;
EncryptUtil.initCrypto(“key”, “vi”);
console.log(“加密前的数据:”, value);
let value_1 = EncryptUtil.aesEncrypt(value);
console.log(“加密后的数据:”, value_1);
let value_2 = EncryptUtil.aesDecrypt(value_1);
console.log(“解密后的数据:”, value_2);

/*
加密前的数据: Thank you oops-framework
加密后的数据:
{“ct”:“Vb2dOMFJ/7LCdPVa/VnpCj9+7cF3u48FuqRGhOlexmU=”,“iv”:“0ed38252b2cde8ee545bd527853dd6be”,“s”:“b7368ad0d8714ec7”}
解密后的数据: Thank you oops-framework
*/


  

## 结语




---


主要参考内容:


* [npm crypto-es]( )
* [oops-framework]( )


关于oops-framework的其他内容可参考原作者博客分享:


[dgflash\_game CSDN]( )


我个人也汇总了一篇关于oops的博客,可参考:


[oops Framework Creator游戏开发框架]( )


最后,祝大家学习生活愉快!




![img](https://img-blog.csdnimg.cn/img_convert/2cd7c5942da767ce35f1a91b8c23873b.png)
![img](https://img-blog.csdnimg.cn/img_convert/3130550c5059586eff9d94ff42d3f370.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值