Ejemplo n.º 1
0
        private void PasswordEncryptionMethodCombo_Changed(object sender, EventArgs e)
        {
            switch (_passwordEncryptionMethodCombo.SelectedValue)
            {
            case EncryptionMethod.LogonCredentials:
                _passwordEncryptionDataLabel.Text     = "用户名:";
                _passwordEncryptionDataButton.Text    = CredentialsUI.GetLoggedInUser();
                _passwordEncryptionDataButton.Tag     = null;
                _passwordEncryptionDataButton.Enabled = false;
                _passwordEncryptionDataInfoLabel.Text = string.Empty;
                break;

            case EncryptionMethod.Certificate:
            {
                X509Certificate2 x509Certificate = _passwordEncryptionDataButton.Tag as X509Certificate2;
                if (x509Certificate == null)
                {
                    try
                    {
                        base.Enabled    = false;
                        x509Certificate = Encryption.SelectCertificate();
                    }
                    finally
                    {
                        base.Enabled = true;
                    }
                }
                if (x509Certificate != null)
                {
                    SetSelectedCertificate(x509Certificate);
                }
                else
                {
                    _passwordEncryptionMethodCombo.SelectedValue = _passwordEncryptionMethodPrevious;
                }
                break;
            }

            default:
                throw new NotImplementedException("Unexpected encryption method '{0}'".InvariantFormat(_passwordEncryptionMethodCombo.SelectedValue.ToString()));
            }
            _passwordEncryptionMethodPrevious = _passwordEncryptionMethodCombo.SelectedValue;
        }
Ejemplo n.º 2
0
        private unsafe static string DecryptStringUsingLocalUser(string encryptedString)
        {
            Crypto.DataBlob optionalEntropy = default(Crypto.DataBlob);
            Crypto.CryptProtectPromptStruct promptStruct = default(Crypto.CryptProtectPromptStruct);
            if (string.IsNullOrEmpty(encryptedString))
            {
                return(string.Empty);
            }
            optionalEntropy.Size = 0;
            promptStruct.Size    = 0;
            byte[]          array  = Convert.FromBase64String(encryptedString);
            Crypto.DataBlob dataIn = default(Crypto.DataBlob);
            dataIn.Size = array.Length;
            Crypto.DataBlob dataOut;
            string          result;

            fixed(byte *value = array)
            {
                dataIn.Data = (IntPtr)(void *)value;
                if (!Crypto.CryptUnprotectData(ref dataIn, null, ref optionalEntropy, (IntPtr)null, ref promptStruct, 0, out dataOut))
                {
                    throw new Exception("使用{0}证书解密失败".InvariantFormat(CredentialsUI.GetLoggedInUser()));
                }
                char *ptr = (char *)(void *)dataOut.Data;

                char[] array2 = new char[dataOut.Size / 2];
                for (int i = 0; i < array2.Length; i++)
                {
                    array2[i] = ptr[i];
                }
                result = new string(array2);
            }

            Crypto.LocalFree(dataOut.Data);
            return(result);
        }
Ejemplo n.º 3
0
 static EncryptionSettings()
 {
     typeof(EncryptionSettings).GetSettingProperties(out _settingProperties);
     _settingProperties["CredentialName"].Attribute.DefaultValue = CredentialsUI.GetLoggedInUser();
 }