コード例 #1
0
        public 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;
                PasswordHash   = SecurityUtil.HashPassword(password);
                _key           = SymetricProvider.EncryptString(password, key);
                IsEncrypted    = true;
                return(true);
            }
            else
            {
                _key        = key;
                IsEncrypted = false;
                return(true);
            }
        }
コード例 #2
0
        static void EncryptKey()
        {
            Console.WriteLine("---------------------");
            Console.WriteLine("Encrypting the Key");
            Console.WriteLine("---------------------");
            Console.WriteLine("Please input path to the file with decrypted key:");
            var keyfile = Console.ReadLine();

            Console.WriteLine("Please input your password:"******"Cannot read the file!");
                    }

                    var enckey = SymetricProvider.EncryptString(password, key);
                    if (enckey != null)
                    {
                        Console.WriteLine("----------------------");
                        Console.WriteLine("Your Encrypted Key is:");
                        Console.WriteLine();
                        Console.WriteLine(enckey);
                        var pth = Path.GetDirectoryName(keyfile);
                        FileHelpers.WriteTextToFile(Path.Combine(pth, "Enckey-" + FileHelpers.GetDateTimeString() + ".txt"), enckey);
                        Console.WriteLine("----------------------");
                    }
                    else
                    {
                        Console.WriteLine("---------!!!!---------");
                        Console.WriteLine("Cannot Encrypt the Key");
                        Console.WriteLine("----------------------");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("---------!!!!---------");
                    Console.WriteLine("Cannot Encrypt the Key");
                    Console.WriteLine("----------------------");
                }
            }
            else
            {
                Console.WriteLine("Wrong input, please try it again.");
            }
        }
コード例 #3
0
        public string GetEncryptedKey(string password = "", bool returnEncrypted = false)
        {
            if (returnEncrypted)
            {
                return(_key);
            }

            if (passwordLoaded && string.IsNullOrEmpty(password))
            {
                password = loadedPassword;
            }

            if (!passwordLoaded && string.IsNullOrEmpty(password))
            {
                return(null);
            }

            if (!string.IsNullOrEmpty(password))
            {
                if (PasswordHash?.Length > 0)
                {
                    if (SecurityUtil.VerifyPassword(password, PasswordHash))
                    {
                        return(SymetricProvider.DecryptString(password, _key));
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                if (!IsEncrypted)
                {
                    return(_key);
                }
                else
                {
                    return(null);
                }
            }
        }