From c820d37521b9f4de23cb035ebce55323fe7dd1d6 Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Wed, 1 Apr 2020 10:54:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FAuth/Controllers/api/UserController.cs | 3 +- FAuth/Extensions/Decryptor/EncryptyWorker.cs | 105 +++++++++++++++++++ FAuth/FAuth.csproj | 14 +-- 3 files changed, 114 insertions(+), 8 deletions(-) create mode 100644 FAuth/Extensions/Decryptor/EncryptyWorker.cs diff --git a/FAuth/Controllers/api/UserController.cs b/FAuth/Controllers/api/UserController.cs index 1151716..d7d68f9 100644 --- a/FAuth/Controllers/api/UserController.cs +++ b/FAuth/Controllers/api/UserController.cs @@ -6,6 +6,7 @@ using FAuth.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using FAuth.DataBase.Tables; +using Microsoft.AspNetCore.Mvc.ModelBinding; namespace FAuth.Controllers.api { @@ -39,7 +40,7 @@ namespace FAuth.Controllers.api /// 用户信息 [HttpPost] [ProducesResponseType(typeof(UserInfo),200)] - public UserInfo GetUserByTicket(string ticket) { + public UserInfo GetUserByTicket([BindRequired]string ticket) { return new UserInfo { UserName = "aaa" }; diff --git a/FAuth/Extensions/Decryptor/EncryptyWorker.cs b/FAuth/Extensions/Decryptor/EncryptyWorker.cs new file mode 100644 index 0000000..56abbde --- /dev/null +++ b/FAuth/Extensions/Decryptor/EncryptyWorker.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using System.Threading.Tasks; + +namespace FAuth.Extensions.Decryptor +{ + /// + /// 加密帮助类 + /// + public class EncryptyWorker + { + /// + /// 补位填充方式 + /// + public PaddingMode Padding { get; set; } + /// + /// 加密算法模式 + /// + public CipherMode Cipher { get; set; } + /// + /// 初始化向量 + /// + public byte[] IV { get; set; } + /// + /// 加密字符串获取Base64密文 + /// + /// 加密key + /// 要加密的字符串 + public string Encrypty(string key,string str) { + if(key.Length < 8) { + throw new Exception("加密用的key长度为8位"); + } + byte[] bKey = Encoding.UTF8.GetBytes(key.Substring(0,8)); + byte[] bStr = Encoding.UTF8.GetBytes(str); + var desc = createProvider(); + using(var mStream = new MemoryStream()) { + using(var cStream = new CryptoStream(mStream,desc.CreateEncryptor(bKey,IV),CryptoStreamMode.Write)) { + cStream.Write(bStr,0,bStr.Length); + cStream.FlushFinalBlock(); + return Convert.ToBase64String(mStream.ToArray()); + } + } + } + /// + /// 从Base64密文解密,获取字符串 + /// + /// 加密键 + /// 密文 + public string DesCrypty(string key,string str) { + if(key.Length < 8) { + throw new Exception("加密用的key长度为8位"); + } + var bStr = Convert.FromBase64String(str); + byte[] bKey = Encoding.UTF8.GetBytes(key.Substring(0,8)); + var desc = createProvider(); + using(var mStream = new MemoryStream()) { + using(var cStream = new CryptoStream(mStream,desc.CreateDecryptor(bKey,IV),CryptoStreamMode.Write)) { + cStream.Write(bStr,0,bStr.Length); + cStream.FlushFinalBlock(); + return Encoding.UTF8.GetString(mStream.ToArray()); + } + } + } + + private DESCryptoServiceProvider createProvider() { + var desc = new DESCryptoServiceProvider(); + desc.Padding = this.Padding; + desc.Mode = this.Cipher; + return desc; + } + + private static EncryptyWorker Deshelper { get; set; } + + private EncryptyWorker() { } + + /// + /// 重用模式获取默认加密帮助器 + /// + public static EncryptyWorker GetHelper() { + if(Deshelper == null) { + byte[] IV = { 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08 }; + Deshelper = GetHelper(PaddingMode.PKCS7,CipherMode.CBC,IV); + + } + return Deshelper; + } + /// + /// 生成新的加密帮助器 + /// + /// 补位方式 + /// 加密算法模式 + /// 初始化向量 + public static EncryptyWorker GetHelper(PaddingMode padding,CipherMode cipher,byte[] IV) { + return new EncryptyWorker { + Cipher = cipher, + IV = IV, + Padding = padding, + }; + } + } +} diff --git a/FAuth/FAuth.csproj b/FAuth/FAuth.csproj index cf110ed..a9c2886 100644 --- a/FAuth/FAuth.csproj +++ b/FAuth/FAuth.csproj @@ -1,4 +1,4 @@ - + netcoreapp3.1 @@ -7,12 +7,12 @@ - - - - - - + + + + + +