1). 加密庫-- aes-js 2). 建立加密工具類
// 擷取Aes加密
// npm i aes-js
let aes = require('aes-js');
// 預設KEY - An example 128-bit key
let _key = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ];
// 預設IV - The initialization vector (must be 16 bytes)
let _iv = [ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,35, 36 ];
/**
* @FileName: AesUtil
* @Author: mazaiting
* @Date: 2018/7/5
* @Description: AES加密工具類,使用方法
* 加密
* let encrypted = AesUtil.encrypt("mazaiting");
* 解密
* let decrypted = AesUtil.decrypt("mazaiting");
*/
class AesUtil {
/**
* 設定KEY
* @param key 密鑰
*/
static key(key) {
if (key.length / 16 !== 0 ) {
console.warn("密鑰無效...")
}
_key = key;
}
/**
* iv設定無效
* @param iv iv
*/
static iv(iv) {
if (iv.length / 16 !== 0 ) {
console.warn("密鑰無效...")
}
_iv = iv;
}
/**
* 加密
* @param text 加密文本
*/
static encrypt(text) {
// 将文本轉換為位元組 - Convert text to bytes
let textBytes = aes.utils.utf8.toBytes(text);
// 設定加密模式為 - ofb ( Output Feedback )
let aesOfb = new aes.ModeOfOperation.ofb(_key, _iv);
// 加密資料
let encryptedBytes = aesOfb.encrypt(textBytes);
// 将二進制轉換為16進制資料 - To print or store the binary data, you may convert it to hex
return aes.utils.hex.fromBytes(encryptedBytes);
}
/**
* 解密
* @param text 待解密文本
*/
static decrypt(text) {
// 将文本轉換為位元組 - When ready to decrypt the hex string, convert it back to bytes
let encryptedBytes = aes.utils.hex.toBytes(text);
// 設定 解密模式
// The output feedback mode of operation maintains internal state,
// so to decrypt a new instance must be instantiated.
let aesOfb = new aes.ModeOfOperation.ofb(_key, _iv);
// 解密資料
let decryptedBytes = aesOfb.decrypt(encryptedBytes);
// 将位元組資料轉換為字元串 - Convert our bytes back into text
return aes.utils.utf8.fromBytes(decryptedBytes);
}
}
/**
* 導出目前Module
*/
module.exports = AesUtil;
3). RN中對Storage資料存取進行加密
import React from 'react';
import {AsyncStorage} from 'react-native';
// npm install react-native-storage --save
import Storage from 'react-native-storage';
// 導入Aes加密工具類
import AesUtil from './../encrypt/AesEncrypt';
class LocalStorage extends Storage {
/**
* 存儲資料
* @param params 參數
*/
save(params) {
// console.log(params);
// 加密key
if (params.hasOwnProperty("key")) {
let key = params.key;
// 設定加密後的資料
params.key = AesUtil.encrypt(key)
}
// 加密data
if (params.hasOwnProperty("data")) {
let data = params.data;
let keys = Object.keys(data);
// 周遊所有的key
for (let i in keys) {
// 擷取鍵值
let key = keys[i];
let value = data[key];
// 加密鍵值
// let encryptKey = AesUtil.encrypt(key);
// 設定對應鍵的值
data[key] = AesUtil.encrypt(value);
}
// console.log("encrypt: " + JSON.stringify(data));
// 設定加密後的資料
params.data = data
}
// console.log(params);
super.save(params);
}
/**
* 加載資料
* @param params 參數清單
*/
load(params) {
// console.log(params);
// 加密key
if (params.hasOwnProperty("key")) {
let key = params.key;
// 設定加密後的資料
params.key = AesUtil.encrypt(key)
}
// console.log(params);
// 加載資料
return new Promise((resolve, reject) => {
return super.load(params).then(data => {
// 如果找到資料,解密
let keys = Object.keys(data);
// 周遊所有的key
for (let i in keys) {
// 擷取鍵值
let key = keys[i];
let value = data[key];
// 加密鍵值
// let encryptKey = AesUtil.encrypt(key);
// 設定對應鍵的值
data[key] = AesUtil.decrypt(value);
}
// console.log("en-data: " + JSON.stringify(data));
return resolve(data)
}).catch(reason => {
reject(reason)
})
});
}
}
const storage = new LocalStorage({
// 容量
size: 1000,
// RN 使用 AsyncStorage
storageBackend: AsyncStorage,
// 有效時間
defaultExpires: 1000 * 3600 * 24,
// 緩存資料在記憶體中
enableCache: true,
// 資料過期時調用
sync: {}
});
// RN
global.storage = storage;