Google Trevor Johns Dec 12, 2013

Changes to SecretKeyFactory API in Android

Article Summary

Trevor Johns from Android Developer Relations reveals a breaking change that could silently corrupt encrypted data for apps using Unicode passphrases. If your app uses PBKDF2 key derivation, this affects you.

Android 4.4 KitKat introduced a subtle but critical change to SecretKeyFactory's PBKDF2WithHmacSHA1 implementation. The update fixed compliance with PKCS #5 standards by using all Unicode character bits instead of just the lower 8 bits, but this means different encryption keys are generated from the same passphrase on Android 4.4+ versus earlier versions.

Key Takeaways

Critical Insight

Apps using Unicode passphrases with PBKDF2 must implement version-specific key generation logic or risk users losing access to encrypted data after upgrading to Android 4.4+.

The article includes production-ready code showing exactly how to handle both old and new Android versions without breaking existing user data.

About This Article

Problem

When Android 4.4 KitKat arrived, its SecretKeyFactory started using the full range of Unicode bits instead of just the lower 8 bits. This broke apps that had generated keys on Android 4.3 or earlier using Unicode passphrases.

Solution

Google added a new PBKDF2WithHmacSHA1And8bit algorithm to keep the old 8-bit behavior working. Developers can check Build.VERSION.SDK_INT to use version-specific key generation logic and maintain compatibility across different Android versions.

Impact

Apps that use Unicode characters in passphrases with PBKDF2 can now use the legacy algorithm on older devices and switch to the PKCS #5-compliant version on Android 4.4 and later. This prevents data corruption from mismatched key derivation.