z-hao-wang / react-native-rsa

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

rsa.setPrivateString is not a funtion #11

Closed sreeksp closed 5 years ago

sreeksp commented 7 years ago

I just tried example given in read me. Can someone help whether i'm missing something else?

Actual use case is , i get the token as a param in url which is rsa encrypted. I need to decrypt it with a private certificate.

screen shot 2017-10-18 at 10 13 04 pm
z-hao-wang commented 7 years ago

What's your code looks like.

On Wed, Oct 18, 2017 at 10:14 PM, sreeksp notifications@github.com wrote:

I just tried example given .

[image: screen shot 2017-10-18 at 10 13 04 pm] https://user-images.githubusercontent.com/15642935/31755179-9df8aaf2-b451-11e7-99f8-945625009cfd.png

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/z-hao-wang/react-native-rsa/issues/11, or mute the thread https://github.com/notifications/unsubscribe-auth/ABjVg1NSVmbe0jxjzrr5J-1rUjUhgRc4ks5sttrIgaJpZM4P-tXD .

-- Hao Wang

sreeksp commented 7 years ago
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  View,
  Linking,
  Platform
} from 'react-native';

import { Header, Button } from './components/common';

const url = 'extrenalurl'
const rsa = require('react-native-rsa');

export default class App extends Component {

componentDidMount() {
  if (Platform.OS === 'android') {
    Linking.getInitialURL().then(url => {
      this.navigate(url);
    });
    } else {
      Linking.addEventListener('url', this.handleOpenURL);
    }
  }
  componentWillUnmount() {
    Linking.removeEventListener('url', this.handleOpenURL);
  }

  handleOpenURL = (event) => {
    console.log(event);
    let url = event.url;
    let params = url.split('param=')[1].split('&');
    //this.navigate(event.url);
    console.log(params);
    let privateKey = `privatekey`
    rsa.setPrivateString(privateKey);
    let decrypted = rsa.decrypt(params);
    console.log('------------------------------------');
    console.log(decrypted);
    console.log('------------------------------------');
  }
  render() {
    return (
        <View style={styles.container}>
          <Header /> 
          <View style={{flex:2/3, justifyContent:'center', alignItems:'center', marginTop:40}}>
          <Text style={styles.welcome}>
            Text Content
          </Text>
          <Text style={styles.instructions}>
            Text content
          </Text>
          <Text style={styles.instructions}>
            Text content
          </Text> 
          </View>
          <View style={{ flex: 1 / 2, justifyContent: 'center' }}>    
          <Button style={{ margin: 20 }} onPress={() => Linking.openURL(url)}>
            Pair phone
          </Button> 
        </View>  
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flexDirection: 'column',
    flex: 1,
  },
  welcome: {
    fontSize: 40,
    textAlign: 'center',
    fontWeight: '700',
    marginTop: 20,
    marginBottom:10,
    color: '#0d1c60',
  },
  instructions: {
    textAlign: 'center',
    color: '#000',
    fontWeight:'500',
    margin: 20,
    fontSize: 16,
  },
});
z-hao-wang commented 7 years ago

There might be some encoding issue or the encrypted data isn't in the right format. I would suggest to try to compare he date you are decrypting with the encrypted by this lib.

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

Then compare the encrypted string with your string

sreeksp commented 7 years ago

The above error is resolved after removing the import and declared RSAKey as var RSAKey = require('react-native-rsa') Now i'm getting JSON parse error, unexpected identifier ''whatever the privateKey is holding". The private key i'm passing is of type string. Does your library support passing .pem file as a privateKey?

var rsa = new RSAKey();
    var privateKey = `stringValue`;
    privateKey = privateKey.toString();
    console.log(privateKey);
    rsa.setPrivateString(privateKey);
    var decrypted = rsa.decrypt(params); 
    console.log(decrypted);
z-hao-wang commented 7 years ago

It does not support import pem key. You will need to pass in the raw key components.

On Thu, Oct 19, 2017 at 11:39 AM, sreeksp notifications@github.com wrote:

so, now i'm getting JSON parse error. does react-native-rsa support passing .pem file as private key

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/z-hao-wang/react-native-rsa/issues/11#issuecomment-337999458, or mute the thread https://github.com/notifications/unsubscribe-auth/ABjVg9ruWQW-QVgCF7ylcB2tW-BvWgPoks5st5d_gaJpZM4P-tXD .

-- Hao Wang

sreeksp commented 7 years ago

is rsa.setPrivateSting() expecting the raw key components to be in json format?

sreeksp commented 7 years ago

@z-hao-wang i have a pem file which holds the rsa private key. Can you help me in which format do i need to pass? or can you give a working example where you pass the raw contents of pem file to rsa.setPrivateString(). This is my code.

  var rsa = new RSAKey();
    console.log(JSON.stringify(app.privateKey));
    rsa.setPrivateString(JSON.stringify(app.privateKey));
    var decrypted = rsa.decrypt(externalUrlParams[0]);
    console.log(decrypted);

i'm getting Invalid rsa private key. But the same privateKey works in my other node application(using crypto2).

z-hao-wang commented 7 years ago

check the test file, it contains format. https://github.com/z-hao-wang/react-native-rsa/blob/master/__tests__/index.spec.js

const publicKey = '{"n":"860d193f420329a5db6cd7eee62f8dbdc710fbbfb87065f6a05de5b5a25af6320889767f7809d8fa167c0df1142bd329033407af06e51482bec5854e9af121a914402aff868589ef7db7924e478a2bed38d7367e4338ca23f57b6214e7bfcfd43102eb85570c7bfd1a3442909878cb82047227371d5cd8d7c7190332ebff0a6f","e":"10001"}';
const privateKey = '{"n":"860d193f420329a5db6cd7eee62f8dbdc710fbbfb87065f6a05de5b5a25af6320889767f7809d8fa167c0df1142bd329033407af06e51482bec5854e9af121a914402aff868589ef7db7924e478a2bed38d7367e4338ca23f57b6214e7bfcfd43102eb85570c7bfd1a3442909878cb82047227371d5cd8d7c7190332ebff0a6f","e":"10001","d":"30ae6114cfec461bad6c019f08890c7876ea7c024c00c586a306767d57d0a0c7dac1d0fec9a109e3087754b8d0127a44d29cfa8d5d7ba3cf376893d8cffbf1b4e6b33177d09ce0bd8a61b1661ccc828fd5ec65a7fb58f5960cbb5e85c44db1bf117c96bdfdaa220ffae415db689d0bea080f4e6c644156ecf643e7437e122301","p":"c1211509d92f269b070c4df3dada4a57f821a1fbcae230e60f354ab5af1ceb2651f526678f88e473fcf3347472c385b6b631ad1ebc705ca13d8f735aa3da639f","q":"b1b0986137bdfa6890d62a5e3aebf38554066dd36ec6cc5dcc9d91cf3a7dffa5d5767daaaebaecdff8cfa1f33428e02db4611b3a56f761201bba4b02d1c36731","dmp1":"66cc792a58d4643438b82ff61114086672c95433767697989d97bc40a1093f9192266f5ec86411563bda289da348b7afdda2eb8764be5a4fe938a62a24f565f9","dmq1":"60fb9c0fe3d6c327f3759126614e8b59c824c228d69b96cbd3746533101fd93af0297e297cb8f5b4c11ec2abf55a2211901438423d59441fbc428fd2781f08e1","coeff":"30065dadc7da627b6c564f6ece12040a18b7073822620f2cda8093e7083be82a1fa9f9922543b4c650750d8040ee6dda4ec67fbf161b6eb501bb1ee7f2079635"}';

As long as you can extract data with same format, you will be able to use them. So that means you can try to find some libs to extra raw data from a pem key.

sreeksp commented 7 years ago

@z-hao-wang i was able to get is this format

{
        "n": "AKxBNuRe-IobWh3BWnS3XkkHg6R0NSjw7fr_LMugmayY9_fIXMAndjMQBpYAoqlTH3Zz3J9bi9bzj0Wmej8ZQkvA-QgbmyzVhHnk_lQjvqSkszvhle6Z735Z8S5DcH8ceRyeYJWXgJX9eqrhfh3dFzvMZNsZId1W6LVAnm5tCioeNIZo72PZK8yyoNJTbRLUOOsv_yxvR7sHrdp8lXM5kWLsTrbOpjN8E4S6lj7bgBzyLuzy3JfcF3g4nXN1D6yLnJ4iRpGIPxl5RsnfJLYyVSiG-7CYnLu7Gk_yZheZpczVm0wUtotFRziV4DsoNGUNccktJgCQXELLDwlFMrZA5xM",
        "e": "AQAB",
        "d": "RRrUnsHLC_z1JkLPu0tlM_8jvPIx8X7Wun9sxTRk8m1b7bggHaa3ML0ZJ0yR9UQ3txm8ROJBM7b6n4KuQGotwp5kSfBpTI9MWmqX7cF5ViwNC9TwhYyUHCiRLXI4y4XswKJ5NQpWt9W9RJi6M9ji3Uaen5dxko6vRSfrZ3mvPj2_blW5HV8j3RIzJ3942CzQw2IX_hQWhityFxTwGY3f3Rh05jpI-SowJlOTIheOHqlR2VFTLTYIvmoZJduFzxlM4t5W3VzGZl_KmC1apNh2N3xPcmimq_YsrDqN5GgkNhuh_WTTB7pa3qsQlFPKnwylLc2LviQafiR-yunolImCwQ",
        "p": "ANL1Mi2H3rH4xwPYa7vVBCVqWEMrX6z872uNBXiVr0XKQQV0kHv5D3HKr28iqpJUgWPysjXCrOIendzNSRgiOpyCnQMHVK1vCncM-HdGrjNUcecqmXOEzABoME5VoeYQZqLIH8vCykJwOg436L7vQHN1zoRb8qo2kaU_c48lPalL",
        "q": "ANEIiXKSU6cpCfNsJozAWQFkEmUceqAiXVlLuN0VpNfu5SRtcXdZW0mVjX6vqHSsrTvmFf_rVWWF-gWMEhH_tkNoGSsfc13px1Q8kg6CdARihtEAXi6BZI9Ox_QDpuN3u2xVnv7V52IrCDVBj0-TaGP-NFvQUPacpn7JYjdE96RZ",
        "dp": "EzUN94VKamvbOudmj7iKYxm2pY76bBavedxiJiOCzNjoaKW_TfpURKkOphDC9QXKrh33ydtKwF_HQcZTJGZhbcSapR7yv8O_cS12Je4CiFvk7wY66ICrzlGuLEOxtO_DWIjeFEBNCBWNw6OlhlTkSl7sibKRepvzUNdhHyRl49c",
        "dq": "AJWkCuFeJ0yPcHQ2hBJW0GDShuijTpW8hB8cuiZrDCsY9ijxwDy0V0mCKlz62xlLVGiA-lbO3b9j_exirbz81jnDF-FrDme4p92Bzv1cHjnVXrXYEZQxRQ_iUfo5cwt790xCryO3dYEtVR7q4_EPkbejj0_6_TrOQdKZ0BnI4Y9h",
        "qi": "ALlFBlvCRspXYBOXf0XM5V0Q4t1vAxgcfNaKi4h612UZDF2GluYIwtuF-uJlxYvhRxq1gzmgP5-Z9gblMb6d8ysWfWPl2RWGYNw0nwOYhWI1_cWRNpfH7W-OY2kHmB3G1bHnf5PxUb-1JL2PvfXSfZ8JY1_xKqYSoSORJYQh2Z7D"
 }

This is giving the decrypted value as null. i converted the pem file to json web key. Anything i'm missing? i see that the sample you shared is a little different

z-hao-wang commented 7 years ago

Looks like you are using base64 encoded data. Make sure convert them to hex strings.

Hao Wang

On Oct 23, 2017, at 12:51 PM, sreeksp notifications@github.com wrote:

@z-hao-wang i was able to get is this formar

{ "n": "AKxBNuRe-IobWh3BWnS3XkkHg6R0NSjw7fr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URKkOphDC9QXKrh33ydtKwF_HQcZTJGZhbcSapR7yv8O_cS12Je4CiFvk7wY66ICrzlGuLEOxtO_DWIjeFEBNCBWNw6OlhlTkSl7sibKRepvzUNdhHyRl49c", "dq": "AJWkCuFeJ0yPcHQ2hBJW0GDShuijTpW8hB8cuiZrDCsY9ijxwDy0V0mCKlz62xlLVGiA-lbO3b9j_exirbz81jnDF-FrDme4p92Bzv1cHjnVXrXYEZQxRQ_iUfo5cwt790xCryO3dYEtVR7q4_EPkbejj0_6_TrOQdKZ0BnI4Y9h", "qi": "ALlFBlvCRspXYBOXf0XM5V0Q4t1vAxgcfNaKi4h612UZDF2GluYIwtuF-uJlxYvhRxq1gzmgP5-Z9gblMb6d8ysWfWPl2RWGYNw0nwOYhWI1_cWRNpfH7W-OY2kHmB3G1bHnf5PxUb-1JL2PvfXSfZ8JY1_xKqYSoSORJYQh2Z7D" } This is giving the decrypted value as null. i converted the pem file to json web key. Anything i'm missing? i see that the sample you shared is a little different

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

mudaligekosala commented 5 years ago

you can use this site to http://tomeko.net/online_tools/hex_to_base64.php to convert attrributes in PEM key to HEX format and format according to this

private key

{
"n":"BC4B9C6454C5FA515E55CE0338882D800254D994497A9D269BA7C7AEC8566A1FC510B6E9636FAA95FAA1047FC335B25F708452945F2A47B3BC57CE8D489FB0D123408740A2E468662A276245F67C39ADFC333916013C3B04D05AA5F2886D093E7212FEA5B606809FF881C6C1F0D1CBD8C59C144017D9F5AE206C", "e":"010001", "d":"1EF8E0F4AFED10CBA994BB9A54063255C80C64EDBB586BD919E9733310C90613CBBA801BCBBB6677DD0DF74B7CB4AC6E7E77858DDA497E19D6752B089A558A23D3E4378E54E35927B3B85AFAACDB5664F38C79B97401B09132064C7482A090032CDC657A9EDFE4502456B4BB684CCFB845539846F24AB42AF877E51CAC3184E1", "p":"F3D846CC801E22756BD954C09DCDA41C62579296C7F76543A725DF47091E98AEA4F10F2C5A94FFC877215C4B969FB210C9F3EBE2B60CFBCB2A9B6774FCBEE4B5", "q":"C5AE7689B02D01308E6D9EE134D29A805C8E1B9321725268A9BD994AE9DC4B0BEEF59D3C450F964F154DF6555DB485C28B30E7FEE676AA8790652F8D2006A30B", "dmp1":"0EED72881A5FBDE44A51E7AA52FEBFC92D0C07614BA48D8272BD12CA1A78D107FFF8F1E0D93ABDDDDBCD23054D9741235DF99CBB2841EB3828C792B1CAB734F1", "dmq1":"92A084963E923336B3FE8581A02FB2A01186EAD72850E6E5713A0C113003D1BA8945AF9A3BD382F727E34B1B28C5350E0736806E66BD9061BB6E026658, "coeff":"C573B3AD4093BE519556530A5220C10E0C0A00E3E74FE8021AF03765E96F49643DC47F7C912972EA7B9521BC5E4724DEBB613F2F011B9BA31F8B3A06D71D" }

public key

{
"n":"BC4B9C6454C5FA515E55CE0338882D800254D994497A9D269BA7C7AEC8566A1FC510B6E9636FAA95FAA1047FC335B25F708452945F2A47B3BC57CE8D489FB0D123408740A2E468662A276245F67C39ADFC333916013C3B04D05AA5F2886D093E7212FEA5B606809FF881C6C1F0D1CBD8C59C144017D9F5AE20", "e":"010001" }

coeff = InverseQ

z-hao-wang commented 5 years ago

closing