-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
94 lines (88 loc) · 2.72 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
TextInput, TouchableOpacity, ScrollView,
Text,
View
} from 'react-native';
import { RSA, RSAKeychain } from 'react-native-rsa-native';// used library for encry and decryp.
export default class App extends React.Component {
constructor(props) {
super(props)
this.state = {
initialMessage: '',
sentMessage: '',
decryptedMessage: '',
encryptedMessage: '',
publicKey: '',
privateKey: ''
}
}
sendMessage() {
this.setState({
sentMessage: this.state.initialMessage
},()=>{
// after setting state we are calling the function
RSA.generate()
.then(keys => {
console.log(keys.private) // the private key
console.log(keys.public) // the public key
RSA.encrypt(this.state.sentMessage, keys.public)
.then(encodedMessage => {
this.setState({
encryptedMessage: encodedMessage,
publicKey: keys.public,
privateKey: keys.public
})
RSA.decrypt(encodedMessage, keys.private)
.then(message => {
console.log(message);
this.setState({
decryptedMessage: message
})
})
})
})
})
}
//view function
render() {
return (
<ScrollView>
<View style={styles.container}>
<TextInput
onChangeText={(input) => { this.setState({ initialMessage: input }) }}
underlineColorAndroid='transparent'
style={{ borderWidth: 1, width: 300, marginLeft: 10, marginTop: 30 }}
/>
<TouchableOpacity onPress={this.sendMessage.bind(this)}>
<Text style={styles.text}>Send Message</Text>
</TouchableOpacity>
{/* <Text style={{ marginTop: 10 }}>Public Key : {this.state.publicKey}</Text>
<Text style={{ marginTop: 10 }}>Private Key : {this.state.privateKey}</Text> */}
<Text style={{ marginTop: 10, fontSize: 15 }}>Sent Message : {this.state.sentMessage}</Text>
<Text style={{ marginTop: 10 }}>Encrypted Message : {this.state.encryptedMessage}</Text>
<Text style={{ marginTop: 10, fontSize: 15 }}>Decrypted Message : {this.state.decryptedMessage}</Text>
</View>
</ScrollView>
);
}
}
const styles = StyleSheet.create({
container: {
// flex: 1,
justifyContent: 'center',
// alignItems: 'center',
backgroundColor: '#F5FCFF',
},
text: {
marginTop: 20,
paddingVertical: 10,
marginLeft: 20,
marginRight: 20,
textAlign: 'center',
color: 'white',
backgroundColor: 'blue'
}
});