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.

Recommended Online RSA Encryption Test Tool credited by devglan.com

Example Client RSA Encryption

import Page1Design from 'generated/pages/page1';
import Crypto from '@smartface/native/global/crypto';
import Data from '@smartface/native/global/data';
import System from '@smartface/native/device/system';
import Label from '@smartface/native/ui/label';
import Button from '@smartface/native/ui/button';
import FlexLayout from '@smartface/native/ui/flexlayout';
import TextBox from '@smartface/native/ui/textbox';
const ENCRYPT_KEY_SIZE = 1024;
const PUBLIC_KEY_DEVICE_KEY = 'publicKey'; //The key to save to the device database
const PRIVATE_KEY_DEVICE_KEY = 'privateKey'; //The key to save to the device database
type KeyPairType = { publicKey: string, privateKey: string };
export default class Page1 extends Page1Design {
router: any;
myLabel: Label;
encryptButton: Button;
decryptButton: Button;
tbEncrypt: TextBox;
keyPair: KeyPairType;
private encryptedText = '';
constructor() {
this.onShow = onShow.bind(this, this.onShow.bind(this));
this.onLoad = onLoad.bind(this, this.onLoad.bind(this));
this.myLabel = new Label();
this.encryptButton = new Button();
this.decryptButton = new Button();
this.tbEncrypt = new TextBox();
generateKeyPair(): KeyPairType {
let privateKey = Data.getStringVariable(PRIVATE_KEY_DEVICE_KEY);
let publicKey = Data.getStringVariable(PUBLIC_KEY_DEVICE_KEY);
if (!privateKey || !publicKey) {
const didGenerate = Crypto.RSA.generateKeyPair({
if (!didGenerate) {
throw new Error('Could not generate keypair');
privateKey = didGenerate.privateKey;
publicKey = didGenerate.publicKey;
Data.setStringVariable(PUBLIC_KEY_DEVICE_KEY, publicKey); // Public key doesn't need to be stored securely
Data.setStringVariable(PRIVATE_KEY_DEVICE_KEY, privateKey); // Consider using secure data
return {
privateKey: privateKey,
publicKey: System.OS === 'iOS' ? Crypto.RSA.ios.getExportedPublicKey() : publicKey,
encrypt(text: string, key: string): string {
const keyBody = {
text: 'Smartface Inc.',
secretText: text
return Crypto.RSA.encrypt({
plainText: JSON.stringify(keyBody)
decrypt(encryptedText: string, key: string): string {
return Crypto.RSA.decrypt({
encryptedText: encryptedText,
initLabel() {
this.myLabel.text = 'Decrpyted text will go here';
this.layout.addChild(this.myLabel, 'myLabel', '.sf-label');
initButtons() {
this.keyPair = this.generateKeyPair();
const buttonWrapper = new FlexLayout();
this.layout.addChild(buttonWrapper, 'buttonWrapper', '.sf-flexlayout', {
heigth: 120
this.encryptButton.text = 'Encrypt';
this.encryptButton.onPress = () => {
this.encryptedText = this.encrypt(this.tbEncrypt.text || '', this.keyPair.publicKey);
this.myLabel.text = this.encryptedText;
this.decryptButton.text = 'Decrypt';
this.decryptButton.onPress = () => {
const decryptedObjectText = this.decrypt(this.encryptedText, this.keyPair.privateKey);
const decrpytedObject = JSON.parse(decryptedObjectText);
this.myLabel.text = decrpytedObject.secretText;
buttonWrapper.addChild(this.encryptButton, 'encryptButton', '.sf-button');
buttonWrapper.addChild(this.decryptButton, 'decryptButton', '.sf-button');
initTextBox() {
this.tbEncrypt.hint = 'Enter text to encrypt';
this.layout.addChild(this.tbEncrypt, 'tbEncrypt', '.sf-textbox');
function onShow(superOnShow) {
function onLoad(superOnLoad) {

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.