升级组件

This commit is contained in:
falcon 2020-04-01 10:54:48 +08:00
parent 5fc4020f46
commit c820d37521
3 changed files with 114 additions and 8 deletions

View File

@ -6,6 +6,7 @@ using FAuth.Models;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using FAuth.DataBase.Tables; using FAuth.DataBase.Tables;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace FAuth.Controllers.api namespace FAuth.Controllers.api
{ {
@ -39,7 +40,7 @@ namespace FAuth.Controllers.api
/// <returns>用户信息</returns> /// <returns>用户信息</returns>
[HttpPost] [HttpPost]
[ProducesResponseType(typeof(UserInfo),200)] [ProducesResponseType(typeof(UserInfo),200)]
public UserInfo GetUserByTicket(string ticket) { public UserInfo GetUserByTicket([BindRequired]string ticket) {
return new UserInfo { return new UserInfo {
UserName = "aaa" UserName = "aaa"
}; };

View File

@ -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
{
/// <summary>
/// 加密帮助类
/// </summary>
public class EncryptyWorker
{
/// <summary>
/// 补位填充方式
/// </summary>
public PaddingMode Padding { get; set; }
/// <summary>
/// 加密算法模式
/// </summary>
public CipherMode Cipher { get; set; }
/// <summary>
/// 初始化向量
/// </summary>
public byte[] IV { get; set; }
/// <summary>
/// 加密字符串获取Base64密文
/// </summary>
/// <param name="key">加密key</param>
/// <param name="str">要加密的字符串</param>
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());
}
}
}
/// <summary>
/// 从Base64密文解密获取字符串
/// </summary>
/// <param name="key">加密键</param>
/// <param name="str">密文</param>
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() { }
/// <summary>
/// 重用模式获取默认加密帮助器
/// </summary>
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;
}
/// <summary>
/// 生成新的加密帮助器
/// </summary>
/// <param name="padding">补位方式</param>
/// <param name="cipher">加密算法模式</param>
/// <param name="IV">初始化向量</param>
public static EncryptyWorker GetHelper(PaddingMode padding,CipherMode cipher,byte[] IV) {
return new EncryptyWorker {
Cipher = cipher,
IV = IV,
Padding = padding,
};
}
}
}

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
@ -7,12 +7,12 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Redis" Version="2.2.0" /> <PackageReference Include="Microsoft.Extensions.Caching.Redis" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.2" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.3" />
<PackageReference Include="NLog" Version="4.6.8" /> <PackageReference Include="NLog" Version="4.7.0" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.0" /> <PackageReference Include="NLog.Web.AspNetCore" Version="4.9.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.1.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="5.2.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.3" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>