szg1993
szg1993

Reputation: 43

Add RSA public key to Android KeyStore in Xamarin Forms

My application uses an RSA public key to encrypt the data before passed to the server. Everything works fine if I embed the public key to my solution and just use it.

However my client wants to change this public key sometimes, so I'm unable to add the PublicKey.key file to the solution as embedded resource. My first tought was that I should install this public key to the Keystore like I install an X509Cetificate under the settings -> security -> Install user certificate method on the phone. This method fails, because my file only contains the public key, which is not a certificate.

How can I store this public key in the Keystore? My goal is to install it, after that in would like to read it with my Xamarin Forms app in order to encrypt the data before I post to the server.

So my goal is not the set this public key to the keystore with code, it has to be there.

Upvotes: 0

Views: 387

Answers (1)

Wendy Zang - MSFT
Wendy Zang - MSFT

Reputation: 10978

You could try to save public key as string into Secure Storage.

To save a value for a given key in secure storage:

try
{
  await SecureStorage.SetAsync("oauth_token", "secret-oauth-token-value");
}
catch (Exception ex)
{
  // Possible that device doesn't support secure storage on device.
}

To retrieve a value from secure storage:

try
{
  var oauthToken = await SecureStorage.GetAsync("oauth_token");
}
catch (Exception ex)
{
  // Possible that device doesn't support secure storage on device.
}

For more details, refer to Xamarin.Essentials: Secure Storage: https://learn.microsoft.com/en-us/xamarin/essentials/secure-storage?context=xamarin%2Fandroid&tabs=ios

Upvotes: 0

Related Questions