Encryption & Cryptography
Encrypting values with RSA
Encryption is a necessity when it comes to a mobile application that communicates with external API. Sending sensitive information like access token, public/private key or user information might be spoofed if sent via plaintext.

RSA Encryption

API Reference Crypto
Using Crypto library, it is possible to generate a keypair locally and use it to fit the needs.
Example Client RSA Encryption
scripts/page1.ts
1
import Page1Design from 'generated/pages/page1';
2
import Crypto from '@smartface/native/global/crypto';
3
import Data from '@smartface/native/global/data';
4
import System from '@smartface/native/device/system';
5
import Label from '@smartface/native/ui/label';
6
import Button from '@smartface/native/ui/button';
7
import FlexLayout from '@smartface/native/ui/flexlayout';
8
import TextBox from '@smartface/native/ui/textbox';
9
10
const ENCRYPT_KEY_SIZE = 1024;
11
const ENCRYPT_CIPHER_TYPE = 'RSA/ECB/PKCS1Padding';
12
const PUBLIC_KEY_DEVICE_KEY = 'publicKey'; //The key to save to the device database
13
const PRIVATE_KEY_DEVICE_KEY = 'privateKey'; //The key to save to the device database
14
15
type KeyPairType = { publicKey: string, privateKey: string };
16
export default class Page1 extends Page1Design {
17
router: any;
18
myLabel: Label;
19
encryptButton: Button;
20
decryptButton: Button;
21
tbEncrypt: TextBox;
22
keyPair: KeyPairType;
23
private encryptedText = '';
24
constructor() {
25
super();
26
this.onShow = onShow.bind(this, this.onShow.bind(this));
27
this.onLoad = onLoad.bind(this, this.onLoad.bind(this));
28
this.myLabel = new Label();
29
this.encryptButton = new Button();
30
this.decryptButton = new Button();
31
this.tbEncrypt = new TextBox();
32
}
33
34
generateKeyPair(): KeyPairType {
35
let privateKey = Data.getStringVariable(PRIVATE_KEY_DEVICE_KEY);
36
let publicKey = Data.getStringVariable(PUBLIC_KEY_DEVICE_KEY);
37
if (!privateKey || !publicKey) {
38
const didGenerate = Crypto.RSA.generateKeyPair({
39
keySize: ENCRYPT_KEY_SIZE
40
});
41
if (!didGenerate) {
42
throw new Error('Could not generate keypair');
43
}
44
privateKey = didGenerate.privateKey;
45
publicKey = didGenerate.publicKey;
46
Data.setStringVariable(PUBLIC_KEY_DEVICE_KEY, publicKey); // Public key doesn't need to be stored securely
47
Data.setStringVariable(PRIVATE_KEY_DEVICE_KEY, privateKey); // Consider using secure data
48
}
49
return {
50
privateKey: privateKey,
51
publicKey: System.OS === 'iOS' ? Crypto.RSA.ios.getExportedPublicKey() : publicKey,
52
};
53
}
54
55
encrypt(text: string, key: string): string {
56
const keyBody = {
57
text: 'Smartface Inc.',
58
secretText: text
59
};
60
return Crypto.RSA.encrypt({
61
key,
62
cipherType: ENCRYPT_CIPHER_TYPE,
63
plainText: JSON.stringify(keyBody)
64
});
65
}
66
67
decrypt(encryptedText: string, key: string): string {
68
69
return Crypto.RSA.decrypt({
70
encryptedText: encryptedText,
71
key,
72
cipherType: ENCRYPT_CIPHER_TYPE
73
});
74
}
75
76
initLabel() {
77
this.myLabel.text = 'Decrpyted text will go here';
78
//@ts-ignore
79
this.layout.addChild(this.myLabel, 'myLabel', '.sf-label');
80
}
81
82
initButtons() {
83
this.keyPair = this.generateKeyPair();
84
const buttonWrapper = new FlexLayout();
85
//@ts-ignore
86
this.layout.addChild(buttonWrapper, 'buttonWrapper', '.sf-flexlayout', {
87
heigth: 120
88
});
89
this.encryptButton.text = 'Encrypt';
90
this.encryptButton.onPress = () => {
91
this.encryptedText = this.encrypt(this.tbEncrypt.text || '', this.keyPair.publicKey);
92
this.myLabel.text = this.encryptedText;
93
};
94
this.decryptButton.text = 'Decrypt';
95
this.decryptButton.onPress = () => {
96
const decryptedObjectText = this.decrypt(this.encryptedText, this.keyPair.privateKey);
97
const decrpytedObject = JSON.parse(decryptedObjectText);
98
this.myLabel.text = decrpytedObject.secretText;
99
};
100
//@ts-ignore
101
buttonWrapper.addChild(this.encryptButton, 'encryptButton', '.sf-button');
102
//@ts-ignore
103
buttonWrapper.addChild(this.decryptButton, 'decryptButton', '.sf-button');
104
}
105
106
initTextBox() {
107
this.tbEncrypt.hint = 'Enter text to encrypt';
108
//@ts-ignore
109
this.layout.addChild(this.tbEncrypt, 'tbEncrypt', '.sf-textbox');
110
}
111
}
112
113
function onShow(superOnShow) {
114
superOnShow();
115
}
116
117
function onLoad(superOnLoad) {
118
superOnLoad();
119
this.initTextBox();
120
this.initLabel();
121
this.initButtons();
122
this.layout.applyLayout();
123
}
Copied!
This code will store your generated public and private key in the device. The private key must be securely store into application.

Native Encryption

When storing sensitive information using Data on your device like in the example code, by default it is stored in plaintext to the device storage and can be retrieved by anyone who peeks into the application files.
Please head into Secure Data (Keystore) documentation to securely encrypt and make tough the extraction of your private key from device. There is no practical need to encrypt public key, since it is... intended to be public.
Last modified 3mo ago