diff --git a/1224/react-native-crypto-js.md b/1224/react-native-crypto-js.md new file mode 100644 index 0000000000000000000000000000000000000000..f56c30e7e9d8db828e742f75e07636f5d25899a2 --- /dev/null +++ b/1224/react-native-crypto-js.md @@ -0,0 +1,174 @@ +> 模板版本:v0.1.2 + +

+

react-native-crypto-js

+

+

+ + Supported platforms + + + License + +

+ + +> [!tip] [Github 地址](https://github.com/imchintan/react-native-crypto-js) + +## 安装与使用 + +进入到工程目录并输入以下命令: + + + +#### **yarn** + +```bash +yarn add react-native-crypto-js@^1.0.0 +``` + +#### **npm** + +```bash +npm install react-native-crypto-js@^1.0.0 --save +``` + + + +下面的代码展示了这个库的基本使用场景: + +```js +import React, { useState } from 'react'; +import { + ScrollView, + Text, + View, + TextInput, + Button, + Alert +} from 'react-native'; + +import CryptoJS from "react-native-crypto-js"; +//import CryptoJS from "rn-crypto-js"; + +//使用AES加密字符串 +function encrypt_str(text: string) { + let ciphertext = CryptoJS.AES.encrypt(text, 'secret key 123').toString(); + Alert.alert('加密后:', ciphertext, [{ text: 'OK' }]); + return ciphertext; +} + +//使用AES解密字符串 +function decrypt_str(text: string) { + let bytes = CryptoJS.AES.decrypt(text, 'secret key 123'); + let originalText = bytes.toString(CryptoJS.enc.Utf8); + Alert.alert('解密后:', originalText, [{ text: 'OK' }]); +} + +//使用AES加密对象 +function encrypt_obj(text: string) { + let ciphertext = CryptoJS.AES.encrypt(JSON.stringify(text), 'secret key 123').toString(); + Alert.alert('加密后:', ciphertext, [{ text: 'OK' }]); + return ciphertext; +} + +//使用AES解密对象 +function decrypt_obj(text: string) { + let bytes = CryptoJS.AES.decrypt(text, 'secret key 123'); + let originalText = JSON.parse(bytes.toString(CryptoJS.enc.Utf8)); + Alert.alert('解密后:', originalText, [{ text: 'OK' }]); + return originalText; +} + +//使用MD5加密字符串 +function MD5_encrypt_str(text: string) { + let ciphertext = CryptoJS.MD5(text).toString(); + Alert.alert('加密后:', ciphertext, [{ text: 'OK' }]); + return ciphertext; + +} + +//使用HmacMD5加密字符串 +function HMD5_encrypt_str(text: string) { + let ciphertext = CryptoJS.HmacMD5(text, 'secret key 123').toString(); + Alert.alert('加密后:', ciphertext, [{ text: 'OK' }]); + return ciphertext; + +} + +export const ReactNativeCryptoJsExample = () => { + const [cryptText, setCryptText] = useState('test 123'); + const [cryptText1, setCryptText1] = useState('test123'); + const [cryptText2, setCryptText2] = useState('test123'); + const [decryptText, setDecryptText] = useState(''); + const [cryptObj, setCryptObj] = useState('[{id: 1}, {id: 2}]'); + const [decryptObj, setDecryptObj] = useState(''); + return ( + + 测试使用AES算法加解密字符串 + + ) => setCryptText(cryptText)} defaultValue={cryptText} /> + +