z-hao-wang / react-native-rsa

React native rsa crypto
MIT License
68 stars 19 forks source link

字段中有json的时候会报错 #5

Closed aggible closed 7 years ago

aggible commented 7 years ago

let ts = moment().format('YYYY-MM-DD HH:mm:ss'); let dataMap = { app_id: '2016120904080829', biz_content: '{"timeout_express":"30m","product_code":"QUICK_MSECURITY_PAY","total_amount":"0.01","subject":"1","body":"购买商品","out_trade_no":"18291829182"}', charset: 'utf-8', method: 'alipay.trade.app.pay', sign_type: 'RSA', timestamp: ts, version: '1.0' }; let paramspairs = .toPairs(dataMap); //参数排序 let sortedPairs = _.sortBy(paramspairs, function(n) { return n[0]; }); let keypairs = ''; for (var pi in sortedPairs) { keypairs = keypairs + sortedPairs[pi][0] + '=' + sortedPairs[pi][1] + '&'; } keypairs = .trimEnd(keypairs, '&'); // 上面是key排序,不用看

// let rsa = new RSAKey();
// for (var pi in sortedPairs) {
//   keypairs = keypairs + sortedPairs[pi][0] + '=' + sortedPairs[pi][1] + '&';
// }
// rsa.setPublicString(RSA_KEY);

会报一个json解析的错误

z-hao-wang commented 7 years ago

I can't run your sample code. If you can provide a piece of code I can run then maybe I can help.

Hao Wang

On Jan 23, 2017, at 8:53 PM, tr0j4n notifications@github.com<mailto:notifications@github.com> wrote:

let ts = moment().format('YYYY-MM-DD HH:mm:ss'); let dataMap = { app_id: '2016120904080829', biz_content: '{"timeout_express":"30m","product_code":"QUICK_MSECURITY_PAY","total_amount":"0.01","subject":"1","body":"购买商品","out_trade_no":"18291829182"}', charset: 'utf-8', method: 'alipay.trade.app.pay', sign_type: 'RSA', timestamp: ts, version: '1.0' }; let paramspairs = .toPairs(dataMap); //参数排序 let sortedPairs = _.sortBy(paramspairs, function(n) { return n[0]; }); let keypairs = ''; for (var pi in sortedPairs) { keypairs = keypairs + sortedPairs[pi][0] + '=' + sortedPairs[pi][1] + '&'; } keypairs = .trimEnd(keypairs, '&'); // 上面是key排序,不用看

// let rsa = new RSAKey(); // for (var pi in sortedPairs) { // keypairs = keypairs + sortedPairs[pi][0] + '=' + sortedPairs[pi][1] + '&'; // } // rsa.setPublicString(RSA_KEY);

会报一个json解析的错误

― You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/z-hao-wang/react-native-rsa/issues/5, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ABjVgwAchBWlB0xdrTdkdpEoFyfFZvQKks5rVYO-gaJpZM4Lr3Ms.

aggible commented 7 years ago

其实我想问的是,setPublicString的参数传入的难道不是RSA公钥吗?为何会报JSON parse错误呢?

let RSA_KEY='MIICXAIBAAKBgQC1UmpqNJwsi31DkEuBosmugTPplxSPmH32tR+zK...';
let kvToSign='k=v&k1=v2';
let rsa = new RSAKey();
rsa.setPublicString(RSA_KEY);
var encrypted = rsa.encrypt(kvToSign);

错误信息是 rsa.js的line 116, RSASetPublicString

JSON Parse error:Unexpected identifier "MIICXAIBAAKBgQC1UmpqNJwsi31DkEuBosmugTPplxSPmH32tR+zK"

aggible commented 7 years ago

好像有点奇怪啊,加密不是应该用私钥的吗?你这个文档是否可以给个示例啊?

let rsa = new RSAKey();
rsa.setPrivateString(RSA_KEY);
var encrypted = rsa.encrypt(keypairs);

好像这样用才对吧?

你这个 var publicKey = rsa.getPublicString(); // return json encoded string var privateKey = rsa.getPrivateString(); // return json encoded string

你这里是 json encoded string,我的private key就是一串字符串,怎么办

z-hao-wang commented 7 years ago

请看readme

var RSAKey = require('react-native-rsa'); const bits = 1024; const exponent = '10001'; // must be a string. This is hex string. decimal = 65537 var rsa = new RSAKey(); rsa.generate(bits, exponent); var publicKey = rsa.getPublicString(); // return json encoded string var privateKey = rsa.getPrivateString(); // return json encoded string

Encrypt

var rsa = new RSAKey(); rsa.setPublicString(publicKey); var originText = 'sample String Value'; var encrypted = rsa.encrypt(originText);

Hao Wang

On Jan 23, 2017, at 10:28 PM, tr0j4n notifications@github.com<mailto:notifications@github.com> wrote:

好像有点奇怪啊,加密不是应该用私钥的吗?你这个文档是否可以给个示例啊?

― You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/z-hao-wang/react-native-rsa/issues/5#issuecomment-274720954, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ABjVg5LVGCZ6xsBRW_IlP3me7L3S7LW-ks5rVZoGgaJpZM4Lr3Ms.

shiyunsheng commented 6 years ago

我们的key是服务器给的,只是一个string类型的。看源码是没办法加密解密的。不支持? @z-hao-wang