ARUP.IssueTracker.Classes.DataProtector.EncryptData C# (CSharp) Метод

EncryptData() публичный статический Метод

Encrypts a string using the DPAPI.
public static EncryptData ( string stringToEncrypt ) : string
stringToEncrypt string The string to encrypt.
Результат string
        public static string EncryptData(string stringToEncrypt)
        {
            if (string.IsNullOrWhiteSpace(stringToEncrypt))
                return string.Empty;
            byte[] encryptedData = ProtectedData.Protect(Encoding.Unicode.GetBytes(stringToEncrypt), Encoding.Unicode.GetBytes(EntropyValue), DataProtectionScope.LocalMachine);
            return Convert.ToBase64String(encryptedData);
        }

Usage Example

Пример #1
0
        // For using in the Settings window only
        public static void SetAllJiraAccounts(List <JiraAccount> accounts)
        {
            try
            {
                Configuration config = GetConfig();
                if (config == null)
                {
                    return;
                }

                // Clear all exisiting accounts
                foreach (string key in config.AppSettings.Settings.AllKeys)
                {
                    if (key.StartsWith("jiraaccount_"))
                    {
                        config.AppSettings.Settings.Remove(key);
                    }
                }

                // Store all new account data
                foreach (JiraAccount ac in accounts)
                {
                    if (!string.IsNullOrWhiteSpace(ac.jiraserver) || !string.IsNullOrWhiteSpace(ac.username) || !string.IsNullOrWhiteSpace(ac.password) || !string.IsNullOrWhiteSpace(ac.guidfield))
                    {
                        ac.password = DataProtector.EncryptData(ac.password);
                        DateTime currentTime = DateTime.Now;
                        ac.savedTime = GetWindowsAccountPasswordLastSetTime(ac.username, currentTime);
                        string key   = "jiraaccount_" + Guid.NewGuid().ToString();
                        string value = SimpleJson.SerializeObject(ac);
                        config.AppSettings.Settings.Add(key, value);
                        config.Save(ConfigurationSaveMode.Modified);
                    }
                }

                // Set active account
                JiraAccount activeAccount = accounts.Find(ac => ac.active);
                if (activeAccount != null)
                {
                    Set("jiraserver", activeAccount.jiraserver);
                    Set("username", activeAccount.username);
                    Set("password", activeAccount.password);  // no need to encrypt here, already did
                    Set("guidfield", activeAccount.guidfield);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("exception: " + ex);
            }
        }