添加加密解密帮助类

This commit is contained in:
falcon 2019-03-01 17:01:53 +08:00
parent 62da771e07
commit 60a8b8eda8
2 changed files with 103 additions and 1 deletions

View File

@ -0,0 +1,102 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Cmdjy.Bll
{
/// <summary>
/// 加密帮助类
/// </summary>
public class DesHelper
{
/// <summary>
/// 补位填充方式
/// </summary>
public PaddingMode Padding { get; set; }
/// <summary>
/// 加密算法模式
/// </summary>
public CipherMode Cipher { get; set; }
/// <summary>
/// 初始化向量
/// </summary>
public byte[] IV { get; set; }
/// <summary>
/// 加密key
/// </summary>
public string Key { get; set; }
/// <summary>
/// 加密字符串获取Base64密文
/// </summary>
/// <param name="str">要加密的字符串</param>
public string Encrypty(string str) {
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="str">密文</param>
/// <returns></returns>
public string DesCrypty(string str) {
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 DesHelper _deshelper { get; set; }
private DesHelper() { }
/// <summary>
/// 重用模式获取默认加密帮助器
/// </summary>
public static DesHelper GetHelper() {
if(_deshelper == null) {
var key = "";
byte[] IV = { 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08 };
_deshelper = GetHelper(PaddingMode.PKCS7,CipherMode.CBC,IV,key);
}
return _deshelper;
}
/// <summary>
/// 生成新的加密帮助器
/// </summary>
/// <param name="padding">补位方式</param>
/// <param name="cipher">加密算法模式</param>
/// <param name="IV">初始化向量</param>
/// <param name="key">加密关键字</param>
public static DesHelper GetHelper(PaddingMode padding,CipherMode cipher,byte[] IV,string key) {
if(key.Length <= 8) {
throw new Exception("加密用的key长度为8位");
}
return new DesHelper {
Cipher = cipher,IV = IV,Key = key,Padding = padding,
};
}
}
}

View File

@ -153,6 +153,7 @@
<Compile Include="App_Start\BundleConfig.cs" />
<Compile Include="App_Start\FilterConfig.cs" />
<Compile Include="App_Start\RouteConfig.cs" />
<Compile Include="Bll\DesHelper.cs" />
<Compile Include="Dal\Wappers\HisDrugInfoWapper.cs" />
<Compile Include="Dal\Wappers\HisPrescriptyInfoWapper.cs" />
<Compile Include="Controllers\HomeController.cs" />
@ -218,7 +219,6 @@
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
<Folder Include="Bll\" />
<Folder Include="Models\" />
<Folder Include="Views\Test\" />
</ItemGroup>