版本: 3.8.0
语言: typescript
环境: mac
简介
项目开发中,针对于一些明文数据,比如本地存储和http数据请求等,进行加密保护,是有必要的。
关于加密手段主要有:
- 对称加密 使用相同的密钥来加密和解密数据,常用的有
aes、des、3des
等 - 非对称加密 使用公钥加密,私钥解密,常用的有
rsa、dsa
等 - 哈希函数 将任意长度的数据映射为固定长度的哈希值,特点是不可逆,常用的有
md5、sha-1、sha-256
等 - 消息认证码(mac) 使用密钥对消息进行加密,并附加在消息中,以确保消息的完整性和真实性 常用的有
hmac
- 数字签名 用于对数据进行签名,以验证数据的来源和完整性,常用的有
rsa、dsa
等
有些时候为了数据的安全,也会使用base64的策略。
它只是一种将二进制数据转换为可打印字符的编码方式,属于编码算法而非加密算法。
该篇文章并非讲述加密算法的各种使用,主要说明内容: 在cocoscreator 3.x中对于明文数据的保护。
crypto-es
crypto-es
是 npm提供的用于加密和解密的包。
它提供了各种加密算法和工具,包括对称加密、哈希函数、数字签名和公钥加密等。此外,它还支持生成随机数、密码学密钥管理等。
在cocoscreator中使用crypto-es
,需要npm进行下载。打开终端,进入项目目录,然后运行命令:
npm install crypto-es
需要了解更多npm,可参考文档:npm的使用介绍
下载成功后,crypto-es
会放到项目目录 ./node_modules中。
然后在脚本中直接引用即可。
import cryptoes from "crypto-es";
简单的示例:
const key = "encrypt";
const value = "hello encrypt";
console.log("加密前的数据:", value);
let value_1 = cryptoes.aes.encrypt(value, key).tostring();
console.log("加密后的数据:", value_1);
let value_2 = cryptoes.aes.decrypt(value_1, key).tostring(cryptoes.enc.utf8);
console.log("解密后的数据:", value_2);
/*
加密前的数据: 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
*/
结语
主要参考内容:
关于oops-framework的其他内容可参考原作者博客分享:
我个人也汇总了一篇关于oops的博客,可参考:
最后,祝大家学习生活愉快!
发表评论