1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- using Org.BouncyCastle.Crypto.Engines;
- using Org.BouncyCastle.Crypto.Modes;
- using Org.BouncyCastle.Crypto.Paddings;
- using Org.BouncyCastle.Crypto.Parameters;
- using Path = System.IO.Path;
- namespace ArchivesCenter3
- {
- /// <summary>
- /// reg.xaml 的交互逻辑
- /// </summary>
- public partial class reg : Window
- {
- public reg()
- {
- InitializeComponent();
- }
- private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
- {
- if (regbox.Text.Length > 0)
- regbutton.IsEnabled = true;
- else
- regbutton.IsEnabled = false;
- }
- private void regbutton_Click(object sender, RoutedEventArgs e)
- {
- Settings1.Default.DatabaseName = regdbN.Text;
- Settings1.Default.DatabaseSubtitle = regdbsN.Text;
- Settings1.Default.WelcomeTitle = regWT.Text;
- Settings1.Default.Password = regbox.Text;
- Settings1.Default.Username = regUN.Text;
- Settings1.Default.IsOOBEPassed = true;
- Settings1.Default.Save();
- string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
- // 构造目标文件夹路径
- string targetFolderPath = Path.Combine(documentsPath, "ArchivesCenter");
- if (!Directory.Exists(targetFolderPath))
- {
- Directory.CreateDirectory(targetFolderPath);
- }
- EncryptFolder(targetFolderPath);
- this.Close();
- }
- private static byte[] key = Encoding.UTF8.GetBytes("8f4a3b5c6d7e9f1a2b3c4d5e6f7a8b9c");
- // 定义一个固定的初始化向量(IV),长度为 16 字节
- private static readonly byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
- // 加密文件夹中的所有文件
- public static void EncryptFolder(string folderPath)
- {
- foreach (string filePath in Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories))
- {
- byte[] fileContent = File.ReadAllBytes(filePath);
- byte[] encryptedContent = Encrypt(fileContent);
- File.WriteAllBytes(filePath, encryptedContent);
- Console.WriteLine($"Encrypted: {filePath}");
- }
- }
- private static byte[] Encrypt(byte[] plaintext)
- {
- var cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(new AesEngine()), new Pkcs7Padding());
- var keyParam = new KeyParameter(key);
- var parameters = new ParametersWithIV(keyParam, iv);
- cipher.Init(true, parameters);
- byte[] output = new byte[cipher.GetOutputSize(plaintext.Length)];
- int length = cipher.ProcessBytes(plaintext, 0, plaintext.Length, output, 0);
- cipher.DoFinal(output, length);
- return output;
- }
- }
- }
|