您的位置:首页 > Web前端 > React

React Native crypt 加/解密

2018-04-13 00:00 561 查看

对称加密

React Native 端

Crypto-js

参考文档

github

安装

npm install --save crypto-js

使用

import CryptoJS from "crypto-js"
const iv =  CryptoJS.enc.Utf8.parse("xxxxxxxxxxxxxxxx").toString(CryptoJS.enc.Hex)
const key = 'xxxx'
let aes_option = {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
};
const CryptByCryptoJS = {

//** CBC加密 **
//params: 注意参数key为WordArray对象
//return: 密码对象 或者 密码对象Base64字符串
_aesEncryptWithMode(message) {
var ciphertext = CryptoJS.AES.encrypt(message, key, {
iv: CryptoJS.enc.Hex.parse(iv),
mode: CryptoJS.mode.CBC,
padding:CryptoJS.pad.Pkcs7
});
return ciphertext;//密码对象(Obejct类型,非WordArray类型),Base64编码。
return ciphertext.toString();//密码对象的Base64字符串

},

//** 解密CBC **
//params: 注意参数ciphertext 必须为 Base64编码的对象或者字符串。
_aesDecryptWithMode(ciphertext){
var decrypted = CryptoJS.AES.decrypt(ciphertext,key,{
iv: CryptoJS.enc.Hex.parse(iv),
mode: CryptoJS.mode.CBC,
padding:CryptoJS.pad.Pkcs7
});
return decrypted.toString(CryptoJS.enc.Utf8);//WordArray对象转utf8字符串
},

/**
* 普通电子加密
*
* @param {any} 需要加密的字符串
* @returns 加密后的字符串
*/
_aesEncryptECB (message){
return CryptoJS.AES.encrypt(message,key);
},

/**
* 解密
*
* @param {any} 需要解密的字符串
* @returns 解密后的字符串
*/
_aesDecryptECB (message){
return CryptoJS.AES.decrypt(message,key).toString(CryptoJS.enc.Utf8);
},

_encryptBase64(message){
var wordArray = CryptoJS.enc.Utf8.parse(message);
var base64 = CryptoJS.enc.Base64.stringify(wordArray);

return base64;
},

_decryptBase64(message){
var parsedWordArray = CryptoJS.enc.Base64.parse(message);
var parsedStr = parsedWordArray.toString(CryptoJS.enc.Utf8);

return parsedStr;
},
}

CSharp

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

参考代码

Java

Create a cipher instance:

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");


Generate key:

SecretKeyFactory factory = SecretKeyFactory.getInstance("1234567887654321");
KeySpec spec = new PBEKeySpec(passphrase.toCharArray(), hex(salt), iterationCount, keySize);
SecretKey key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");


Encrypt:

cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(hex(iv)));
byte[] encrypted = cipher.doFinal(bytes);


Decrypt:

cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(hex(iv)));
byte[] decrypted = cipher.doFinal(bytes);

参考代码

E.G.

key与iv要为16位,得到16的字符数组按照16进制编码转化为字符串

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import java.util.Arrays;
public static String encrypt(String content, String key) throws Exception {
try {
Key keySpec = new SecretKeySpec(key.getBytes(), "AES");    //两个参数,第一个为私钥字节数组, 第二个为加密方式 AES或者DES
String iv   = "1234567890123456";//初始化向量参数,AES 为16bytes. DES 为8bytes.
IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, keySpec,ivSpec);
//Cipher cipher = AesUtil.generateCipher(Cipher.ENCRYPT_MODE,"1234567890123456".getBytes(),"1234567890123456".getBytes());
byte[] byteResult = cipher.doFinal(content.getBytes());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < byteResult.length; i++) {
String hex = Integer.toHexString(byteResult[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

非对称加密

生成公钥和私钥(OpenSSL)

# 可以在终端输入 openssl 进入openssl 控制台,去掉下列命令钱的 openssl,再执行下面命令

#生成私钥
openssl genrsa -out rsa_private_key.pem 2048

#把RSA私钥转换成PKCS8格式
openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt -out rsa_private_key_pkcs8.pem

#生成公钥
openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem

react-native-rsa-native

React Native

参考文档
github

安装

$ yarn add react-native-rsa-native
or:
$ npm install react-native-rsa-native --save

Mostly automatic installation:
$ react-native link react-native-rsa-native

IOS

In your React Native Xcode project, right click on your project and go 'Add Files to ...', then navigate to /node_modules/react-native-rsa-native/ios and select the RNRSA.xcodeproj file. Then in the build settings for your target under 'Link Binary With Libraries', add libRNRSA.a.

使用

import {RSA, RSAKeychain} from 'react-native-rsa-native';
async encrypt(str){
var result = await RSA.encrypt(str, PublicString)
return result
}
async decrypt(encryptedMessage){
let result = await RSA.decrypt(encryptedMessage, PrivateString)
return result
}

CSharp

代码案例

https://www.codeproject.com/articles/210576/rsa-private-key-import-from-pem-format-in-csharp

http://www.cnblogs.com/dudu/p/csharp-openssl-encrypt-decrypt.html

https://social.msdn.microsoft.com/Forums/vstudio/en-US/d7e2ccea-4bea-4f22-890b-7e48c267657f/creating-a-x509-certificate-from-a-rsa-private-key-in-pem-file?forum=csharpgeneral

https://github.com/jrnker/CSharp-easy-RSA-PEM

Java

http://www.cnblogs.com/vicent/p/3805722.html

http://www.cnblogs.com/isItOk/p/5866870.html

参考文献

https://blog.csdn.net/black_dreamer/article/details/51902323

https://my.oschina.net/Jacker/blog/86383

http://outofmemory.cn/code-snippet/35524/AES-with-javascript-java-csharp-python-or-php

https://www.cnblogs.com/wz122889488/p/6899615.html

http://www.cnblogs.com/dudu/p/csharp-openssl-encrypt-decrypt.html

https://www.cnblogs.com/jtlgb/p/6762050.html

https://www.cnblogs.com/isaboy/p/csharp_openssl_rsa_jsencrypt.html

https://blog.csdn.net/gzy11/article/details/54573973

https://blog.csdn.net/gzy11/article/details/58609719
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息