Blog | G5 Cyber Security

Secure Phone App Logins

TL;DR

Storing passwords directly on a phone is risky. Use the device’s secure storage (Keychain on iOS, KeyStore on Android) to store an encrypted token instead of usernames and passwords. Implement multi-factor authentication for extra security.

1. Why Not Store Passwords Directly?

Phone devices are vulnerable to:

Storing passwords in plain text or even weakly encrypted formats is a major security risk.

2. Using Secure Storage (Keychain/KeyStore)

Both iOS and Android provide secure storage mechanisms:

Instead of saving usernames and passwords directly, store an encrypted token.

3. Token Generation & Encryption

  1. User Registration/Login: When a user registers or logs in, verify their credentials against your server-side database.
  2. Generate a Token: If authentication is successful, generate a unique token (e.g., using UUIDs). This token should have an expiry time.
  3. Encrypt the Token: Use a strong encryption algorithm (AES) with a key derived from the user’s password or biometric data (if available). Never store the encryption key directly in your app code! The Keychain/KeyStore will manage this for you.
  4. Store Encrypted Token: Save the encrypted token to the device’s secure storage.

Example (Conceptual – specific implementation varies by platform):

// Pseudo-code - Encryption example

4. Authentication Flow

  1. App Launch: Check if an encrypted token exists in secure storage.
  2. Decrypt Token: If a token is found, attempt to decrypt it using the Keychain/KeyStore.
  3. Token Validation: Send the decrypted token to your server for validation. The server should:
    • Check if the token exists in its database.
    • Verify that the token hasn’t expired.
    • Ensure the token hasn’t been revoked (e.g., user logged out on another device).
  4. Grant Access: If the server validates the token, grant access to the app. Otherwise, prompt the user for login credentials.

5. Multi-Factor Authentication (MFA)

Add an extra layer of security with MFA:

MFA significantly reduces the risk of unauthorized access, even if a token is compromised.

6. Important Considerations

Exit mobile version