Course contentsShow
← Security
Lesson 2735 of 3,05251. Mobile SecurityFree lesson

Mobile Cryptography Best Practices

Proper key generation, storage in hardware-backed keystores, and avoiding common crypto mistakes.

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: SecRandomCopyBytes from the Security framework
  • Android: SecureRandom class

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 kSecAttrTokenIDSecureEnclave to 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

  1. Hard-coded keys: Never embed encryption keys in source code or resources
  2. ECB mode: Always use authenticated encryption modes (GCM, not ECB or plain CBC)
  3. Static IVs: Generate unique initialization vectors for each encryption operation
  4. Weak algorithms: Avoid MD5, SHA-1, DES—use AES-256, SHA-256+
  5. 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.