Mobile Cryptography Best Practices
You'll learn how to correctly generate, store, and use cryptographic keys on mobile devices while avoiding common mistakes that compromise security.
Why Mobile Crypto Is Different
Mobile cryptography requires special care because devices get lost, stolen, or physically compromised far more often than servers. You need cryptographic operations to be both secure and survive device compromise scenarios.
Proper Key Generation
Never generate keys from weak entropy sources. Use platform-provided secure random number generators:
- iOS:
SecRandomCopyBytesfrom the Security framework - Android:
SecureRandomclass
Think of key generation like creating a password: using predictable patterns (like sequential numbers) makes it trivial to guess. True randomness is non-negotiable.
Hardware-Backed Key Storage
The most critical practice: store keys in hardware-backed keystores, not in files or shared preferences.
- iOS: Use the Keychain with
kSecAttrTokenIDSecureEnclaveto leverage the Secure Enclave - Android: Use the KeyStore system with
setIsStrongBoxBacked(true)when available
Hardware-backed storage means keys never leave a secure chip, even if the OS is compromised. Cryptographic operations happen inside the hardware module—the key material itself is never exposed to your app code.
Common Mistakes to Avoid
- Hard-coded keys: Never embed encryption keys in source code or resources
- ECB mode: Always use authenticated encryption modes (GCM, not ECB or plain CBC)
- Static IVs: Generate unique initialization vectors for each encryption operation
- Weak algorithms: Avoid MD5, SHA-1, DES—use AES-256, SHA-256+
- Rolling your own crypto: Use platform APIs, not custom implementations
Key Takeaway: Generate keys with secure randomness, store them in hardware-backed keystores (Secure Enclave/StrongBox), and use platform-provided crypto APIs with modern algorithms and authenticated modes—never hard-code keys or implement custom cryptographic primitives.