This guide explains how to directly access methods from the rn-encryption
library in a React Native project, including usage examples for AES, RSA, Hashing, HMAC, Random String, and Base64 utilities.
- Library Installation
- Requirements
- Setup in React Native
- Direct Method Import
- API Overview
- Usage Examples
- Troubleshooting
- Best Practices
- FAQ
- Security Best Practices
Install the library using npm or yarn:
expo install @yourorg/native-encryption
# OR
npm install rn-encryption --save
# OR
yarn add rn-encryption
For Android:
cd android && ./gradlew clean && cd ..
npx react-native run-android
For iOS:
cd ios && pod install && cd ..
npx react-native run-ios
New architecture required. React native >= 0.76.5 Works with Expo Bare Workflow & Vanilla React Native
No additional configuration is required. The methods can be directly imported and used.
You can directly import the methods you need:
import {
encryptAES,
decryptAES,
encryptRSA,
decryptRSA,
hashSHA256,
hashSHA512,
hmacSHA256,
base64Encode,
base64Decode,
generateRandomString,
generateAESKey,
generateRSAKeyPair,
generateECDSAKeyPair,
signDataECDSA,
verifySignatureECDSA,
encryptAsyncAES,
decryptAsyncAES,
encryptAsyncRSA,
decryptAsyncRSA
} from 'rn-encryption';;
Each method can be accessed directly without a default object wrapper.
generateAESKey(keySize: number): string
encryptAES(data: string, key: string): string
decryptAES(data: string, key: string): string
generateRSAKeyPair(): keypair
encryptRSA(data: string, publicKey: string): string
decryptRSA(data: string, privateKey: string): string
hashSHA256(input: string): string
hashSHA512(input: string): string
hmacSHA256(data: string, key: string): string
generateRandomString(input: number): string
base64Encode(input: string): string
base64Decode(input: string): string
generateECDSAKeyPair(): keypair
signDataECDSA(data: string, key: string): string
verifySignatureECDSA(data: string,signatureBase64: string, key: string): boolean
encryptAsyncAES(data: string, key: string): Promise<string>
decryptAsyncAES(data: string, key: string): Promise<string>
encryptAsyncRSA(data: string, key: string): Promise<string>
decryptAsyncRSA(data: string, key: string): Promise<string>
import { useState } from 'react';
import { View, StyleSheet, Text, Button } from 'react-native';
import {
encryptAES,
decryptAES,
encryptRSA,
decryptRSA,
hashSHA256,
hashSHA512,
hmacSHA256,
base64Encode,
base64Decode,
generateRandomString,
generateAESKey,
generateRSAKeyPair,
generateECDSAKeyPair,
signDataECDSA,
verifySignatureECDSA,
encryptAsyncAES,
decryptAsyncAES,
encryptAsyncRSA,
decryptAsyncRSA
} from 'rn-encryption';
interface EncryptionError {
name: string;
message: string;
}
export default function DashboardScreen() {
const [result, setResult] = useState(''); // Encryption/Decryption result
function handleRSAEncryption() {
const plaintext = 'Hello, RSA Encryption!';
const generatedKeys = generateRSAKeyPair();
try {
// Step 1: Encrypt the plaintext using the Public Key
const encryptedData = encryptRSA(
plaintext,
generatedKeys.publicKey
);
// Step 2: Decrypt the encrypted data using the Private Key
const decryptedData = decryptRSA(
encryptedData,
generatedKeys.privateKey
);
// Step 3: Validation
if (decryptedData === plaintext) {
console.log('β
RSA Encryption and Decryption Successful!');
} else {
console.error('β Decrypted data does not match original plaintext!');
}
} catch (error) {
console.error('β οΈ RSA Error:', error);
}
}
async function handleAsyncRSAEncryption() {
const plaintext = 'Hello, RSA Encryption!';
const generatedKeys = generateRSAKeyPair();
try {
// Step 1: Encrypt the plaintext using the Public Key
const encryptedData = await encryptAsyncRSA(
plaintext,
generatedKeys.publicKey
);
// Step 2: Decrypt the encrypted data using the Private Key
const decryptedData = await decryptAsyncRSA(
encryptedData,
generatedKeys.privateKey
);
// Step 3: Validation
if (decryptedData === plaintext) {
console.log('β
RSA Encryption and Decryption Successful!');
} else {
console.error('β Decrypted data does not match original plaintext!');
}
} catch (error) {
console.error('β οΈ RSA Error:', error);
}
}
const handleAESEncryption = () => {
const sampleObject = {
name: 'John Doe',
age: 30,
roles: ['admin', 'editor'],
};
try {
const generatedKey = generateAESKey(256);
const jsonString = JSON.stringify(sampleObject);
const encryptedString = encryptAES(jsonString, generatedKey);
// Decrypt and parse JSON
const decryptedJsonString = decryptAES(encryptedString, generatedKey);
const decryptedObject = JSON.parse(decryptedJsonString);
console.log('Decrypted Object:', decryptedObject);
} catch (err: unknown) {
if (err instanceof Error) {
let error = err.cause as EncryptionError;
console.log('β Error:123', error.message);
} else {
console.log('β Unknown Error:', err);
}
setResult('An error occurred during encryption/decryption.');
}
};
const handleAsyncESEncryption = async () => {
const sampleObject = {
name: 'John Doe',
age: 30,
roles: ['admin', 'editor'],
};
try {
const generatedKey = generateAESKey(256);
const jsonString = JSON.stringify(sampleObject);
const encryptedString = await encryptAsyncAES(jsonString, generatedKey);
console.log('encrypted Object:', encryptedString);
// Decrypt and parse JSON
const decryptedJsonString = await decryptAsyncAES(encryptedString, generatedKey);
const decryptedObject = JSON.parse(decryptedJsonString);
console.log('Decrypted Object:', decryptedObject);
} catch (err: unknown) {
if (err instanceof Error) {
let error = err.cause as EncryptionError;
console.log('β Error:123', error.message);
} else {
console.log('β Unknown Error:', err);
}
setResult('An error occurred during encryption/decryption.');
}
};
const hashing = () => {
try {
console.log('--- Hashing ---');
const sha256Hash = hashSHA256('Hello Hashing');
console.log('SHA-256 Hash:', sha256Hash);
const sha512Hash = hashSHA512('Hello Hashing');
console.log('SHA-512 Hash:', sha512Hash);
} catch (err) {
console.log('error is', err);
}
};
const hmac = () => {
try {
console.log('--- HMAC ---');
const hmac = hmacSHA256('Hello HMAC', 'MyHMACKey');
console.log('HMAC-SHA256:', hmac);
} catch (err) {
console.log('error is', err);
}
};
const signData = () => {
const keyPair = generateECDSAKeyPair();
const data = 'Hello, ECDSA!';
const signature = signDataECDSA(data, keyPair.privateKey);
const isValid = verifySignatureECDSA(data, signature, keyPair.publicKey);
console.log('Signature:', signature);
console.log('Is Valid Signature:', isValid);
};
const base64 = ()=> {
try {
console.log('--- Base64 Encoding/Decoding ---');
const base64Encoded = base64Encode('Hello Base64 Encoding');
console.log('Base64 Encoded:', base64Encoded);
const base64Decoded = base64Decode(base64Encoded);
console.log('Base64 Decoded:', base64Decoded);
} catch (err) {
console.log('error is', err);
}
};
const createRandomString = () => {
try {
console.log('--- Utilities ---');
const randomString = generateRandomString(16);
console.log('Random String:', randomString);
} catch (err) {
console.log('error is', err);
}
};
return (
<View style={{ flex: 1, alignItems: 'center', paddingTop: 80 }}>
<Button title="Encrypt & Decrypt AES" onPress={handleAESEncryption} />
<Button title="Async Encrypt & Decrypt AES" onPress={handleAsyncESEncryption} />
<Button title="Encrypt & Decrypt RSA" onPress={handleRSAEncryption} />
<Button title="Encrypt & Decrypt RSA" onPress={handleAsyncRSAEncryption} />
<Button title="Hashing" onPress={hashing} />
<Button title="HMAC" onPress={hmac} />
<Button title="Base64 Encoding" onPress={base64} />
<Button title="Generate random" onPress={createRandomString} />
<Button title="Sign & Validate data" onPress={signData} />
<Text style={styles.resultText}>{result}</Text>
</View>
);
}
const styles = StyleSheet.create({
inputContainer: {
marginVertical: 20,
alignItems: 'center',
width: '80%',
},
textInput: {
borderWidth: 1,
borderColor: '#ccc',
borderRadius: 5,
padding: 10,
width: '100%',
marginTop: 10,
},
resultText: {
marginVertical: 20,
textAlign: 'center',
fontSize: 16,
},
counterWrapper: {
height: 150,
justifyContent: 'center',
alignItems: 'center',
},
counterView: {
width: 280,
height: 140,
},
text: {
marginBottom: 20,
fontSize: 16,
},
});
-
Library Not Found:
- Run
npx react-native link rn-encryption
. - Clean and rebuild the project.
- Run
-
AES Key Size Error:
- Ensure the AES key is 16, 24, or 32 characters.
-
RSA Key Parsing Issue:
- Verify the RSA key is in Base64-encoded PEM format.
-
Permission Issues:
- Ensure native permissions are set correctly in AndroidManifest.xml or iOS Podfile.
- Do Not Hardcode Keys: Use
.env
or secure storage for keys. - Handle Errors Gracefully: Wrap calls in
try-catch
blocks. - Validate Key Sizes: Ensure AES and RSA keys meet size requirements.
Q: Does the library support both Android and iOS?
A: Partially, rn-encryption
fully supports ios and encryptAES & decryptAES for Android platforms.
Q: Can I use the library in Expo?
A: Yes, if you're using Expo Bare Workflow.
Q: How do I debug encryption issues?
A: Add console logs and verify that keys and data are correctly passed.
- Use Strong Keys: Always use AES-256 for symmetric encryption and RSA-2048 for asymmetric encryption.
- Key Storage: Store keys securely using Android Keystore and iOS Keychain.
- Avoid Hardcoding Keys: Do not hardcode encryption keys directly in the app.
Feature | Android (JCA) | iOS (CryptoKit) |
---|---|---|
Symmetric Encryption | β AES-256-GCM | β AES-256-GCM |
Asymmetric Encryption | β RSA-2048 | β RSA-2048 |
Key Derivation | β PBKDF2 | β PBKDF2 / β HKDF |
Hashing | β SHA-256, β SHA-512 | β SHA-256, β SHA-512 |
Message Authentication | β HMAC-SHA256 | β HMAC-SHA256 |
Digital Signatures | β ECDSA | β ECDSA (via CryptoKit) |
Key Management | β Android Keystore | β iOS Keychain |
Initialization Vector (IV) | β SecureRandom (12/16 Bytes) | β Randomized IV (12 Bytes) |
Authentication Tag | β Built-in (GCM Mode) | β Built-in (GCM Mode) |
Error Handling | β Strong Validation | β Strong Validation |
Performance | β‘ Optimized for Android | β‘ Optimized for iOS |
Parallel Processing | β Supported in GCM | β Supported in GCM |
Cryptographic Library | β Java Cryptography (JCA) | β CryptoKit |