示例#1
0
        static Task <string> PlatformGetAsync(string key)
        {
            var context = Platform.AppContext;

            string defaultEncStr = null;
            var    encStr        = Preferences.Get(Utils.Md5Hash(key), defaultEncStr, Alias);

            string decryptedData = null;

            if (!string.IsNullOrEmpty(encStr))
            {
                var encData = Convert.FromBase64String(encStr);
                var ks      = new AndroidKeyStore(context, Alias, AlwaysUseAsymmetricKeyStorage);
                try
                {
                    decryptedData = ks.Decrypt(encData);
                }
                catch (AEADBadTagException)
                {
                    System.Diagnostics.Debug.WriteLine($"Unable to decrypt key, {key}, which is likely due to an app uninstall. Removing old key and returning null.");
                    PlatformRemove(key);
                }
            }

            return(Task.FromResult(decryptedData));
        }
        static Task <string> PlatformGetAsync(string key)
        {
            var context = Platform.AppContext;

            string encStr           = default;
            var    foundLegacyValue = false;

            if (LegacyKeyHashFallback)
            {
                // If not found, could have been previously stored with md5 key
                if (!Preferences.ContainsKey(key, Alias))
                {
                    // If previously stored with md5 key, save with new key
                    var md5Key = Md5Hash(key);
                    if (Preferences.ContainsKey(md5Key, Alias))
                    {
                        encStr = Preferences.Get(md5Key, null, Alias);
                        Preferences.Set(key, encStr, Alias);
                        foundLegacyValue = true;

                        try
                        {
                            Preferences.Remove(md5Key, Alias);
                        }
                        catch
                        {
                        }
                    }
                }
            }

            if (!foundLegacyValue)
            {
                encStr = Preferences.Get(key, null, Alias);
            }

            string decryptedData = null;

            if (!string.IsNullOrEmpty(encStr))
            {
                try
                {
                    var encData = Convert.FromBase64String(encStr);
                    lock (locker)
                    {
                        var ks = new AndroidKeyStore(context, Alias, AlwaysUseAsymmetricKeyStorage);
                        decryptedData = ks.Decrypt(encData);
                    }
                }
                catch (AEADBadTagException)
                {
                    System.Diagnostics.Debug.WriteLine($"Unable to decrypt key, {key}, which is likely due to an app uninstall. Removing old key and returning null.");
                    Remove(key);
                }
            }

            return(Task.FromResult(decryptedData));
        }
示例#3
0
        static Task PlatformSetAsync(string key, string data)
        {
            var context = Platform.AppContext;

            var ks            = new AndroidKeyStore(context, Alias, AlwaysUseAsymmetricKeyStorage);
            var encryptedData = ks.Encrypt(data);

            var encStr = Convert.ToBase64String(encryptedData);

            Preferences.Set(Utils.Md5Hash(key), encStr, Alias);

            return(Task.CompletedTask);
        }
        static Task <string> PlatformGetAsync(string key)
        {
            var context = Platform.CurrentContext;

            string encStr;

            using (var prefs = context.GetSharedPreferences(Alias, FileCreationMode.Private))
                encStr = prefs.GetString(Utils.Md5Hash(key), null);

            var encData = Convert.FromBase64String(encStr);

            var ks            = new AndroidKeyStore(context, Alias, AlwaysUseAsymmetricKeyStorage);
            var decryptedData = ks.Decrypt(encData);

            return(Task.FromResult(decryptedData));
        }
        static Task PlatformSetAsync(string key, string data)
        {
            var context = Platform.AppContext;

            var ks            = new AndroidKeyStore(context, Alias, AlwaysUseAsymmetricKeyStorage);
            var encryptedData = ks.Encrypt(data);

            using (var prefs = context.GetSharedPreferences(Alias, FileCreationMode.Private))
                using (var prefsEditor = prefs.Edit())
                {
                    var encStr = Convert.ToBase64String(encryptedData);
                    prefsEditor.PutString(Utils.Md5Hash(key), encStr);
                    prefsEditor.Commit();
                }

            return(Task.CompletedTask);
        }
示例#6
0
        static Task <string> PlatformGetAsync(string key)
        {
            var context = Platform.AppContext;

            string defaultEncStr = null;
            var    encStr        = Preferences.Get(Utils.Md5Hash(key), defaultEncStr, Alias);

            string decryptedData = null;

            if (!string.IsNullOrEmpty(encStr))
            {
                var encData = Convert.FromBase64String(encStr);
                var ks      = new AndroidKeyStore(context, Alias, AlwaysUseAsymmetricKeyStorage);
                decryptedData = ks.Decrypt(encData);
            }

            return(Task.FromResult(decryptedData));
        }
        static Task PlatformSetAsync(string key, string data)
        {
            var context = Platform.AppContext;

            byte[] encryptedData = null;
            lock (locker)
            {
                var ks = new AndroidKeyStore(context, Alias, AlwaysUseAsymmetricKeyStorage);
                encryptedData = ks.Encrypt(data);
            }

            var encStr = Convert.ToBase64String(encryptedData);

            Preferences.Set(key, encStr, Alias);

            CheckForAndRemoveLegacyKey(key);

            return(Task.CompletedTask);
        }