123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- using Microsoft.Win32;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Windows;
- using System.Windows.Forms;
- using Vinno.IUS.Common.Configuration;
- using OpenFileDialog = Microsoft.Win32.OpenFileDialog;
- namespace EncyptTool
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- private string _defaultPath = @"../Server/Settings/Server/Setting.conf";
- public MainWindow()
- {
- InitializeComponent();
- SourcePath.Text = _defaultPath;
- OutputPath.Text = _defaultPath;
- }
- private void SelectSource_Click(object sender, RoutedEventArgs e)
- {
- var ofd = new OpenFileDialog
- {
- Filter = "(*.json,*.conf)|*.json;*.conf",
- };
- if (ofd.ShowDialog() == true)
- {
- SourcePath.Text = ofd.FileName;
- OutputPath.Text = ofd.FileName;
- }
- }
- private void SelectOutput_Click(object sender, RoutedEventArgs e)
- {
- var openFileDialog = new FolderBrowserDialog();
- if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- OutputPath.Text = openFileDialog.SelectedPath;
- }
- }
- private void EncryptButton_Click(object sender, RoutedEventArgs e)
- {
- if (string.IsNullOrEmpty(OutputPath.Text) || string.IsNullOrEmpty(SourcePath.Text))
- {
- System.Windows.MessageBox.Show("Tip", "Source path or output path is empty!");
- return;
- }
- string textToBeEncrypt = string.Empty;
-
- try
- {
- textToBeEncrypt = File.ReadAllText(SourcePath.Text);
- List<string> simplfiedServiceList = null;
- if (SimpleServiceFlag.IsChecked != null && (bool)SimpleServiceFlag.IsChecked)
- {
- simplfiedServiceList = new List<string>
- {
- vCloudServiceEnum.AccountService.ToString(),
- vCloudServiceEnum.LoginService.ToString(),
- vCloudServiceEnum.UnifyLogService.ToString(),
- vCloudServiceEnum.GatewayService.ToString(),
- vCloudServiceEnum.DatabaseService.ToString(),
- vCloudServiceEnum.StorageService.ToString(),
- vCloudServiceEnum.LiveVideoService.ToString(),
- vCloudServiceEnum.UploadService.ToString(),
- vCloudServiceEnum.RemedicalService.ToString(),
- vCloudServiceEnum.ReportService.ToString(),
- vCloudServiceEnum.ApiService.ToString(),
- vCloudServiceEnum.ShareService.ToString(),
- vCloudServiceEnum.ManagementService.ToString(),
- vCloudServiceEnum.TimeService.ToString(),
- vCloudServiceEnum.AdminService.ToString(),
- vCloudServiceEnum.AssignTerminalService.ToString()
- };
- using (StreamReader file = File.OpenText(SourcePath.Text))
- {
- using (JsonTextReader reader = new JsonTextReader(file))
- {
- JObject o = (JObject)JToken.ReadFrom(reader);
- var services = o["Service"];
- foreach (var servcie in services)
- {
- var serviceName = (servcie as JProperty).Name;
- if (!simplfiedServiceList.Contains(serviceName))
- {
- var strToBeReplace= $"\"{serviceName}\"" + ": 1";
- var strToBeReplace2 = $"\"{serviceName}\"" + ":1";
- var replaceStr= $"\"{serviceName}\"" + ": 0";
- textToBeEncrypt = Regex.Replace(textToBeEncrypt, strToBeReplace, replaceStr);
- textToBeEncrypt = Regex.Replace(textToBeEncrypt, strToBeReplace2, replaceStr);
- }
- }
- }
- }
- }
- }
- catch (Exception)
- {
- System.Windows.MessageBox.Show("File path is invalid!", "Error");
- return;
- }
-
- var settingCrypter = new ConfigCrypter();
- var encryptedSetting = settingCrypter.EncryptDES(textToBeEncrypt);
- if (string.IsNullOrEmpty(encryptedSetting))
- {
- System.Windows.MessageBox.Show("Encrypt failed!", "Error");
- }
- else
- {
- try
- {
- var fileName = Path.GetFileName(SourcePath.Text);
- var fileNameExtention = Path.GetExtension(SourcePath.Text);
- var writePath = Path.GetExtension(OutputPath.Text).Contains(fileNameExtention) ?
- OutputPath.Text : Path.Combine(OutputPath.Text, fileName);
- File.WriteAllText(writePath, encryptedSetting);
- System.Windows.MessageBox.Show("Encrypt success!", "Success");
- }
- catch
- {
- System.Windows.MessageBox.Show("Writting encrypted setting file failed!", "Error");
- }
- }
- }
- private void DecryptButton_Click(object sender, RoutedEventArgs e)
- {
- try {
- var textToBeDencrypt = File.ReadAllText(SourcePath.Text);
- var settingCrypter = new ConfigCrypter();
- var encryptedSetting = settingCrypter.DecryptDES(textToBeDencrypt);
- var fileNameExtention = Path.GetExtension(SourcePath.Text);
- var writePath = Path.GetExtension(OutputPath.Text).Contains(fileNameExtention) ?
- OutputPath.Text : Path.Combine(OutputPath.Text, Path.GetFileName(SourcePath.Text));
- File.WriteAllText(writePath, encryptedSetting);
- System.Windows.MessageBox.Show("Decrypt success!", "Success");
- }
- catch
- {
- System.Windows.MessageBox.Show("Writting encrypted setting file failed!", "Error");
- }
- }
- }
- }
|