|
@@ -1,6 +1,9 @@
|
|
|
using System.IO;
|
|
|
+using System.Reflection;
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
|
+using DocumentFormat.OpenXml.InkML;
|
|
|
+
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
|
using Microsoft.EntityFrameworkCore.Metadata;
|
|
@@ -9,195 +12,328 @@ using UniformMaterialManagementSystem.Data;
|
|
|
|
|
|
namespace UniformMaterialManagementSystem.Utils
|
|
|
{
|
|
|
- public static class DataBaseUtil
|
|
|
- {
|
|
|
- private static readonly string DataBasePath = Environment.CurrentDirectory + "\\DataBase";
|
|
|
- private const string Password = "88384e6a-07e0-47e9-8214-4470a16e78da";
|
|
|
-
|
|
|
- public static async void ExportTable<T>(string targetConnectionString) where T : class
|
|
|
- {
|
|
|
- CreateExportFolder();
|
|
|
+ //public static class DataBaseUtil
|
|
|
+ //{
|
|
|
+ // private static readonly string DataBasePath = Environment.CurrentDirectory + "\\DataBase";
|
|
|
+ // private const string Password = "88384e6a-07e0-47e9-8214-4470a16e78da";
|
|
|
|
|
|
- var optionsBuilderSource = new DbContextOptionsBuilder<SqliteContext>();
|
|
|
- optionsBuilderSource.UseSqlite("Data Source=UniformMaterialManagementSystem.db");
|
|
|
+ // private static readonly SqliteContext _context = new SqliteContext("");
|
|
|
|
|
|
- var targetPath = $"{DataBasePath}\\UniformMaterialManagementSystem.db";
|
|
|
+ // public static async void ExportTable<T>(string targetConnectionString,IEnumerable<T> sourceData) where T : class
|
|
|
+ // {
|
|
|
+ // CreateExportFolder();
|
|
|
|
|
|
- var optionsBuilderTarget = new DbContextOptionsBuilder<SqliteContext>();
|
|
|
- optionsBuilderTarget.UseSqlite($"Data Source={targetPath}");
|
|
|
-
|
|
|
- await using var sourceContext = new SqliteContext(optionsBuilderSource.Options);
|
|
|
- await using var targetContext = new SqliteContext(optionsBuilderTarget.Options);
|
|
|
-
|
|
|
- // 读取源数据
|
|
|
- var sourceData = await sourceContext.Set<T>()
|
|
|
- .IncludeAll()
|
|
|
- .ToListAsync();
|
|
|
-
|
|
|
- // 确保目标数据库创建
|
|
|
- await targetContext.Database.MigrateAsync();
|
|
|
-
|
|
|
- // 获取目标 DbSet
|
|
|
- var targetDbSet = targetContext.Set<T>();
|
|
|
-
|
|
|
- // 添加到目标数据库
|
|
|
- targetDbSet.AddRange(sourceData);
|
|
|
- await targetContext.SaveChangesAsync();
|
|
|
-
|
|
|
- EncryptFile(targetPath, targetConnectionString);
|
|
|
- }
|
|
|
-
|
|
|
- public static async void ImportTable<T>(string sourceConnectionString) where T : class
|
|
|
- {
|
|
|
- CreateExportFolder();
|
|
|
-
|
|
|
- var sourcePath = $"{DataBasePath}\\UniformMaterialManagementSystem.db";
|
|
|
- DecryptFile(sourceConnectionString, sourcePath);
|
|
|
-
|
|
|
- var optionsBuilderSource = new DbContextOptionsBuilder<SqliteContext>();
|
|
|
- optionsBuilderSource.UseSqlite($"Data Source={sourcePath}");
|
|
|
-
|
|
|
- var optionsBuilderTarget = new DbContextOptionsBuilder<SqliteContext>();
|
|
|
- optionsBuilderTarget.UseSqlite("Data Source=UniformMaterialManagementSystem.db");
|
|
|
-
|
|
|
- await using var sourceContext = new SqliteContext(optionsBuilderSource.Options);
|
|
|
- await using var targetContext = new SqliteContext(optionsBuilderTarget.Options);
|
|
|
-
|
|
|
- // 确保目标数据库创建
|
|
|
- await targetContext.Database.MigrateAsync();
|
|
|
-
|
|
|
- // 读取源数据
|
|
|
- var sourceData = sourceContext.Set<T>()
|
|
|
- .IncludeAll()
|
|
|
- .ToList();
|
|
|
-
|
|
|
- // 获取目标 DbSet
|
|
|
- var targetDbSet = targetContext.Set<T>();
|
|
|
-
|
|
|
- // 如果目标 DbSet 有数据,可以选择清除现有数据或更新数据
|
|
|
- // targetDbSet.RemoveRange(targetDbSet.ToList());
|
|
|
- // targetContext.SaveChanges();
|
|
|
-
|
|
|
- // 添加到目标数据库
|
|
|
- targetDbSet.AddRange(sourceData);
|
|
|
- await targetContext.SaveChangesAsync();
|
|
|
- }
|
|
|
-
|
|
|
- private static void CreateExportFolder()
|
|
|
- {
|
|
|
- if (!Directory.Exists(DataBasePath))
|
|
|
- {
|
|
|
- Directory.CreateDirectory(DataBasePath);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static IQueryable<TEntity> IncludeAll<TEntity>(this DbSet<TEntity> dbSet, int maxDepth = int.MaxValue) where TEntity :class
|
|
|
- {
|
|
|
- IQueryable<TEntity> result = dbSet;
|
|
|
- var context = dbSet.GetService<ICurrentDbContext>().Context;
|
|
|
- var includePaths = GetIncludePaths<TEntity>(context, maxDepth);
|
|
|
-
|
|
|
- foreach (var includePath in includePaths)
|
|
|
- {
|
|
|
- result = result.Include(includePath);
|
|
|
- }
|
|
|
-
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- private static IEnumerable<string> GetIncludePaths<T>(DbContext context, int maxDepth = int.MaxValue)
|
|
|
- {
|
|
|
- if (maxDepth < 0)
|
|
|
- throw new ArgumentOutOfRangeException(nameof(maxDepth));
|
|
|
-
|
|
|
- var entityType = context.Model.FindEntityType(typeof(T));
|
|
|
- var includedNavigations = new HashSet<INavigation>();
|
|
|
- var stack = new Stack<IEnumerator<INavigation>>();
|
|
|
-
|
|
|
- while (true)
|
|
|
- {
|
|
|
- var entityNavigations = new List<INavigation>();
|
|
|
-
|
|
|
- if (stack.Count <= maxDepth)
|
|
|
- {
|
|
|
- if (entityType != null)
|
|
|
- foreach (var navigation in entityType.GetNavigations())
|
|
|
- {
|
|
|
- if (includedNavigations.Add(navigation))
|
|
|
- entityNavigations.Add(navigation);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (entityNavigations.Count == 0)
|
|
|
- {
|
|
|
- if (stack.Count > 0)
|
|
|
- yield return string.Join(".", stack.Reverse().Select(e => e.Current.Name));
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- foreach (var navigation in entityNavigations)
|
|
|
- {
|
|
|
- var inverseNavigation = navigation.Inverse;
|
|
|
- if (inverseNavigation != null)
|
|
|
- includedNavigations.Add(inverseNavigation);
|
|
|
- }
|
|
|
-
|
|
|
- stack.Push(entityNavigations.GetEnumerator());
|
|
|
- }
|
|
|
-
|
|
|
- while (stack.Count > 0 && !stack.Peek().MoveNext())
|
|
|
- stack.Pop();
|
|
|
-
|
|
|
- if (stack.Count == 0)
|
|
|
- break;
|
|
|
-
|
|
|
- entityType = stack.Peek().Current.TargetEntityType;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private static void EncryptFile(string inputFile, string outputFile)
|
|
|
- {
|
|
|
- // 生成密钥和初始化向量
|
|
|
- GenerateAesKeyAndIv(out var key, out var iv);
|
|
|
-
|
|
|
- using var aesAlg = Aes.Create();
|
|
|
- aesAlg.Key = key;
|
|
|
- aesAlg.IV = iv;
|
|
|
-
|
|
|
- var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
|
|
|
-
|
|
|
- using var outFileStream = new FileStream(outputFile, FileMode.Create);
|
|
|
- using var csEncrypt = new CryptoStream(outFileStream, encryptor, CryptoStreamMode.Write);
|
|
|
- using var inFileStream = new FileStream(inputFile, FileMode.Open);
|
|
|
-
|
|
|
- inFileStream.CopyTo(csEncrypt);
|
|
|
- }
|
|
|
-
|
|
|
- private static void DecryptFile(string inputFile, string outputFile)
|
|
|
- {
|
|
|
- // 生成密钥和初始化向量
|
|
|
- GenerateAesKeyAndIv(out var key, out var iv);
|
|
|
-
|
|
|
- using var aesAlg = Aes.Create();
|
|
|
- aesAlg.Key = key;
|
|
|
- aesAlg.IV = iv;
|
|
|
-
|
|
|
- var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
|
|
|
-
|
|
|
- using var fileStream = new FileStream(inputFile, FileMode.Open);
|
|
|
- using var csDecrypt = new CryptoStream(fileStream, decryptor, CryptoStreamMode.Read);
|
|
|
- using var outFileStream = new FileStream(outputFile, FileMode.Create);
|
|
|
-
|
|
|
- csDecrypt.CopyTo(outFileStream);
|
|
|
- }
|
|
|
-
|
|
|
- private static void GenerateAesKeyAndIv(out byte[] key, out byte[] iv)
|
|
|
- {
|
|
|
- using var pdb = new Rfc2898DeriveBytes(Password, [0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76
|
|
|
- ]);
|
|
|
-
|
|
|
- key = pdb.GetBytes(32); // 256位密钥
|
|
|
- iv = pdb.GetBytes(16); // 128位IV
|
|
|
- }
|
|
|
- }
|
|
|
+ // var optionsBuilderSource = new DbContextOptionsBuilder<SqliteContext>();
|
|
|
+ // optionsBuilderSource.UseSqlite("Data Source=UniformMaterialManagementSystem.db");
|
|
|
+
|
|
|
+ // var targetPath = $"{DataBasePath}\\UniformMaterialManagementSystem.db";
|
|
|
+
|
|
|
+ // var optionsBuilderTarget = new DbContextOptionsBuilder<SqliteContext>();
|
|
|
+ // optionsBuilderTarget.UseSqlite($"Data Source={targetPath}");
|
|
|
+
|
|
|
+ // await using var sourceContext = new SqliteContext(optionsBuilderSource.Options);
|
|
|
+ // await using var targetContext = new SqliteContext(optionsBuilderTarget.Options);
|
|
|
+
|
|
|
+ // // 确保目标数据库创建
|
|
|
+ // await targetContext.Database.MigrateAsync();
|
|
|
+
|
|
|
+ // // 获取目标 DbSet
|
|
|
+ // var targetDbSet = targetContext.Set<T>();
|
|
|
+
|
|
|
+ // // 添加到目标数据库
|
|
|
+ // targetDbSet.AddRange(sourceData);
|
|
|
+ // await targetContext.SaveChangesAsync();
|
|
|
+
|
|
|
+ // EncryptFile(targetPath, targetConnectionString);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // public static async void ImportTable<T>(string sourceConnectionString) where T : class
|
|
|
+ // {
|
|
|
+ // CreateExportFolder();
|
|
|
+
|
|
|
+ // var sourcePath = $"{DataBasePath}\\UniformMaterialManagementSystem.db";
|
|
|
+ // DecryptFile(sourceConnectionString, sourcePath);
|
|
|
+
|
|
|
+ // var optionsBuilderSource = new DbContextOptionsBuilder<SqliteContext>();
|
|
|
+ // optionsBuilderSource.UseSqlite($"Data Source={sourcePath}");
|
|
|
+
|
|
|
+ // var optionsBuilderTarget = new DbContextOptionsBuilder<SqliteContext>();
|
|
|
+ // optionsBuilderTarget.UseSqlite("Data Source=UniformMaterialManagementSystem.db");
|
|
|
+
|
|
|
+ // await using var sourceContext = new SqliteContext(optionsBuilderSource.Options);
|
|
|
+ // await using var targetContext = new SqliteContext(optionsBuilderTarget.Options);
|
|
|
+
|
|
|
+ // // 确保目标数据库创建
|
|
|
+ // await targetContext.Database.MigrateAsync();
|
|
|
+
|
|
|
+ // // 读取源数据
|
|
|
+ // var sourceData = sourceContext.Set<T>()
|
|
|
+ // .IncludeAll()
|
|
|
+ // .ToList();
|
|
|
+
|
|
|
+ // // 获取目标 DbSet
|
|
|
+ // var targetDbSet = targetContext.Set<T>();
|
|
|
+
|
|
|
+ // // 如果目标 DbSet 有数据,可以选择清除现有数据或更新数据
|
|
|
+ // //targetDbSet.RemoveRange(targetDbSet.ToList());
|
|
|
+ // //await targetContext.SaveChangesAsync();
|
|
|
+
|
|
|
+ // // 添加到目标数据库
|
|
|
+ // targetDbSet.AddRange(sourceData);
|
|
|
+ // await targetContext.SaveChangesAsync();
|
|
|
+ // }
|
|
|
+
|
|
|
+ // public static async Task ImportData<T>(List<T> entities, Func<T, object> keySelector, bool updateExisting = true) where T : class
|
|
|
+ // {
|
|
|
+ // var dbSet = _context.Set<T>();
|
|
|
+ // var existingEntities = await dbSet.ToListAsync();
|
|
|
+
|
|
|
+ // foreach (var entity in entities)
|
|
|
+ // {
|
|
|
+ // var key = keySelector(entity);
|
|
|
+ // var existingEntity = existingEntities.FirstOrDefault(e => keySelector(e).Equals(key));
|
|
|
+
|
|
|
+ // if (existingEntity != null && updateExisting)
|
|
|
+ // {
|
|
|
+ // // 更新现有实体
|
|
|
+ // await UpdateEntityWithRelations(existingEntity, entity);
|
|
|
+ // }
|
|
|
+ // else if (existingEntity == null)
|
|
|
+ // {
|
|
|
+ // // 添加新实体
|
|
|
+ // await AddEntityWithRelations(entity);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+
|
|
|
+ // await _context.SaveChangesAsync();
|
|
|
+ // }
|
|
|
+
|
|
|
+ // private static async Task UpdateEntityWithRelations<T>(T existingEntity, T newEntity) where T : class
|
|
|
+ // {
|
|
|
+ // _context.Entry(existingEntity).CurrentValues.SetValues(newEntity);
|
|
|
+
|
|
|
+ // foreach (var property in typeof(T).GetProperties())
|
|
|
+ // {
|
|
|
+ // if (property.PropertyType.IsClass && property.PropertyType != typeof(string))
|
|
|
+ // {
|
|
|
+ // var existingRelatedEntity = property.GetValue(existingEntity);
|
|
|
+ // var newRelatedEntity = property.GetValue(newEntity);
|
|
|
+
|
|
|
+ // if (newRelatedEntity != null)
|
|
|
+ // {
|
|
|
+ // if (existingRelatedEntity == null)
|
|
|
+ // {
|
|
|
+ // // 如果现有实体没有相关实体,但新实体有,则添加新的相关实体
|
|
|
+ // property.SetValue(existingEntity, newRelatedEntity);
|
|
|
+ // _context.Entry(newRelatedEntity).State = EntityState.Added;
|
|
|
+ // }
|
|
|
+ // else
|
|
|
+ // {
|
|
|
+ // // 如果两者都有相关实体,则递归更新
|
|
|
+ // await UpdateEntityWithRelations(existingRelatedEntity, newRelatedEntity);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // else if (property.PropertyType.IsGenericType &&
|
|
|
+ // property.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>))
|
|
|
+ // {
|
|
|
+ // // 处理集合导航属性
|
|
|
+ // await HandleCollectionNavigation(existingEntity, newEntity, property);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+
|
|
|
+ // private static async Task AddEntityWithRelations<T>(T entity) where T : class
|
|
|
+ // {
|
|
|
+ // await _context.Set<T>().AddAsync(entity);
|
|
|
+
|
|
|
+ // foreach (var property in typeof(T).GetProperties())
|
|
|
+ // {
|
|
|
+ // if (property.PropertyType.IsClass && property.PropertyType != typeof(string))
|
|
|
+ // {
|
|
|
+ // var relatedEntity = property.GetValue(entity);
|
|
|
+ // if (relatedEntity != null)
|
|
|
+ // {
|
|
|
+ // await AddEntityWithRelations(relatedEntity);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // else if (property.PropertyType.IsGenericType &&
|
|
|
+ // property.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>))
|
|
|
+ // {
|
|
|
+ // if (property.GetValue(entity) is not IEnumerable<object> collection) continue;
|
|
|
+
|
|
|
+ // foreach (var item in collection)
|
|
|
+ // {
|
|
|
+ // await AddEntityWithRelations(item);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+
|
|
|
+ // private static async Task HandleCollectionNavigation<T>(T existingEntity, T newEntity, PropertyInfo property) where T : class
|
|
|
+ // {
|
|
|
+ // var existingCollection = property.GetValue(existingEntity) as IEnumerable<object>;
|
|
|
+ // var newCollection = property.GetValue(newEntity) as IEnumerable<object>;
|
|
|
+
|
|
|
+ // if (existingCollection == null || newCollection == null)
|
|
|
+ // return;
|
|
|
+
|
|
|
+ // var existingList = existingCollection.ToList();
|
|
|
+ // var newList = newCollection.ToList();
|
|
|
+
|
|
|
+ // // 获取集合元素类型
|
|
|
+ // var elementType = property.PropertyType.GetGenericArguments()[0];
|
|
|
+
|
|
|
+ // // 假设每个元素都有一个 Id 属性作为键
|
|
|
+ // var keyProperty = elementType.GetProperty("Id");
|
|
|
+ // if (keyProperty == null)
|
|
|
+ // throw new InvalidOperationException($"No Id property found for type {elementType.Name}");
|
|
|
+
|
|
|
+ // foreach (var newItem in newList)
|
|
|
+ // {
|
|
|
+ // var newItemKey = keyProperty.GetValue(newItem);
|
|
|
+ // var existingItem = existingList.FirstOrDefault(e => keyProperty.GetValue(e).Equals(newItemKey));
|
|
|
+
|
|
|
+ // if (existingItem != null)
|
|
|
+ // {
|
|
|
+ // // 更新现有项
|
|
|
+ // await UpdateEntityWithRelations(existingItem, newItem);
|
|
|
+ // }
|
|
|
+ // else
|
|
|
+ // {
|
|
|
+ // // 添加新项
|
|
|
+ // existingList.Add(newItem);
|
|
|
+ // await AddEntityWithRelations(newItem);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+
|
|
|
+ // // 移除不再存在的项
|
|
|
+ // var itemsToRemove = existingList.Where(e => !newList.Any(n => keyProperty.GetValue(n).Equals(keyProperty.GetValue(e)))).ToList();
|
|
|
+ // foreach (var item in itemsToRemove)
|
|
|
+ // {
|
|
|
+ // existingList.Remove(item);
|
|
|
+ // _context.Entry(item).State = EntityState.Deleted;
|
|
|
+ // }
|
|
|
+
|
|
|
+ // // 更新集合
|
|
|
+ // property.SetValue(existingEntity, existingList);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // private static void CreateExportFolder()
|
|
|
+ // {
|
|
|
+ // if (!Directory.Exists(DataBasePath))
|
|
|
+ // {
|
|
|
+ // Directory.CreateDirectory(DataBasePath);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+
|
|
|
+ // public static IQueryable<TEntity> IncludeAll<TEntity>(this DbSet<TEntity> dbSet, int maxDepth = int.MaxValue) where TEntity :class
|
|
|
+ // {
|
|
|
+ // IQueryable<TEntity> result = dbSet;
|
|
|
+ // var context = dbSet.GetService<ICurrentDbContext>().Context;
|
|
|
+ // var includePaths = GetIncludePaths<TEntity>(context, maxDepth);
|
|
|
+
|
|
|
+ // foreach (var includePath in includePaths)
|
|
|
+ // {
|
|
|
+ // result = result.Include(includePath);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // return result;
|
|
|
+ // }
|
|
|
+
|
|
|
+ // private static IEnumerable<string> GetIncludePaths<T>(DbContext context, int maxDepth = int.MaxValue)
|
|
|
+ // {
|
|
|
+ // if (maxDepth < 0)
|
|
|
+ // throw new ArgumentOutOfRangeException(nameof(maxDepth));
|
|
|
+
|
|
|
+ // var entityType = context.Model.FindEntityType(typeof(T));
|
|
|
+ // var includedNavigations = new HashSet<INavigation>();
|
|
|
+ // var stack = new Stack<IEnumerator<INavigation>>();
|
|
|
+
|
|
|
+ // while (true)
|
|
|
+ // {
|
|
|
+ // var entityNavigations = new List<INavigation>();
|
|
|
+
|
|
|
+ // if (stack.Count <= maxDepth)
|
|
|
+ // {
|
|
|
+ // if (entityType != null)
|
|
|
+ // foreach (var navigation in entityType.GetNavigations())
|
|
|
+ // {
|
|
|
+ // if (includedNavigations.Add(navigation))
|
|
|
+ // entityNavigations.Add(navigation);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+
|
|
|
+ // if (entityNavigations.Count == 0)
|
|
|
+ // {
|
|
|
+ // if (stack.Count > 0)
|
|
|
+ // yield return string.Join(".", stack.Reverse().Select(e => e.Current.Name));
|
|
|
+ // }
|
|
|
+ // else
|
|
|
+ // {
|
|
|
+ // foreach (var navigation in entityNavigations)
|
|
|
+ // {
|
|
|
+ // var inverseNavigation = navigation.Inverse;
|
|
|
+ // if (inverseNavigation != null)
|
|
|
+ // includedNavigations.Add(inverseNavigation);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // stack.Push(entityNavigations.GetEnumerator());
|
|
|
+ // }
|
|
|
+
|
|
|
+ // while (stack.Count > 0 && !stack.Peek().MoveNext())
|
|
|
+ // stack.Pop();
|
|
|
+
|
|
|
+ // if (stack.Count == 0)
|
|
|
+ // break;
|
|
|
+
|
|
|
+ // entityType = stack.Peek().Current.TargetEntityType;
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+
|
|
|
+ // private static void EncryptFile(string inputFile, string outputFile)
|
|
|
+ // {
|
|
|
+ // // 生成密钥和初始化向量
|
|
|
+ // GenerateAesKeyAndIv(out var key, out var iv);
|
|
|
+
|
|
|
+ // using var aesAlg = Aes.Create();
|
|
|
+ // aesAlg.Key = key;
|
|
|
+ // aesAlg.IV = iv;
|
|
|
+
|
|
|
+ // var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
|
|
|
+
|
|
|
+ // using var outFileStream = new FileStream(outputFile, FileMode.Create);
|
|
|
+ // using var csEncrypt = new CryptoStream(outFileStream, encryptor, CryptoStreamMode.Write);
|
|
|
+ // using var inFileStream = new FileStream(inputFile, FileMode.Open);
|
|
|
+
|
|
|
+ // inFileStream.CopyTo(csEncrypt);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // private static void DecryptFile(string inputFile, string outputFile)
|
|
|
+ // {
|
|
|
+ // // 生成密钥和初始化向量
|
|
|
+ // GenerateAesKeyAndIv(out var key, out var iv);
|
|
|
+
|
|
|
+ // using var aesAlg = Aes.Create();
|
|
|
+ // aesAlg.Key = key;
|
|
|
+ // aesAlg.IV = iv;
|
|
|
+
|
|
|
+ // var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
|
|
|
+
|
|
|
+ // using var fileStream = new FileStream(inputFile, FileMode.Open);
|
|
|
+ // using var csDecrypt = new CryptoStream(fileStream, decryptor, CryptoStreamMode.Read);
|
|
|
+ // using var outFileStream = new FileStream(outputFile, FileMode.Create);
|
|
|
+
|
|
|
+ // csDecrypt.CopyTo(outFileStream);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // private static void GenerateAesKeyAndIv(out byte[] key, out byte[] iv)
|
|
|
+ // {
|
|
|
+ // using var pdb = new Rfc2898DeriveBytes(Password, [0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76
|
|
|
+ // ]);
|
|
|
+
|
|
|
+ // key = pdb.GetBytes(32); // 256位密钥
|
|
|
+ // iv = pdb.GetBytes(16); // 128位IV
|
|
|
+ // }
|
|
|
+ //}
|
|
|
}
|