AES encryption/decryption for react-native
npm install --save @ably/react-native-aesor
yarn add @ably/react-native-aescd ios
pod installThis library uses React Native TurboModules for optimal performance.
import { encrypt, decrypt } from '@ably/react-native-aes';
async function encryptData() {
try {
// Algorithm name (e.g., 'aes-128-cbc', 'aes-256-cbc', 'aes-128-ctr', 'aes-256-ctr')
const algorithm = 'aes-256-cbc';
// Convert your key, iv, and data to ArrayBuffers
const key = new Uint8Array(32); // 32 bytes for AES-256
const iv = new Uint8Array(16); // 16 bytes for AES IV
const data = new TextEncoder().encode('Hello World');
const encryptedData = await encrypt(
algorithm,
iv.buffer,
key.buffer,
data.buffer
);
console.log('Encrypted:', new Uint8Array(encryptedData));
// Decrypt the data
const decryptedData = await decrypt(
algorithm,
iv.buffer,
key.buffer,
encryptedData
);
const text = new TextDecoder().decode(decryptedData);
console.log('Decrypted:', text);
return text;
} catch (e) {
console.error('Encryption error:', e);
}
}encrypt(name: String, iv: ArrayBuffer, key: ArrayBuffer, data: ArrayBuffer) => Promise<ArrayBuffer>decrypt(name: String, iv: ArrayBuffer, key: ArrayBuffer, data: ArrayBuffer) => Promise<ArrayBuffer>
aes-128-cbc- AES-128 in CBC modeaes-192-cbc- AES-192 in CBC modeaes-256-cbc- AES-256 in CBC modeaes-128-ctr- AES-128 in CTR modeaes-192-ctr- AES-192 in CTR modeaes-256-ctr- AES-256 in CTR mode