Skip to content

Using Android Keystore for Secure Data Storage

The provided AndroidKeystore class allows developers to securely save and retrieve a string of data in the Android Keystore within a Unity application. The Android Keystore provides a secure storage mechanism for sensitive information such as cryptographic keys, passwords, or any other confidential data. Here’s a step-by-step guide on how to use this class:

Saving a String to the Android Keystore

To save a string securely in the Android Keystore, you can use the SaveStringToKeystore method:

public static void SaveStringToKeystore(string secret)
{
// Save a secret string to the Android Keystore
AndroidKeystore.SaveStringToKeystore("MySecretPassword123");
}

To retrieve the saved string from the Android Keystore, you can use the GetStringFromKeystore method:

public static string GetStringFromKeystore()
{
// Retrieve the saved string from the Android Keystore
string secret = AndroidKeystore.GetStringFromKeystore();
}

Usage Example

Here’s an example of how you can use the AndroidKeystore class to securely save and retrieve a string:

// Save a secret string to the Android Keystore
AndroidKeystore.SaveStringToKeystore("MySecretPassword123");
// Retrieve the saved string from the Android Keystore
string secret = AndroidKeystore.GetStringFromKeystore();
if (secret != null)
{
Debug.Log("Retrieved secret: " + secret);
}
else
{
Debug.Log("Secret not found or not running on Android.");
}

This example demonstrates how to save a secret string and then retrieve it securely from the Android Keystore within your Unity application.