Пример #1
0
        /// <summary>
        /// Gets the key/password value from the keyChain.
        /// </summary>
        /// <returns>The key/password (or null if the password was not found in the KeyChain).</returns>
        /// <param name="keyName">Keyname/username.</param>
        public bool GetKey(string keyName, out string value)
        {
            var wantedAlias = MakeAlias(keyName, _serviceId);

            var aliases = _androidKeyStore.Aliases();

            while (aliases.HasMoreElements)
            {
                var alias = aliases.NextElement().ToString();
                if (alias.Contains(wantedAlias))
                {
                    var e = _androidKeyStore.GetEntry(alias, _passwordProtection) as KeyStore.SecretKeyEntry;
                    if (e != null)
                    {
                        var bytes = e.SecretKey.GetEncoded();
                        value = System.Text.Encoding.UTF8.GetString(bytes);
                        return(true);
                    }
                }
            }

            value = null;

            return(false);
        }
        private string AESEncrypt_New(byte[] decrypted)
        {
            Cipher c = Cipher.GetInstance(_AESMode);

            KeyStore.IEntry  storeEntry = _keyStore.GetEntry(_secureStoredKeyAlias, null);
            IKey             key        = ((KeyStore.SecretKeyEntry)storeEntry).SecretKey;
            GCMParameterSpec param      = new GCMParameterSpec(128, _fixedIV);

            c.Init(CipherMode.EncryptMode, key, param);
            byte[] encodedBytes = c.DoFinal(decrypted);
            return(Base64.EncodeToString(encodedBytes, Base64Flags.Default));
        }
Пример #3
0
 public IEnumerable<Account> FindAccountsForService(string serviceId)
 {
     if (_keyStore == null)
     {
         InitializeStore();
     }
     
     var accounts = new List<Account>();
     string postfix = "-" + serviceId;
     IEnumeration aliases = _keyStore.Aliases();
     while(aliases.HasMoreElements)
     {
         string alias = aliases.NextElement().ToString();
         if(alias.EndsWith(postfix))
         {
             var entry = _keyStore.GetEntry(alias, _prot) as KeyStore.SecretKeyEntry;
             if(entry != null)
             {
                 byte[] bytes = entry.SecretKey.GetEncoded();
                 string serialized = Encoding.UTF8.GetString(bytes);
                 Account account = Account.Deserialize(serialized);
                 accounts.Add(account);
             }
         }
     }
     return accounts;
 }
        public override IEnumerable <ISalesforceUser> FindAccountsForService(string serviceId)
        {
            var r = new List <SalesforceUser> ();

            var postfix = "-" + serviceId;

            var aliases = ks.Aliases();

            while (aliases.HasMoreElements)
            {
                var alias = aliases.NextElement().ToString();
                if (alias.EndsWith(postfix))
                {
                    var e = ks.GetEntry(alias, prot) as KeyStore.SecretKeyEntry;
                    if (e != null)
                    {
                        var bytes      = e.SecretKey.GetEncoded();
                        var serialized = System.Text.Encoding.UTF8.GetString(bytes);
                        var acct       = SalesforceUser.Deserialize(serialized);
                        r.Add(acct);
                    }
                }
            }

            r.Sort((a, b) => a.Username.CompareTo(b.Username));

            return(r);
        }
Пример #5
0
        IEnumerable <LoginDetails> FindAccountsForService(string serviceId)
        {
            var r = new List <LoginDetails>();

            var postfix = "-" + serviceId;

            var aliases = _keyStore.Aliases();

            while (aliases.HasMoreElements)
            {
                var alias = aliases.NextElement().ToString();
                if (alias.EndsWith(postfix, StringComparison.Ordinal))
                {
                    var e = _keyStore.GetEntry(alias, _passwordProtection) as KeyStore.SecretKeyEntry;
                    if (e != null)
                    {
                        var bytes      = e.SecretKey.GetEncoded();
                        var serialized = Encoding.UTF8.GetString(bytes);
                        var acct       = LoginDetails.Deserialize(serialized);
                        r.Add(acct);
                    }
                }
            }

            r.Sort((a, b) => string.Compare(a.Username, b.Username, StringComparison.Ordinal));

            return(r);
        }
 public Java.Security.KeyStore.IEntry Get(string key)
 {
     if (Contains(key))
     {
         return(keyStore.GetEntry(key, Password));
     }
     return(null);
 }
Пример #7
0
        private byte[] RSAEncrypt(byte[] secret, string keyAlias)
        {
            var privateKeyEntry = (KeyStore.PrivateKeyEntry)_keyStore.GetEntry(keyAlias, null);

            if (privateKeyEntry == null)
            {
                GenerateRSAKeyPairs(keyAlias);
                privateKeyEntry = (KeyStore.PrivateKeyEntry)_keyStore.GetEntry(keyAlias, null);
            }

            // Encrypt the text
            var cipher = Cipher.GetInstance(RSAMode, "AndroidOpenSSL");

            cipher.Init(CipherMode.EncryptMode, privateKeyEntry.Certificate.PublicKey);

            return(cipher.DoFinal(secret));
        }
Пример #8
0
 /// <summary>
 /// Load RSA key pair from KeyStore
 /// </summary>
 private static void AccessKeyStore()
 {
     if (_keyStore.ContainsAlias(ALIAS))
     {
         IPrivateKey privateKey = (_keyStore.GetEntry(ALIAS, null) as KeyStore.PrivateKeyEntry).PrivateKey;
         IPublicKey  publicKey  = _keyStore.GetCertificate(ALIAS).PublicKey;
         _keyPair = new KeyPair(publicKey, privateKey);
     }
 }
        public string EncryptKey(string privKey)
        {
            KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry)_keyStore.GetEntry(_alias, null);
            var publicKey = privateKeyEntry.Certificate.PublicKey;

            Cipher cipher = Cipher.GetInstance("RSA/ECB/PKCS1Padding", "AndroidOpenSSL");

            cipher.Init(CipherMode.EncryptMode, publicKey);//

            MemoryStream       outputStream       = new MemoryStream();
            CipherOutputStream cipherOutputStream = new CipherOutputStream(
                outputStream, cipher);

            cipherOutputStream.Write(Encoding.UTF8.GetBytes(privKey));
            cipherOutputStream.Close();

            byte[] encryptedBytes = outputStream.ToArray();
            return(Nethereum.Hex.HexConvertors.Extensions.HexByteConvertorExtensions.ToHexCompact(encryptedBytes));
        }
Пример #10
0
        private static ISecretKey GetSecretKey(
            KeyStore keyStore,
            string alias)
        {
            SecretKeyEntry entry = (SecretKeyEntry)keyStore.GetEntry(alias, null);

            if (entry != null)
            {
                ISecretKey secretKey = entry.SecretKey;
                return(secretKey);
            }
            else
            {
                return(null);
            }
        }
Пример #11
0
        public T Get <T>(string key)
        {
            var aliases = ks.Aliases();

            while (aliases.HasMoreElements)
            {
                var alias = aliases.NextElement().ToString();
                if (alias.Equals(key.Trim(), StringComparison.InvariantCulture))
                {
                    if (ks.GetEntry(alias, prot) is KeyStore.SecretKeyEntry e)
                    {
                        var bytes = e.SecretKey.GetEncoded();
                        return(JsonConvert.DeserializeObject <T>(UnicodeEncoding.UTF8.GetString(bytes)));
                    }
                }
            }

            return(default(T));
        }
 public IEnumerable<string> FindAccountsForService(string serviceId)
 {
     var r = new List<string>();
     var postfix = "-" + serviceId;
     var aliases = ks.Aliases();
     while (aliases.HasMoreElements)
     {
         var alias = aliases.NextElement().ToString();
         if (alias.EndsWith(postfix))
         {
             var e = ks.GetEntry(alias, prot) as KeyStore.SecretKeyEntry;
             if (e != null)
             {
                 var bytes = e.SecretKey.GetEncoded();
                 var password = System.Text.Encoding.UTF8.GetString(bytes);
                 r.Add(password);
             }
         }
     }
     return r;
 }
        public string GetKey(string keyName)
        {
            var wantedAlias = MakeAlias(keyName, _serviceId).ToLower();

            var aliases = _androidKeyStore.Aliases();

            while (aliases.HasMoreElements)
            {
                var alias = aliases.NextElement().ToString();
                if (alias.ToLower().Contains(wantedAlias))
                {
                    var e = _androidKeyStore.GetEntry(alias, new KeyStore.PasswordProtection(_serviceId.ToCharArray())) as KeyStore.SecretKeyEntry;
                    if (e != null)
                    {
                        var bytes = e.SecretKey.GetEncoded();
                        //return System.Text.Encoding.UTF8.GetString(bytes);
                        return(bytes.GetString());
                    }
                }
            }
            return("");
        }
Пример #14
0
        public bool TryGetPassword(string username, string service, out string password)
        {
            var aliases = keystore.Aliases();

            while (aliases.HasMoreElements)
            {
                var alias = aliases.NextElement().ToString();
                if (alias.Equals(GetAlias(username, service)))
                {
                    var e = keystore.GetEntry(alias, this.protection) as KeyStore.SecretKeyEntry;
                    if (e != null)
                    {
                        var bytes = e.SecretKey.GetEncoded();
                        password = System.Text.Encoding.UTF8.GetString(bytes);
                        return(true);
                    }
                }
            }

            password = string.Empty;
            return(false);
        }
        public Dictionary <string, string> FindAccountsForService(string serviceId)
        {
            Dictionary <string, string> values = null;

            var postfix = "-" + serviceId;

            var aliases = ks.Aliases();

            while (aliases.HasMoreElements)
            {
                var alias = aliases.NextElement().ToString();
                if (alias.EndsWith(postfix))
                {
                    var e = ks.GetEntry(alias, prot) as KeyStore.SecretKeyEntry;
                    if (e != null)
                    {
                        var bytes            = e.SecretKey.GetEncoded();
                        var serializedValues = System.Text.Encoding.UTF8.GetString(bytes);
                        values = JsonConvert.DeserializeObject <Dictionary <string, string> >(serializedValues);
                    }
                }
            }
            return(values);
        }
Пример #16
0
        public IEnumerable <string> FindAccountsForService(string serviceId)
        {
            List <string> r       = new List <string>();
            string        postfix = "-" + serviceId;
            IEnumeration  aliases = _keyStore.Aliases();

            while (aliases.HasMoreElements)
            {
                string alias = aliases.NextElement().ToString();
                if (alias.EndsWith(postfix))
                {
                    KeyStore.SecretKeyEntry e = _keyStore.GetEntry(
                        alias,
                        _passProtection) as KeyStore.SecretKeyEntry;
                    if (e != null)
                    {
                        byte[] bytes    = e.SecretKey.GetEncoded();
                        string password = System.Text.Encoding.UTF8.GetString(bytes);
                        r.Add(password);
                    }
                }
            }
            return(r);
        }
Пример #17
0
 private KeyStore.PrivateKeyEntry GetRsaKeyEntry(string alias)
 {
     return(_keyStore.GetEntry(alias, null) as KeyStore.PrivateKeyEntry);
 }
Пример #18
0
        private async Task <byte[]> GetEncryptionKeyLockedAsync(string keyName)
        {
            byte[] key = null;

            try
            {
                char[]   deviceId = GetDeviceId().ToCharArray();
                KeyStore keyStore = await GetOrCreateKeyStoreAsync(deviceId).ConfigureAwait(false);

                KeyStore.IProtectionParameter protectionParameter = new KeyStore.PasswordProtection(deviceId);
                KeyStore.SecretKeyEntry       secretKeyEntry      = (KeyStore.SecretKeyEntry)keyStore.GetEntry(keyName, protectionParameter);

                if (secretKeyEntry != null)
                {
                    ISecretKey secretKey = secretKeyEntry.SecretKey;
                    if (secretKey != null)
                    {
                        key = secretKey.GetEncoded();
                    }
                }
            }
            catch (FileNotFoundException)
            {
                // If the file isn't found, it's not a big deal and should mean it's just the first run.
                // The caller or the GetOrCreate method above will need to create it if we don't find it here.
            }

            return(key);
        }