Login.xaml.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. if (Settings1.Default.IsOOBEPassed)
  22. LoginButton.IsEnabled = true;
  23. else
  24. {
  25. reg reg = new reg();
  26. reg.ShowDialog();
  27. LoginButton.IsEnabled = true;
  28. }
  29. }
  30. private void Button_Click(object sender, RoutedEventArgs e)
  31. {
  32. string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  33. string targetFolderPath = Path.Combine(documentsPath, "ArchivesCenter");
  34. if (LoginKeyBox.Text == Settings1.Default.Password)
  35. {
  36. DecryptFolder(targetFolderPath);
  37. MainWindow mainWindow = new MainWindow();
  38. mainWindow.Show();
  39. this.Close();
  40. }
  41. }
  42. private static byte[] key = Encoding.UTF8.GetBytes("8f4a3b5c6d7e9f1a2b3c4d5e6f7a8b9c");
  43. // 定义一个固定的初始化向量(IV),长度为 16 字节
  44. private static readonly byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
  45. public static void DecryptFolder(string folderPath)
  46. {
  47. foreach (string filePath in Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories))
  48. {
  49. byte[] encryptedContent = File.ReadAllBytes(filePath);
  50. byte[] decryptedContent = Decrypt(encryptedContent);
  51. File.WriteAllBytes(filePath, decryptedContent);
  52. Console.WriteLine($"Decrypted: {filePath}");
  53. }
  54. }
  55. private static byte[] Decrypt(byte[] ciphertext)
  56. {
  57. var cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(new AesEngine()), new Pkcs7Padding());
  58. var keyParam = new KeyParameter(key);
  59. var parameters = new ParametersWithIV(keyParam, iv);
  60. cipher.Init(false, parameters);
  61. byte[] output = new byte[cipher.GetOutputSize(ciphertext.Length)];
  62. int length = cipher.ProcessBytes(ciphertext, 0, ciphertext.Length, output, 0);
  63. cipher.DoFinal(output, length);
  64. return output;
  65. }
  66. }
  67. }