Login.xaml.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.IO;
  2. using System.Text;
  3. using System.Windows;
  4. using System.Windows.Input;
  5. using System.Windows.Shapes;
  6. using Org.BouncyCastle.Crypto.Engines;
  7. using Org.BouncyCastle.Crypto.Modes;
  8. using Org.BouncyCastle.Crypto.Paddings;
  9. using Org.BouncyCastle.Crypto.Parameters;
  10. using Path = System.IO.Path;
  11. namespace ArchivesCenter3
  12. {
  13. /// <summary>
  14. /// Login.xaml 的交互逻辑
  15. /// </summary>
  16. public partial class Login : Window
  17. {
  18. public Login()
  19. {
  20. InitializeComponent();
  21. Settings1.Default.IsOOBEPassed = false;
  22. Settings1.Default.Save();
  23. if (Settings1.Default.IsOOBEPassed)
  24. LoginButton.IsEnabled = true;
  25. else
  26. {
  27. reg reg = new reg();
  28. reg.ShowDialog();
  29. LoginButton.IsEnabled = true;
  30. }
  31. }
  32. private void Button_Click(object sender, RoutedEventArgs e)
  33. {
  34. string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  35. string targetFolderPath = Path.Combine(documentsPath, "ArchivesCenter");
  36. if (LoginKeyBox.Text == Settings1.Default.Password)
  37. {
  38. DecryptFolder(targetFolderPath);
  39. MainWindow mainWindow = new MainWindow();
  40. mainWindow.Show();
  41. this.Close();
  42. }
  43. }
  44. private static byte[] key = Encoding.UTF8.GetBytes("8f4a3b5c6d7e9f1a2b3c4d5e6f7a8b9c");
  45. // 定义一个固定的初始化向量(IV),长度为 16 字节
  46. private static readonly byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
  47. public static void DecryptFolder(string folderPath)
  48. {
  49. foreach (string filePath in Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories))
  50. {
  51. byte[] encryptedContent = File.ReadAllBytes(filePath);
  52. byte[] decryptedContent = Decrypt(encryptedContent);
  53. File.WriteAllBytes(filePath, decryptedContent);
  54. Console.WriteLine($"Decrypted: {filePath}");
  55. }
  56. }
  57. private static byte[] Decrypt(byte[] ciphertext)
  58. {
  59. var cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(new AesEngine()), new Pkcs7Padding());
  60. var keyParam = new KeyParameter(key);
  61. var parameters = new ParametersWithIV(keyParam, iv);
  62. cipher.Init(false, parameters);
  63. byte[] output = new byte[cipher.GetOutputSize(ciphertext.Length)];
  64. int length = cipher.ProcessBytes(ciphertext, 0, ciphertext.Length, output, 0);
  65. cipher.DoFinal(output, length);
  66. return output;
  67. }
  68. }
  69. }