reg.xaml.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Shapes;
  15. using Org.BouncyCastle.Crypto.Engines;
  16. using Org.BouncyCastle.Crypto.Modes;
  17. using Org.BouncyCastle.Crypto.Paddings;
  18. using Org.BouncyCastle.Crypto.Parameters;
  19. using Path = System.IO.Path;
  20. namespace ArchivesCenter3
  21. {
  22. /// <summary>
  23. /// reg.xaml 的交互逻辑
  24. /// </summary>
  25. public partial class reg : Window
  26. {
  27. public reg()
  28. {
  29. InitializeComponent();
  30. }
  31. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  32. {
  33. if (regbox.Text.Length > 0)
  34. regbutton.IsEnabled = true;
  35. else
  36. regbutton.IsEnabled = false;
  37. }
  38. private void regbutton_Click(object sender, RoutedEventArgs e)
  39. {
  40. Settings1.Default.DatabaseName = regdbN.Text;
  41. Settings1.Default.DatabaseSubtitle = regdbsN.Text;
  42. Settings1.Default.WelcomeTitle = regWT.Text;
  43. Settings1.Default.Password = regbox.Text;
  44. Settings1.Default.Username = regUN.Text;
  45. Settings1.Default.IsOOBEPassed = true;
  46. Settings1.Default.Save();
  47. string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  48. // 构造目标文件夹路径
  49. string targetFolderPath = Path.Combine(documentsPath, "ArchivesCenter");
  50. if (!Directory.Exists(targetFolderPath))
  51. {
  52. Directory.CreateDirectory(targetFolderPath);
  53. }
  54. EncryptFolder(targetFolderPath);
  55. this.Close();
  56. }
  57. private static byte[] key = Encoding.UTF8.GetBytes("8f4a3b5c6d7e9f1a2b3c4d5e6f7a8b9c");
  58. // 定义一个固定的初始化向量(IV),长度为 16 字节
  59. private static readonly byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
  60. // 加密文件夹中的所有文件
  61. public static void EncryptFolder(string folderPath)
  62. {
  63. foreach (string filePath in Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories))
  64. {
  65. byte[] fileContent = File.ReadAllBytes(filePath);
  66. byte[] encryptedContent = Encrypt(fileContent);
  67. File.WriteAllBytes(filePath, encryptedContent);
  68. Console.WriteLine($"Encrypted: {filePath}");
  69. }
  70. }
  71. private static byte[] Encrypt(byte[] plaintext)
  72. {
  73. var cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(new AesEngine()), new Pkcs7Padding());
  74. var keyParam = new KeyParameter(key);
  75. var parameters = new ParametersWithIV(keyParam, iv);
  76. cipher.Init(true, parameters);
  77. byte[] output = new byte[cipher.GetOutputSize(plaintext.Length)];
  78. int length = cipher.ProcessBytes(plaintext, 0, plaintext.Length, output, 0);
  79. cipher.DoFinal(output, length);
  80. return output;
  81. }
  82. }
  83. }