Esempio n. 1
0
        /// <summary>
        /// Encrypt the string with Shared secret
        /// </summary>
        /// <param name="message">Message to encrypt</param>
        /// <param name="bobAddress">Receiver Neblio Address</param>
        /// <param name="key">Shared secret key</param>
        /// <returns></returns>
        public static async Task <(bool, string)> EncryptStringWithSharedSecretWithKey(string message, string bobAddress, string key)
        {
            if (string.IsNullOrEmpty(message))
            {
                return(false, "Message cannot be empty or null.");
            }
            if (string.IsNullOrEmpty(bobAddress))
            {
                return(false, "Partner Address cannot be empty or null.");
            }
            if (string.IsNullOrEmpty(key))
            {
                return(false, "Input secret cannot null.");
            }

            try
            {
                var emesage = SymetricProvider.EncryptString(key, message);
                return(true, emesage);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Cannot encrypt message. " + ex.Message);
                return(false, "Cannot encrypt message. " + ex.Message);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Encrypt the string with Shared secret
        /// </summary>
        /// <param name="message">Message to encrypt</param>
        /// <param name="bobAddress">Receiver Neblio Address</param>
        /// <param name="secret">Alice secret</param>
        /// <param name="sharedkey">shared key</param>
        /// <returns></returns>
        public static async Task <(bool, string)> EncryptStringWithSharedSecret(string message, string bobAddress, BitcoinSecret secret, string sharedkey = "")
        {
            if (string.IsNullOrEmpty(message))
            {
                return(false, "Message cannot be empty or null.");
            }
            if (string.IsNullOrEmpty(bobAddress))
            {
                return(false, "Partner Address cannot be empty or null.");
            }
            if (secret == null)
            {
                return(false, "Input secret cannot null.");
            }

            (bool, string)key = (false, "");
            if (string.IsNullOrEmpty(sharedkey))
            {
                key = await GetSharedSecret(bobAddress, secret);

                if (!key.Item1)
                {
                    return(key);
                }
            }
            else
            {
                key.Item2 = sharedkey;
            }

            try
            {
                var emesage = SymetricProvider.EncryptString(key.Item2, message);
                return(true, emesage);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Cannot encrypt message. " + ex.Message);
                return(false, "Cannot encrypt message. " + ex.Message);
            }
        }
        /// <summary>
        /// Load the key and password.
        /// Same logic as constructor of this class
        /// </summary>
        /// <param name="key"></param>
        /// <param name="password"></param>
        /// <param name="fromDb"></param>
        /// <returns></returns>
        public async Task <bool> LoadNewKey(string key, string password = "", bool fromDb = false)
        {
            if (string.IsNullOrEmpty(key))
            {
                return(false);
            }

            if (fromDb)
            {
                _key = key;
                return(true);
            }

            if (!string.IsNullOrEmpty(password))
            {
                loadedPassword = password;
                passwordLoaded = true;
                _key           = SymetricProvider.EncryptString(password, key);
                IsEncrypted    = true;
                return(true);
            }
            else
            {
                _key        = key;
                IsEncrypted = false;
                if (!string.IsNullOrEmpty(password))
                {
                    loadedPassword = string.Empty;
                }
                else
                {
                    loadedPassword = password;
                }
                passwordLoaded = true;
                return(true);
            }
        }