8 changed files with 164 additions and 35 deletions
@ -0,0 +1,85 @@ |
|||
using System; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using System.Security.Cryptography; |
|||
using System.Text; |
|||
|
|||
namespace BBWY.Common.Extensions |
|||
{ |
|||
public static class EncryptionExtension |
|||
{ |
|||
|
|||
public static string Md5Encrypt(this string originStr) |
|||
{ |
|||
using (var md5 = MD5.Create()) |
|||
{ |
|||
return string.Join(string.Empty, md5.ComputeHash(Encoding.UTF8.GetBytes(originStr)).Select(x => x.ToString("x2"))); |
|||
} |
|||
} |
|||
|
|||
//AES加密 传入,要加密的串和, 解密key
|
|||
public static string AESEncrypt(this string input) |
|||
{ |
|||
var key = "dataplatform2019"; |
|||
var ivStr = "1012132405963708"; |
|||
|
|||
var encryptKey = Encoding.UTF8.GetBytes(key); |
|||
var iv = Encoding.UTF8.GetBytes(ivStr); //偏移量,最小为16
|
|||
using (var aesAlg = Aes.Create()) |
|||
{ |
|||
using (var encryptor = aesAlg.CreateEncryptor(encryptKey, iv)) |
|||
{ |
|||
using (var msEncrypt = new MemoryStream()) |
|||
{ |
|||
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, |
|||
CryptoStreamMode.Write)) |
|||
|
|||
using (var swEncrypt = new StreamWriter(csEncrypt)) |
|||
{ |
|||
swEncrypt.Write(input); |
|||
} |
|||
var decryptedContent = msEncrypt.ToArray(); |
|||
|
|||
return Convert.ToBase64String(decryptedContent); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
public static string AESDecrypt(this string cipherText) |
|||
{ |
|||
var fullCipher = Convert.FromBase64String(cipherText); |
|||
|
|||
var ivStr = "1012132405963708"; |
|||
var key = "dataplatform2019"; |
|||
|
|||
var iv = Encoding.UTF8.GetBytes(ivStr); |
|||
var decryptKey = Encoding.UTF8.GetBytes(key); |
|||
|
|||
using (var aesAlg = Aes.Create()) |
|||
{ |
|||
using (var decryptor = aesAlg.CreateDecryptor(decryptKey, iv)) |
|||
{ |
|||
string result; |
|||
using (var msDecrypt = new MemoryStream(fullCipher)) |
|||
{ |
|||
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) |
|||
{ |
|||
using (var srDecrypt = new StreamReader(csDecrypt)) |
|||
{ |
|||
result = srDecrypt.ReadToEnd(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
} |
|||
} |
|||
|
|||
public static string Base64Encrypt(this string originStr) |
|||
{ |
|||
return Convert.ToBase64String(Encoding.UTF8.GetBytes(originStr)); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,32 @@ |
|||
using BBWY.Server.Business; |
|||
using Microsoft.AspNetCore.Authentication.JwtBearer; |
|||
using Microsoft.AspNetCore.Authorization; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using System.Data; |
|||
|
|||
namespace BBWY.Server.API.Controllers |
|||
{ |
|||
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] |
|||
public class SqlController : BaseApiController |
|||
{ |
|||
private SqlBusiness sqlBusiness; |
|||
|
|||
public SqlController(IHttpContextAccessor httpContextAccessor, SqlBusiness sqlBusiness) : base(httpContextAccessor) |
|||
{ |
|||
this.sqlBusiness = sqlBusiness; |
|||
} |
|||
|
|||
[HttpPost] |
|||
public int ExecuteNonQuery([FromBody]BaseSqlData baseSqlData) |
|||
{ |
|||
return sqlBusiness.ExecuteNonQuery(baseSqlData); |
|||
} |
|||
|
|||
[HttpPost] |
|||
public DataTable ExecuteDataTable([FromBody] BaseSqlData baseSqlData) |
|||
{ |
|||
return sqlBusiness.ExecuteDataTable(baseSqlData); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,33 @@ |
|||
using BBWY.Common.Extensions; |
|||
using BBWY.Common.Models; |
|||
using System.Data; |
|||
using Yitter.IdGenerator; |
|||
|
|||
namespace BBWY.Server.Business |
|||
{ |
|||
/// <summary>
|
|||
/// SQL业务类,仅适用内部系统之间调用
|
|||
/// </summary>
|
|||
public class SqlBusiness : BaseBusiness, IDenpendency |
|||
{ |
|||
public SqlBusiness(IFreeSql fsql, NLogManager nLogManager, IIdGenerator idGenerator) : base(fsql, nLogManager, idGenerator) |
|||
{ |
|||
|
|||
} |
|||
|
|||
public int ExecuteNonQuery(BaseSqlData baseSqlData) |
|||
{ |
|||
return fsql.Ado.ExecuteNonQuery(baseSqlData.Sql.AESDecrypt()); |
|||
} |
|||
|
|||
public DataTable ExecuteDataTable(BaseSqlData baseSqlData) |
|||
{ |
|||
return fsql.Ado.ExecuteDataTable(baseSqlData.Sql.AESDecrypt()); |
|||
} |
|||
} |
|||
|
|||
public class BaseSqlData |
|||
{ |
|||
public string Sql { get; set; } |
|||
} |
|||
} |
Loading…
Reference in new issue