添加加密解密帮助类
This commit is contained in:
parent
62da771e07
commit
60a8b8eda8
102
WebSiteCode/Cmdjy/Cmdjy/Bll/DesHelper.cs
Normal file
102
WebSiteCode/Cmdjy/Cmdjy/Bll/DesHelper.cs
Normal 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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -153,6 +153,7 @@
|
||||||
<Compile Include="App_Start\BundleConfig.cs" />
|
<Compile Include="App_Start\BundleConfig.cs" />
|
||||||
<Compile Include="App_Start\FilterConfig.cs" />
|
<Compile Include="App_Start\FilterConfig.cs" />
|
||||||
<Compile Include="App_Start\RouteConfig.cs" />
|
<Compile Include="App_Start\RouteConfig.cs" />
|
||||||
|
<Compile Include="Bll\DesHelper.cs" />
|
||||||
<Compile Include="Dal\Wappers\HisDrugInfoWapper.cs" />
|
<Compile Include="Dal\Wappers\HisDrugInfoWapper.cs" />
|
||||||
<Compile Include="Dal\Wappers\HisPrescriptyInfoWapper.cs" />
|
<Compile Include="Dal\Wappers\HisPrescriptyInfoWapper.cs" />
|
||||||
<Compile Include="Controllers\HomeController.cs" />
|
<Compile Include="Controllers\HomeController.cs" />
|
||||||
|
@ -218,7 +219,6 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="App_Data\" />
|
<Folder Include="App_Data\" />
|
||||||
<Folder Include="Bll\" />
|
|
||||||
<Folder Include="Models\" />
|
<Folder Include="Models\" />
|
||||||
<Folder Include="Views\Test\" />
|
<Folder Include="Views\Test\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user