-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
182 lines (170 loc) · 5.48 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import React from 'react';
import {StyleSheet, Text, TextInput, Button, View, ScrollView, Clipboard} from 'react-native';
import CryptoJS from 'crypto-js';
import autobind from 'autobind-decorator'
class WebxInput extends React.Component {
render() {
return (
<TextInput
{...this.props}
style={styles.input}
autoCorrect={false}
underlineColorAndroid='transparent'
/>
);
}
}
class WebxMultilineInput extends React.Component {
render() {
return (
<TextInput
{...this.props}
style={[styles.input, styles.multilineInput]}
multiline={true}
autoCorrect={false}
underlineColorAndroid='transparent'
blurOnSubmit={false}
/>
);
}
}
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
pwd1: "antidisestablishmentarianism",
pwd2: "honorificabilitudinitatibus",
pwd3: "WeiBuXin @_@ 微不信",
cipherText: "",
plainText: ""
};
}
@autobind
encrypt(text) {
return 'Webx!' + CryptoJS.AES.encrypt(text, this.state.pwd1 + '!' + this.state.pwd2 + '!' + this.state.pwd3);
}
@autobind
decrypt(text) {
if (text.startsWith('Webx!')) {
try {
let dec = CryptoJS.AES.decrypt(text.slice(5), this.state.pwd1 + '!' + this.state.pwd2 + '!' + this.state.pwd3);
return dec.toString(CryptoJS.enc.Utf8);
} catch (e) {
return null;
}
} else {
return null;
}
}
@autobind
updatePlainText(cipherText) {
let decTxt = this.decrypt(cipherText);
if (!decTxt) decTxt = "Sorry, I can't decrypt your text.";
this.setState({
cipherText: cipherText,
plainText: cipherText ? decTxt : "",
})
}
@autobind
updateCipherText(plainText) {
this.setState({
cipherText: plainText ? this.encrypt(plainText) : "",
plainText: plainText,
})
}
@autobind
async copy(field) {
await Clipboard.setString(this.state[field]);
}
@autobind
async paste(field, isCipher) {
const content = await Clipboard.getString();
this.setState({[field]: content});
if (isCipher) {
this.updatePlainText(content);
} else {
this.updateCipherText(content)
}
}
render() {
return (
<ScrollView
contentContainerStyle={styles.container}
keyboardShouldPersistTaps="handled"
>
<Text>Encryption Key 1</Text>
<WebxInput value={this.state.pwd1} onChangeText={(text) => {
this.setState({
pwd1: text,
}, () => this.updatePlainText(this.state.cipherText));
}}/>
<Text>Encryption Key 2</Text>
<WebxInput value={this.state.pwd2} onChangeText={(text) => {
this.setState({
pwd2: text,
}, () => this.updatePlainText(this.state.cipherText));
}}/>
<View style={styles.inlineContainer}>
<Text style={styles.inlineComponent}>Encrypted Text</Text>
<View style={styles.inlineComponent}>
<Button
onPress={() => this.copy('cipherText')}
title="Copy"
/>
</View>
<View style={styles.inlineComponent}>
<Button
onPress={() => this.paste('cipherText', true)}
title="Paste"
/>
</View>
</View>
<WebxMultilineInput value={this.state.cipherText} onChangeText={this.updatePlainText}/>
<View style={styles.inlineContainer}>
<Text style={styles.inlineComponent}>Plain Text</Text>
<View style={styles.inlineComponent}>
<Button
onPress={() => this.copy('plainText')}
title="Copy"
/>
</View>
<View style={styles.inlineComponent}>
<Button
onPress={() => this.paste('plainText', false)}
title="Paste"
/>
</View>
</View>
<WebxMultilineInput value={this.state.plainText} onChangeText={this.updateCipherText}/>
</ScrollView>
);
}
}
const styles = StyleSheet.create({
container: {
flexGrow: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
paddingVertical: 10
},
input: {
width: 240,
height: 40,
borderColor: 'gray',
borderWidth: 1
},
multilineInput: {
height: 80,
},
inlineContainer: {
flexWrap: 'wrap',
alignItems: 'flex-start',
flexDirection: 'row',
marginVertical: 5
},
inlineComponent: {
alignSelf: 'center',
marginHorizontal: 5
}
});