Hamid Fathi
Hamid Fathi

Reputation: 310

how store dara safe and encrypted in Xamarin Android

I'm writing a bank payment app using Xamarin android that store user card information. I'm looking for a safe and secure for storing data. the only thing I found is below link that store account information which is useless for me:

https://developer.xamarin.com/recipes/cross-platform/xamarin-forms/general/store-credentials/

Upvotes: 0

Views: 627

Answers (1)

Taier
Taier

Reputation: 2119

KeyStore is the most secure way to store sensitive data on Android. You can store there anything, if it can be serialized it to string.

For example, you can use free library KeyChain.Net that provides clear API for saving and retreading data from Android and iOS KeyStores.

If you want to save Class with account information and credit card number, you can serealize that class using JSON Newtonsoft.Json

using System;
using Newtonsoft.Json;
public class Account
{
    public string user_id;
    public string credit_card_number;

    public override string ToString () {
       return JsonConvert.SerializeObject (this);
    }
}

Save Account:

string accountKey ="secure_key";

    public static bool SaveAccount (Account account) {
        KeyChainHelper keyChainHelper = new KeyChainHelper ();
        return keyChainHelper.SetKey (accountKey, account.ToString ());
    }

Get Account:

public static Account GetSavedAccount () {
    KeyChainHelper keyChainHelper = new KeyChainHelper ();
    string jsonAccount = keyChainHelper.GetKey (accountKey);
    if (jsonAccount == null || jsonAccount == "") {
        return null;
    }

    Account user = JObject.Parse (jsonAccount).ToObject<Account> ();
    return account;
}

Upvotes: 1

Related Questions