网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事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行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**