Răsfoiți Sursa

移除AES/DES的默认加密密钥

懒得勤快 3 ani în urmă
părinte
comite
7ca77f8dda

+ 7 - 69
Masuit.Tools.Abstractions/Security/Encrypt.cs

@@ -1,6 +1,5 @@
 using System;
 using System.IO;
-using System.Linq;
 using System.Security.Cryptography;
 using System.Text;
 using System.Text.RegularExpressions;
@@ -14,45 +13,6 @@ namespace Masuit.Tools.Security
     {
         #region DES对称加密解密
 
-        /// <summary>
-        /// 加密密钥,默认取“masuit”的MD5值
-        /// </summary>
-        public static string DefaultEncryptKey = "masuit".MDString2();
-
-        /// <summary>
-        /// 使用默认加密
-        /// </summary>
-        /// <param name="strText">被加密的字符串</param>
-        /// <returns>加密后的数据</returns>
-        public static string DesEncrypt(this string strText)
-        {
-            try
-            {
-                return DesEncrypt(strText, DefaultEncryptKey);
-            }
-            catch
-            {
-                return "";
-            }
-        }
-
-        /// <summary>
-        /// 使用默认解密
-        /// </summary>
-        /// <param name="strText">需要解密的 字符串</param>
-        /// <returns>解密后的数据</returns>
-        public static string DesDecrypt(this string strText)
-        {
-            try
-            {
-                return DesDecrypt(strText, DefaultEncryptKey);
-            }
-            catch
-            {
-                return "";
-            }
-        }
-
         /// <summary> 
         /// 加密字符串
         /// 加密密钥必须为8位
@@ -291,8 +251,6 @@ namespace Masuit.Tools.Security
 
         #region 对称加密算法AES RijndaelManaged加密解密
 
-        private static readonly string Default_AES_Key = "@#kim123";
-
         private static byte[] Keys =
         {
             0x41,
@@ -329,17 +287,6 @@ namespace Masuit.Tools.Security
             return Convert.ToBase64String(crypto.Key);
         }
 
-        /// <summary>
-        /// 对称加密算法AES RijndaelManaged加密(RijndaelManaged(AES)算法是块式加密算法)
-        /// </summary>
-        /// <param name="encryptString">待加密字符串</param>
-        /// <param name="mode">加密模式</param>
-        /// <returns>加密结果字符串</returns>
-        public static string AESEncrypt(this string encryptString, CipherMode mode = CipherMode.CBC)
-        {
-            return AESEncrypt(encryptString, Default_AES_Key, mode);
-        }
-
         /// <summary>
         /// 对称加密算法AES RijndaelManaged加密(RijndaelManaged(AES)算法是块式加密算法)
         /// </summary>
@@ -398,17 +345,6 @@ namespace Masuit.Tools.Security
             return Convert.ToBase64String(encryptedData);
         }
 
-        /// <summary>
-        /// 对称加密算法AES RijndaelManaged解密字符串
-        /// </summary>
-        /// <param name="decryptString">待解密的字符串</param>
-        /// <param name="mode">加密模式</param>
-        /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
-        public static string AESDecrypt(this string decryptString, CipherMode mode = CipherMode.CBC)
-        {
-            return AESDecrypt(decryptString, Default_AES_Key, mode);
-        }
-
         /// <summary>
         /// 对称加密算法AES RijndaelManaged解密字符串
         /// </summary>
@@ -668,14 +604,15 @@ namespace Masuit.Tools.Security
         /// 对指定文件AES加密
         /// </summary>
         /// <param name="input">源文件流</param>
+        /// <param name="key">加密密钥</param>
         /// <param name="mode">加密模式</param>
         /// <param name="outputPath">输出文件路径</param>
-        public static void AESEncryptFile(this FileStream input, string outputPath, CipherMode mode = CipherMode.CBC)
+        public static void AESEncryptFile(this FileStream input, string outputPath, string key, CipherMode mode = CipherMode.CBC)
         {
             using var fren = new FileStream(outputPath, FileMode.Create);
-            using var enfr = AESEncryptStrream(fren, Default_AES_Key, mode);
+            using var enfr = AESEncryptStrream(fren, key, mode);
             byte[] bytearrayinput = new byte[input.Length];
-            input.Read(bytearrayinput, 0, bytearrayinput.Length);
+            _ = input.Read(bytearrayinput, 0, bytearrayinput.Length);
             enfr.Write(bytearrayinput, 0, bytearrayinput.Length);
         }
 
@@ -683,12 +620,13 @@ namespace Masuit.Tools.Security
         /// 对指定的文件AES解密
         /// </summary>
         /// <param name="input">源文件流</param>
+        /// <param name="key">解密密钥</param>
         /// <param name="mode">加密模式</param>
         /// <param name="outputPath">输出文件路径</param>
-        public static void AESDecryptFile(this FileStream input, string outputPath, CipherMode mode = CipherMode.CBC)
+        public static void AESDecryptFile(this FileStream input, string outputPath, string key, CipherMode mode = CipherMode.CBC)
         {
             using FileStream frde = new FileStream(outputPath, FileMode.Create);
-            using CryptoStream defr = AESDecryptStream(input, Default_AES_Key, mode);
+            using CryptoStream defr = AESDecryptStream(input, key, mode);
             byte[] bytearrayoutput = new byte[1024];
             while (true)
             {

+ 6 - 6
Masuit.Tools.sln

@@ -9,8 +9,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masuit.Tools.Core", "Masuit
 EndProject
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "测试", "测试", "{E0B8FBD1-A28A-4420-9DE2-6BD06035CBAC}"
 EndProject
-Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreTest", "NetCoreTest\NetCoreTest.csproj", "{ABEC3F65-6556-44FD-BECE-33F7927FB3AE}"
-EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masuit.Tools.NoSQL.MongoDBClient", "Masuit.Tools.NoSQL.MongoDBClient\Masuit.Tools.NoSQL.MongoDBClient.csproj", "{2C4FF8E9-2662-4618-ABA4-BA55AA5EA9AB}"
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masuit.Tools.Abstractions", "Masuit.Tools.Abstractions\Masuit.Tools.Abstractions.csproj", "{74E7C0A2-499D-408F-A2E4-08878700C2DE}"
@@ -29,6 +27,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masuit.Tools.Net45", "Masui
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masuit.Tools.AspNetCore", "Masuit.Tools.AspNetCore\Masuit.Tools.AspNetCore.csproj", "{73BA93B7-C6AE-4B39-892E-3596D91BF96C}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCoreTest", "NetCoreTest\NetCoreTest.csproj", "{144D7A0C-002D-48E4-8814-EA14011CFFFC}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -43,10 +43,6 @@ Global
 		{149E6872-CE95-493B-B136-535E64C65A49}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{149E6872-CE95-493B-B136-535E64C65A49}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{149E6872-CE95-493B-B136-535E64C65A49}.Release|Any CPU.Build.0 = Release|Any CPU
-		{ABEC3F65-6556-44FD-BECE-33F7927FB3AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{ABEC3F65-6556-44FD-BECE-33F7927FB3AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{ABEC3F65-6556-44FD-BECE-33F7927FB3AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{ABEC3F65-6556-44FD-BECE-33F7927FB3AE}.Release|Any CPU.Build.0 = Release|Any CPU
 		{2C4FF8E9-2662-4618-ABA4-BA55AA5EA9AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 		{2C4FF8E9-2662-4618-ABA4-BA55AA5EA9AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{2C4FF8E9-2662-4618-ABA4-BA55AA5EA9AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -83,6 +79,10 @@ Global
 		{73BA93B7-C6AE-4B39-892E-3596D91BF96C}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{73BA93B7-C6AE-4B39-892E-3596D91BF96C}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{73BA93B7-C6AE-4B39-892E-3596D91BF96C}.Release|Any CPU.Build.0 = Release|Any CPU
+		{144D7A0C-002D-48E4-8814-EA14011CFFFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{144D7A0C-002D-48E4-8814-EA14011CFFFC}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{144D7A0C-002D-48E4-8814-EA14011CFFFC}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{144D7A0C-002D-48E4-8814-EA14011CFFFC}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 15 - 13
NetCoreTest/Controllers/HomeController.cs

@@ -1,20 +1,22 @@
-using Masuit.Tools.Security;
+using Masuit.Tools.AspNetCore.ModelBinder;
 using Microsoft.AspNetCore.Mvc;
 using System.Net;
-using System.Threading.Tasks;
 
-namespace NetCoreTest.Controllers
+namespace NetCoreTest.Controllers;
+
+[ApiController]
+public class HomeController : Controller
 {
-    public class HomeController : Controller
+    [HttpPost("test")]
+    [ProducesResponseType(typeof(MyClass), (int)HttpStatusCode.OK)]
+    public async Task<ActionResult> Test([FromBodyOrDefault] MyClass mc)
     {
-        [HttpGet("rsaenc")]
-        [ProducesResponseType((int)HttpStatusCode.OK)]
-        public async Task<IActionResult> Rsa(string str)
-        {
-            var rsaKey = RsaCrypt.GenerateRsaKeys();
-            var enc = str.RSAEncrypt();
-            var dec = enc.RSADecrypt();
-            return Ok(dec);
-        }
+        return Ok(mc);
     }
 }
+
+public class MyClass
+{
+    public string MyProperty { get; set; }
+    public List<string> List { get; set; }
+}

+ 7 - 14
NetCoreTest/NetCoreTest.csproj

@@ -1,27 +1,20 @@
-<Project Sdk="Microsoft.NET.Sdk.Web">
+<Project Sdk="Microsoft.NET.Sdk.Web">
 
   <PropertyGroup>
     <TargetFramework>net6.0</TargetFramework>
-    <RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>
-    <RunAnalyzersDuringLiveAnalysis>false</RunAnalyzersDuringLiveAnalysis>
-    <LangVersion>latest</LangVersion>
+    <ImplicitUsings>enable</ImplicitUsings>
   </PropertyGroup>
 
-  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
-    <CodeAnalysisRuleSet />
-  </PropertyGroup>
-
-  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
-    <CodeAnalysisRuleSet />
-  </PropertyGroup>
+  <ItemGroup>
+    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
+  </ItemGroup>
 
   <ItemGroup>
-    <Folder Include="wwwroot\" />
+    <Folder Include="Controllers\" />
   </ItemGroup>
 
   <ItemGroup>
-    <ProjectReference Include="..\Masuit.Tools.Core\Masuit.Tools.Core.csproj" />
-    <ProjectReference Include="..\Masuit.Tools.Excel\Masuit.Tools.Excel.csproj" />
+    <ProjectReference Include="..\Masuit.Tools.AspNetCore\Masuit.Tools.AspNetCore.csproj" />
   </ItemGroup>
 
 </Project>

+ 23 - 20
NetCoreTest/Program.cs

@@ -1,26 +1,29 @@
-using System;
-using Masuit.Tools.Systems;
-using Microsoft.AspNetCore;
-using Microsoft.AspNetCore.Hosting;
-using NetCoreTest;
-using Newtonsoft.Json;
+using Masuit.Tools.AspNetCore.ModelBinder;
+using Masuit.Tools.Files;
+using Masuit.Tools.Media;
 
-string json1 = "{\"a\":\"aa\"}";
-string json2 = "{\"b\":\"bb\"}";
-string json3 = "{\"MyProperty\":\"mm\"}";
-JsonConvert.DefaultSettings = () => new JsonSerializerSettings() { ContractResolver = new CompositeContractResolver() };
-var m1 = JsonConvert.DeserializeObject<MyClass>(json1);
-var m2 = JsonConvert.DeserializeObject<MyClass>(json2);
-var m3 = JsonConvert.DeserializeObject<MyClass>(json3);
+var stream = File.Open(@"D:\images\QQ½ØÍ¼20190923195408.jpg", FileMode.Open, FileAccess.ReadWrite);
+var watermarker = new ImageWatermarker(stream);
+var ms = watermarker.AddWatermark(File.OpenRead(@"D:\images\QQ½ØÍ¼20190923195408_¿´Í¼Íõ.png"), 0.5f);
+ms.SaveFile(@"Y:\1.jpg");
+Console.WriteLine(1);
 Console.ReadKey();
 
-//CreateWebHostBuilder(args).Build().Run();
-static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
-    WebHost.CreateDefaultBuilder(args)
-        .UseStartup<Startup>();
+var builder = WebApplication.CreateBuilder(args);
+builder.Services.AddControllers(options => options.ModelBinderProviders.InsertBodyOrDefaultBinding());
+builder.Services.AddEndpointsApiExplorer();
+builder.Services.AddSwaggerGen();
 
-public class MyClass
+var app = builder.Build();
+
+if (app.Environment.IsDevelopment())
 {
-    [SerializeIgnore, FallbackJsonProperty(nameof(MyProperty), "a", "b")]
-    public string MyProperty { get; set; }
+    app.UseSwagger();
+    app.UseSwaggerUI();
 }
+
+app.UseAuthorization();
+
+app.MapControllers();
+
+app.Run();

+ 0 - 23
NetCoreTest/Properties/PublishProfiles/FolderProfile.pubxml

@@ -1,23 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-此文件由 Web 项目的发布/打包过程使用。可以通过编辑此 MSBuild 文件
-自定义此过程的行为。为了解与此相关的更多内容,请访问 https://go.microsoft.com/fwlink/?LinkID=208121。 
--->
-<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
-  <PropertyGroup>
-    <WebPublishMethod>FileSystem</WebPublishMethod>
-    <PublishProvider>FileSystem</PublishProvider>
-    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
-    <LastUsedPlatform>Any CPU</LastUsedPlatform>
-    <SiteUrlToLaunchAfterPublish />
-    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
-    <ExcludeApp_Data>False</ExcludeApp_Data>
-    <ProjectGuid>abec3f65-6556-44fd-bece-33f7927fb3ae</ProjectGuid>
-    <publishUrl>bin\Release\netcoreapp2.1\publish\</publishUrl>
-    <DeleteExistingFiles>True</DeleteExistingFiles>
-    <TargetFramework>netcoreapp2.2</TargetFramework>
-    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
-    <SelfContained>false</SelfContained>
-    <_IsPortable>true</_IsPortable>
-  </PropertyGroup>
-</Project>

+ 14 - 12
NetCoreTest/Properties/launchSettings.json

@@ -1,29 +1,31 @@
-{
+{
+  "$schema": "https://json.schemastore.org/launchsettings.json",
   "iisSettings": {
     "windowsAuthentication": false,
     "anonymousAuthentication": true,
     "iisExpress": {
-      "applicationUrl": "http://localhost:11070",
+      "applicationUrl": "http://localhost:3847",
       "sslPort": 0
     }
   },
-  "$schema": "http://json.schemastore.org/launchsettings.json",
   "profiles": {
-    "IIS Express": {
-      "commandName": "IISExpress",
+    "NetCoreTest": {
+      "commandName": "Project",
+      "dotnetRunMessages": true,
       "launchBrowser": true,
-      "launchUrl": "api/values",
+      "launchUrl": "swagger",
+      "applicationUrl": "http://localhost:5013",
       "environmentVariables": {
         "ASPNETCORE_ENVIRONMENT": "Development"
       }
     },
-    "NetCoreTest": {
-      "commandName": "Project",
-      "launchUrl": "api/values",
+    "IIS Express": {
+      "commandName": "IISExpress",
+      "launchBrowser": true,
+      "launchUrl": "swagger",
       "environmentVariables": {
         "ASPNETCORE_ENVIRONMENT": "Development"
-      },
-      "applicationUrl": "http://localhost:5000"
+      }
     }
   }
-}
+}

+ 0 - 43
NetCoreTest/Startup.cs

@@ -1,43 +0,0 @@
-using Masuit.Tools.Core.AspNetCore;
-using Microsoft.AspNetCore.Builder;
-using Microsoft.AspNetCore.Hosting;
-using Microsoft.AspNetCore.Mvc;
-using Microsoft.Extensions.Configuration;
-using Microsoft.Extensions.DependencyInjection;
-using Microsoft.Extensions.Hosting;
-
-namespace NetCoreTest
-{
-    public class Startup
-    {
-        public Startup(IConfiguration configuration)
-        {
-            Configuration = configuration;
-        }
-
-        public IConfiguration Configuration { get; }
-
-        // This method gets called by the runtime. Use this method to add services to the container.
-        public void ConfigureServices(IServiceCollection services)
-        {
-            services.AddStaticHttpContext();
-            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest);
-        }
-
-        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
-        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
-        {
-            if (env.IsDevelopment())
-            {
-                app.UseDeveloperExceptionPage();
-            }
-
-            app.UseRouting(); // 放在 UseStaticFiles 之后
-            app.UseEndpoints(endpoints =>
-           {
-               endpoints.MapControllers(); // 属性路由
-               endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}"); // 默认路由
-           });
-        }
-    }
-}

+ 2 - 3
NetCoreTest/appsettings.Development.json

@@ -1,9 +1,8 @@
 {
   "Logging": {
     "LogLevel": {
-      "Default": "Debug",
-      "System": "Information",
-      "Microsoft": "Information"
+      "Default": "Information",
+      "Microsoft.AspNetCore": "Warning"
     }
   }
 }

+ 2 - 1
NetCoreTest/appsettings.json

@@ -1,7 +1,8 @@
 {
   "Logging": {
     "LogLevel": {
-      "Default": "Warning"
+      "Default": "Information",
+      "Microsoft.AspNetCore": "Warning"
     }
   },
   "AllowedHosts": "*"