public App() { InitializeComponent(); if (!string.IsNullOrEmpty(Preferences.Get("accesstoken", ""))) { MainPage = new MasterPage(); } else if (string.IsNullOrEmpty(Preferences.Get("useremail", "")) && string.IsNullOrEmpty(Preferences.Get("password", ""))) { MainPage = new NavigationPage(new LoginPage()); } }
ISecretKey GetKey() { // check to see if we need to get our key from past-versions or newer versions. // we want to use symmetric if we are >= 23 or we didn't set it previously. useSymmetric = Preferences.Get(useSymmetricPreferenceKey, Platform.HasApiLevel(BuildVersionCodes.M), SecureStorage.Alias); // If >= API 23 we can use the KeyStore's symmetric key if (useSymmetric && !alwaysUseAsymmetricKey) { return(GetSymmetricKey()); } // NOTE: KeyStore in < API 23 can only store asymmetric keys // specifically, only RSA/ECB/PKCS1Padding // So we will wrap our symmetric AES key we just generated // with this and save the encrypted/wrapped key out to // preferences for future use. // ECB should be fine in this case as the AES key should be // contained in one block. // Get the asymmetric key pair var keyPair = GetAsymmetricKeyPair(); var existingKeyStr = Preferences.Get(prefsMasterKey, null, alias); if (!string.IsNullOrEmpty(existingKeyStr)) { try { var wrappedKey = Convert.FromBase64String(existingKeyStr); var unwrappedKey = UnwrapKey(wrappedKey, keyPair.Private); var kp = unwrappedKey.JavaCast <ISecretKey>(); return(kp); } catch (InvalidKeyException ikEx) { System.Diagnostics.Debug.WriteLine($"Unable to unwrap key: Invalid Key. This may be caused by system backup or upgrades. All secure storage items will now be removed. {ikEx.Message}"); } catch (IllegalBlockSizeException ibsEx) { System.Diagnostics.Debug.WriteLine($"Unable to unwrap key: Illegal Block Size. This may be caused by system backup or upgrades. All secure storage items will now be removed. {ibsEx.Message}"); } catch (BadPaddingException paddingEx) { System.Diagnostics.Debug.WriteLine($"Unable to unwrap key: Bad Padding. This may be caused by system backup or upgrades. All secure storage items will now be removed. {paddingEx.Message}"); } SecureStorage.RemoveAll(); } var keyGenerator = KeyGenerator.GetInstance(aesAlgorithm); var defSymmetricKey = keyGenerator.GenerateKey(); var newWrappedKey = WrapKey(defSymmetricKey, keyPair.Public); Preferences.Set(prefsMasterKey, Convert.ToBase64String(newWrappedKey), alias); return(defSymmetricKey); }