加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

可直接用于React Native开发各种加密标准的JavaScript库(MD5,AES

发布时间:2020-12-15 05:10:40 所属栏目:百科 来源:网络整理
导读:尊重版权,未经授权不得转载 本文来自brix 文章来自江清清的技术专栏(http://www.lcode.org) 项目库地址:https://github.com/brix/crypto-js (一).前言 之前很多朋友问有没有非常OK的,MD5,AES,DES相关库,需要在ReactNative上面进行使用。之前我也封装过一下

尊重版权,未经授权不得转载

本文来自brix 文章来自江清清的技术专栏(http://www.lcode.org)

项目库地址:https://github.com/brix/crypto-js

(一).前言

之前很多朋友问有没有非常OK的,MD5,AES,DES相关库,需要在ReactNative上面进行使用。之前我也封装过一下MD5和AES相关的库,但是那个是在原生模块基础上面进行封装的,使用起来非常不便捷。其实做加密操作,加密标准都是统一的,直接有相关JavaScript库可以用来使用,今天给大家推荐一款前端加密库,直接安装就即可在reactNative上面进行使用。常用的MD5,SHA等等一系列的加密方法都已经封装好了。

(二).安装

必须依赖环境

  1. Node.js
  2. npm (Node.js package manager)
?
1
npm install crypto-js
(三).引入方法

模块引入方法

1
2
3
4
var AES = require( "crypto-js/aes" );
SHA256 = require( "crypto-js/sha256" );
...
console.log(SHA256( "Message" ));

或者引导所有的库,进行方法调用

2
CryptoJS = require( "crypto-js" );
console.log(CryptoJS.HmacSHA1( , "Key" ));
(四).使用方法

这边简单介绍一下其中AES加密的使用方法。具体该库还有其他很多的标准加密的API请查看:https://code.google.com/archive/p/crypto-js/ 注意该地址请自带梯子访问。

AES加解密演示

4.1.普通文本AES加解密实例

4
5
6
7
8
9
10
);
// Encrypt
ciphertext = CryptoJS.AES.encrypt( 'my message' 'secret key 123' );
// Decrypt
bytes = CryptoJS.AES.decrypt(ciphertext.toString(),255)!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:auto!important; background:none!important">);
plaintext = bytes.toString(CryptoJS.enc.Utf8);
console.log(plaintext);

4.2.简单对象加解密实例

10
11
12
);
data = [{id: 1},{id: 2}]
// Encrypt
ciphertext = CryptoJS.AES.encrypt(JSON.stringify(data),255)!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:auto!important; background:none!important">);
// Decrypt
);
decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
console.log(decryptedData);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

相关内容
推荐文章
站长推荐
热点阅读