MainWindow.xaml.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using Microsoft.Win32;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text.RegularExpressions;
  9. using System.Windows;
  10. using System.Windows.Forms;
  11. using Vinno.IUS.Common.Configuration;
  12. using OpenFileDialog = Microsoft.Win32.OpenFileDialog;
  13. namespace EncyptTool
  14. {
  15. /// <summary>
  16. /// MainWindow.xaml 的交互逻辑
  17. /// </summary>
  18. public partial class MainWindow : Window
  19. {
  20. private string _defaultPath = @"../Server/Settings/Server/Setting.conf";
  21. public MainWindow()
  22. {
  23. InitializeComponent();
  24. SourcePath.Text = _defaultPath;
  25. OutputPath.Text = _defaultPath;
  26. }
  27. private void SelectSource_Click(object sender, RoutedEventArgs e)
  28. {
  29. var ofd = new OpenFileDialog
  30. {
  31. Filter = "(*.json,*.conf)|*.json;*.conf",
  32. };
  33. if (ofd.ShowDialog() == true)
  34. {
  35. SourcePath.Text = ofd.FileName;
  36. OutputPath.Text = ofd.FileName;
  37. }
  38. }
  39. private void SelectOutput_Click(object sender, RoutedEventArgs e)
  40. {
  41. var openFileDialog = new FolderBrowserDialog();
  42. if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  43. {
  44. OutputPath.Text = openFileDialog.SelectedPath;
  45. }
  46. }
  47. private void EncryptButton_Click(object sender, RoutedEventArgs e)
  48. {
  49. if (string.IsNullOrEmpty(OutputPath.Text) || string.IsNullOrEmpty(SourcePath.Text))
  50. {
  51. System.Windows.MessageBox.Show("Tip", "Source path or output path is empty!");
  52. return;
  53. }
  54. string textToBeEncrypt = string.Empty;
  55. try
  56. {
  57. textToBeEncrypt = File.ReadAllText(SourcePath.Text);
  58. List<string> simplfiedServiceList = null;
  59. if (SimpleServiceFlag.IsChecked != null && (bool)SimpleServiceFlag.IsChecked)
  60. {
  61. simplfiedServiceList = new List<string>
  62. {
  63. vCloudServiceEnum.AccountService.ToString(),
  64. vCloudServiceEnum.LoginService.ToString(),
  65. vCloudServiceEnum.UnifyLogService.ToString(),
  66. vCloudServiceEnum.GatewayService.ToString(),
  67. vCloudServiceEnum.DatabaseService.ToString(),
  68. vCloudServiceEnum.StorageService.ToString(),
  69. vCloudServiceEnum.LiveVideoService.ToString(),
  70. vCloudServiceEnum.UploadService.ToString(),
  71. vCloudServiceEnum.RemedicalService.ToString(),
  72. vCloudServiceEnum.ReportService.ToString(),
  73. vCloudServiceEnum.ApiService.ToString(),
  74. vCloudServiceEnum.ShareService.ToString(),
  75. vCloudServiceEnum.ManagementService.ToString(),
  76. vCloudServiceEnum.TimeService.ToString(),
  77. vCloudServiceEnum.AdminService.ToString(),
  78. vCloudServiceEnum.AssignTerminalService.ToString()
  79. };
  80. using (StreamReader file = File.OpenText(SourcePath.Text))
  81. {
  82. using (JsonTextReader reader = new JsonTextReader(file))
  83. {
  84. JObject o = (JObject)JToken.ReadFrom(reader);
  85. var services = o["Service"];
  86. foreach (var servcie in services)
  87. {
  88. var serviceName = (servcie as JProperty).Name;
  89. if (!simplfiedServiceList.Contains(serviceName))
  90. {
  91. var strToBeReplace= $"\"{serviceName}\"" + ": 1";
  92. var strToBeReplace2 = $"\"{serviceName}\"" + ":1";
  93. var replaceStr= $"\"{serviceName}\"" + ": 0";
  94. textToBeEncrypt = Regex.Replace(textToBeEncrypt, strToBeReplace, replaceStr);
  95. textToBeEncrypt = Regex.Replace(textToBeEncrypt, strToBeReplace2, replaceStr);
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. catch (Exception)
  103. {
  104. System.Windows.MessageBox.Show("File path is invalid!", "Error");
  105. return;
  106. }
  107. var settingCrypter = new ConfigCrypter();
  108. var encryptedSetting = settingCrypter.EncryptDES(textToBeEncrypt);
  109. if (string.IsNullOrEmpty(encryptedSetting))
  110. {
  111. System.Windows.MessageBox.Show("Encrypt failed!", "Error");
  112. }
  113. else
  114. {
  115. try
  116. {
  117. var fileName = Path.GetFileName(SourcePath.Text);
  118. var fileNameExtention = Path.GetExtension(SourcePath.Text);
  119. var writePath = Path.GetExtension(OutputPath.Text).Contains(fileNameExtention) ?
  120. OutputPath.Text : Path.Combine(OutputPath.Text, fileName);
  121. File.WriteAllText(writePath, encryptedSetting);
  122. System.Windows.MessageBox.Show("Encrypt success!", "Success");
  123. }
  124. catch
  125. {
  126. System.Windows.MessageBox.Show("Writting encrypted setting file failed!", "Error");
  127. }
  128. }
  129. }
  130. private void DecryptButton_Click(object sender, RoutedEventArgs e)
  131. {
  132. try {
  133. var textToBeDencrypt = File.ReadAllText(SourcePath.Text);
  134. var settingCrypter = new ConfigCrypter();
  135. var encryptedSetting = settingCrypter.DecryptDES(textToBeDencrypt);
  136. var fileNameExtention = Path.GetExtension(SourcePath.Text);
  137. var writePath = Path.GetExtension(OutputPath.Text).Contains(fileNameExtention) ?
  138. OutputPath.Text : Path.Combine(OutputPath.Text, Path.GetFileName(SourcePath.Text));
  139. File.WriteAllText(writePath, encryptedSetting);
  140. System.Windows.MessageBox.Show("Decrypt success!", "Success");
  141. }
  142. catch
  143. {
  144. System.Windows.MessageBox.Show("Writting encrypted setting file failed!", "Error");
  145. }
  146. }
  147. }
  148. }