public bool IsSameConfigFileVO(ConfigFileVO other) { if (other != null && ViewPassword == other.ViewPassword && EditPassword == other.EditPassword && EqualityComparer <byte[]> .Default.Equals(EncryptedViewPasswordByteArray, other.EncryptedViewPasswordByteArray)) { if (AllConfigs.Count != other.AllConfigs.Count) { return(false); } for (int i = 0; i < AllConfigs.Count; i++) { if (AllConfigs[i].Equals(other.AllConfigs[i]) == false) { return(false); } } return(true); } else { return(false); } }
public ConfigFileEditorForm(ConfigFileVO vo) { InitializeComponent(); _CurrentConfigFileVO = vo; // 新建的不传入配置文件路径,隐藏“另存为”按钮和路径 if (string.IsNullOrEmpty(vo.ConfigFilePath)) { LblCurrentPathTips.Visible = false; LblCurrentConfigFilePath.Visible = false; BtnSaveAs.Visible = false; } else { _CurrentConfigFilePath = vo.ConfigFilePath; LblCurrentConfigFilePath.Text = vo.ConfigFilePath; } TxtEditPassword.Text = vo.EditPassword; TxtViewPassword.Text = vo.ViewPassword; foreach (OneConfigJsonVO oneConfig in vo.AllConfigs) { EditConfigUserControl newControl = new EditConfigUserControl(this, oneConfig); _EditConfigUserControlList.Add(newControl); Pnl.Controls.Add(newControl); } ResetConfigControlLocation(); }
private void BtnSave_Click(object sender, EventArgs e) { string errorInfo = null; ConfigFileVO vo = _CheckAndGetConfigs(out errorInfo); if (vo == null) { MessageBox.Show(this, errorInfo, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (_CurrentConfigFilePath == null) { SaveFileDialog dialog = new SaveFileDialog(); dialog.Title = "请选择要保存的路径"; dialog.Filter = $"(*.{Utils.CONFIG_FILE_EXTENSION})|*.{Utils.CONFIG_FILE_EXTENSION}"; if (dialog.ShowDialog() == DialogResult.OK) { Utils.WriteConfigFile(vo, dialog.FileName); MessageBox.Show(this, $"保存配置文件成功,路径为{dialog.FileName}\n\n点击“确定”后将自动关闭此窗口", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information); _IsForceClose = true; Close(); } } else { Utils.WriteConfigFile(vo, _CurrentConfigFilePath); MessageBox.Show(this, $"保存配置文件成功,路径为{_CurrentConfigFilePath}\n\n点击“确定”后将自动关闭此窗口", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information); _IsForceClose = true; Close(); } }
public static void TestEditModelConfigFile() { string filePath = @"D:\test_config.ftpconf"; string editPassword = "******"; ConfigFileVO editModelVO = new ConfigFileVO(); editModelVO.ConfigFilePath = filePath; editModelVO.EditPassword = editPassword; editModelVO.ViewPassword = "******"; editModelVO.AllConfigs = _TestConfigList; Utils.WriteConfigFile(editModelVO, filePath); string errorInfo = null; ConfigFileVO readConfig = Utils.ReadConfigFileByEditModel(filePath, editPassword, out errorInfo); if (errorInfo == null) { Debug.Print(readConfig.ConfigFilePath); Debug.Print(readConfig.AllConfigs[1].Password); } else { Debug.Print(errorInfo); } }
/** * 二进制形式的配置文件格式: * 第1个4字节int写入加密后的“ViewPassword:用管理员密码加密后的使用者密码”对应的byte[]的长度,其作用是当用“管理员密码”进行编辑时,可以解密这行内容,拿到用于加密配置信息json的“使用者密码”,从而进行解密 * 接下来写入上面所说的byte[]的内容 * 最后写入用“使用者密码”加密的配置信息json对应的byte[] */ public static void WriteConfigFile(ConfigFileVO vo, string filePath) { FileStream fs = new FileStream(filePath, FileMode.Create); BinaryWriter binWriter = new BinaryWriter(fs, Encoding.Default); if (vo.IsOnlyView == false) { // 用“管理员密码”加密后的使用者密码 byte[] viewPasswordByteArray = Encoding.Default.GetBytes(VIEW_PASSWORD_PREFIX + vo.ViewPassword); byte[] encryptedViewPasswordByteArray = _Encryptor.Encrypt(viewPasswordByteArray, vo.EditPassword); // 先写入上面byte[]的长度 binWriter.Write(encryptedViewPasswordByteArray.Length); // 再写入用“管理员密码”加密后的使用者密码 binWriter.Write(encryptedViewPasswordByteArray); } else { binWriter.Write(vo.EncryptedViewPasswordByteArray.Length); binWriter.Write(vo.EncryptedViewPasswordByteArray); } // 最后写入用“使用者密码”加密的配置信息json byte[] configByteArray = Encoding.Default.GetBytes(JsonMapper.ToJson(vo.AllConfigs)); byte[] encrypedConfigByteArray = _Encryptor.Encrypt(configByteArray, vo.ViewPassword); binWriter.Write(encrypedConfigByteArray); binWriter.Close(); fs.Close(); }
public static ConfigFileVO ReadConfigFileByEditModel(string filePath, string editPassword, out string errorInfo) { FileStream fs = null; BinaryReader binReader = null; ConfigFileVO vo = new ConfigFileVO(); vo.IsOnlyView = false; vo.ConfigFilePath = filePath; try { fs = new FileStream(filePath, FileMode.Open); binReader = new BinaryReader(fs, Encoding.Default); int totalLength = (int)fs.Length; int encryptedViewPasswordByteArrayLength = binReader.ReadInt32(); byte[] encryptedViewPasswordByteArray = binReader.ReadBytes(encryptedViewPasswordByteArrayLength); byte[] viewPasswordByteArray = _Decryptor.Decrypt(encryptedViewPasswordByteArray, editPassword); string viewPasswordStr = Encoding.Default.GetString(viewPasswordByteArray); if (viewPasswordStr.StartsWith(VIEW_PASSWORD_PREFIX) == false) { errorInfo = "解密失败,请确认是否输入了正确的用于编辑配置的管理员密码"; return(null); } else { vo.EditPassword = editPassword; vo.ViewPassword = viewPasswordStr.Substring(VIEW_PASSWORD_PREFIX.Length); } byte[] encryptedConfigByteArray = binReader.ReadBytes(totalLength - 4 - encryptedViewPasswordByteArrayLength); byte[] configByteArray = _Decryptor.Decrypt(encryptedConfigByteArray, vo.ViewPassword); string configStr = Encoding.Default.GetString(configByteArray); vo.AllConfigs = JsonMapper.ToObject <List <OneConfigJsonVO> >(configStr); errorInfo = null; return(vo); } catch { errorInfo = "解密失败,请确认是否输入了正确的用于编辑配置的管理员密码"; return(null); } finally { if (binReader != null) { binReader.Close(); } if (fs != null) { fs.Close(); } } }
private void BtnLoadConfigFile_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Title = "请选择要打开的配置文件"; dialog.Filter = $"(*.{Utils.CONFIG_FILE_EXTENSION})|*.{Utils.CONFIG_FILE_EXTENSION}"; dialog.Multiselect = false; if (dialog.ShowDialog() == DialogResult.OK) { string filePath = dialog.FileName; InputViewPasswordForm inputViewPasswordForm = new InputViewPasswordForm(); if (inputViewPasswordForm.ShowDialog() == DialogResult.OK) { // 校验使用者密码是否正确 string readConfigFileErrorInfo = null; ConfigFileVO vo = Utils.ReadConfigFileByViewModel(filePath, inputViewPasswordForm.InputPassword, out readConfigFileErrorInfo); if (readConfigFileErrorInfo != null) { MessageBox.Show(this, readConfigFileErrorInfo, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { _CurrentViewConfigFilePath = dialog.FileName; BtnSaveLocalPathToConfigFile.Enabled = true; _CurrentConfigFileVO = vo; LblCurrentConfigFilePath.Text = dialog.FileName; Pnl.Controls.Clear(); _ViewConfigUserControlList.Clear(); foreach (OneConfigJsonVO oneConfig in vo.AllConfigs) { ViewConfigUserControl newControl = new ViewConfigUserControl(this, oneConfig); _ViewConfigUserControlList.Add(newControl); Pnl.Controls.Add(newControl); } ResetConfigControlLocation(); } } } }
public static void TestViewModelConfigFile() { string filePath = @"D:\test_config.ftpconf"; string viewPassword = "******"; string errorInfo = null; ConfigFileVO readConfig = Utils.ReadConfigFileByViewModel(filePath, viewPassword, out errorInfo); if (errorInfo == null) { Debug.Print(readConfig.ConfigFilePath); Debug.Print(readConfig.AllConfigs[1].Password); Debug.Assert(readConfig.EditPassword == null); Debug.Assert(readConfig.EncryptedViewPasswordByteArray != null && readConfig.EncryptedViewPasswordByteArray.Length > 0); } else { Debug.Print(errorInfo); } }
public static ConfigFileVO ReadConfigFileByViewModel(string filePath, string viewPassword, out string errorInfo) { FileStream fs = null; BinaryReader binReader = null; ConfigFileVO vo = new ConfigFileVO(); vo.IsOnlyView = true; vo.ConfigFilePath = filePath; vo.ViewPassword = viewPassword; try { fs = new FileStream(filePath, FileMode.Open); binReader = new BinaryReader(fs, Encoding.Default); int totalLength = (int)fs.Length; int encryptedViewPasswordByteArrayLength = binReader.ReadInt32(); vo.EncryptedViewPasswordByteArray = binReader.ReadBytes(encryptedViewPasswordByteArrayLength); byte[] encryptedConfigByteArray = binReader.ReadBytes(totalLength - 4 - encryptedViewPasswordByteArrayLength); byte[] configByteArray = _Decryptor.Decrypt(encryptedConfigByteArray, vo.ViewPassword); string configStr = Encoding.Default.GetString(configByteArray); vo.AllConfigs = JsonMapper.ToObject <List <OneConfigJsonVO> >(configStr); errorInfo = null; return(vo); } catch { errorInfo = "解密失败,请确认是否输入了正确的用于读取配置的使用者密码"; return(null); } finally { if (binReader != null) { binReader.Close(); } if (fs != null) { fs.Close(); } } }
private void BtnSaveAs_Click(object sender, EventArgs e) { string errorInfo = null; ConfigFileVO vo = _CheckAndGetConfigs(out errorInfo); if (vo == null) { MessageBox.Show(this, errorInfo, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } SaveFileDialog dialog = new SaveFileDialog(); dialog.Title = "请选择要保存的路径"; dialog.Filter = $"(*.{Utils.CONFIG_FILE_EXTENSION})|*.{Utils.CONFIG_FILE_EXTENSION}"; // 默认选到当前打开的配置文件所在目录 dialog.InitialDirectory = Path.GetDirectoryName(_CurrentConfigFilePath); if (dialog.ShowDialog() == DialogResult.OK) { Utils.WriteConfigFile(vo, dialog.FileName); _IsForceClose = true; Close(); } }
private ConfigFileVO _CheckAndGetConfigs(out string errorInfo) { ConfigFileVO vo = new ConfigFileVO(); if (_EditConfigUserControlList.Count < 1) { errorInfo = "当前没有任何配置"; return(null); } /** * 使用者密码 */ string inputViewPassword = TxtViewPassword.Text.Trim(); if (string.IsNullOrEmpty(inputViewPassword)) { errorInfo = "未输入使用者密码"; return(null); } vo.ViewPassword = inputViewPassword; /** * 编辑密码 */ string inputEditPassword = TxtEditPassword.Text.Trim(); if (string.IsNullOrEmpty(inputEditPassword)) { errorInfo = "未输入管理员密码"; return(null); } vo.EditPassword = inputEditPassword; if (inputViewPassword.Equals(inputEditPassword)) { errorInfo = "管理员密码不能与使用者密码相同"; return(null); } /** * 检查每一个配置项 */ bool isAllConfigCheckOk = true; StringBuilder errorStringBuilder = new StringBuilder(); for (int i = 0; i < _EditConfigUserControlList.Count; i++) { string oneConfigErrorInfo = null; OneConfigJsonVO oneConfigVO = _EditConfigUserControlList[i].CheckAndGetConfig(out oneConfigErrorInfo); if (oneConfigVO == null) { isAllConfigCheckOk = false; errorStringBuilder.AppendLine($"第{i + 1}个配置项存在错误:{oneConfigErrorInfo}"); } vo.AllConfigs.Add(oneConfigVO); } if (isAllConfigCheckOk == false) { errorInfo = errorStringBuilder.ToString(); return(null); } errorInfo = null; return(vo); }
private void BtnConfigFileEditor_Click(object sender, EventArgs e) { bool isOpenConfigFile = !string.IsNullOrEmpty(_CurrentViewConfigFilePath); DialogResult dialogResult; if (isOpenConfigFile == false) { MessageBoxManager.OK = "打开已有配置"; MessageBoxManager.Cancel = "新建配置"; MessageBoxManager.Register(); dialogResult = MessageBox.Show(this, "请选择打开已有配置或者新建一个", "请选择", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); } else { MessageBoxManager.Yes = "打开当前配置"; MessageBoxManager.No = "打开其他配置"; MessageBoxManager.Cancel = "新建配置"; MessageBoxManager.Register(); dialogResult = MessageBox.Show(this, $"您可以选择修改当前打开的配置文件{_CurrentViewConfigFilePath}\n也可以打开其他配置文件或者新建一个", "请选择", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); } MessageBoxManager.Unregister(); // 选择一个配置文件打开 if (dialogResult == DialogResult.OK || dialogResult == DialogResult.No) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Title = "请选择要打开的配置文件"; dialog.Filter = $"(*.{Utils.CONFIG_FILE_EXTENSION})|*.{Utils.CONFIG_FILE_EXTENSION}"; dialog.Multiselect = false; if (dialog.ShowDialog() == DialogResult.OK) { string filePath = dialog.FileName; InputEditPasswordForm inputEditPasswordForm = new InputEditPasswordForm(); if (inputEditPasswordForm.ShowDialog() == DialogResult.OK) { // 校验管理员密码是否正确 string readConfigFileErrorInfo = null; ConfigFileVO vo = Utils.ReadConfigFileByEditModel(filePath, inputEditPasswordForm.InputPassword, out readConfigFileErrorInfo); if (readConfigFileErrorInfo != null) { MessageBox.Show(this, readConfigFileErrorInfo, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { new ConfigFileEditorForm(vo).ShowDialog(); } } } } // 直接打开当前配置文件 else if (dialogResult == DialogResult.Yes) { InputEditPasswordForm inputEditPasswordForm = new InputEditPasswordForm(); if (inputEditPasswordForm.ShowDialog() == DialogResult.OK) { // 校验管理员密码是否正确 string readConfigFileErrorInfo = null; ConfigFileVO vo = Utils.ReadConfigFileByEditModel(_CurrentViewConfigFilePath, inputEditPasswordForm.InputPassword, out readConfigFileErrorInfo); if (readConfigFileErrorInfo != null) { MessageBox.Show(this, readConfigFileErrorInfo, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { new ConfigFileEditorForm(vo).ShowDialog(); } } } // 新建配置文件 else { new ConfigFileEditorForm(new ConfigFileVO(false)).ShowDialog(); } }