init lserp cs 5.0
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Security.Cryptography;
|
||||
using System.IO;
|
||||
|
||||
namespace Zhaizj.Framework.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// 证书加密解密
|
||||
/// </summary>
|
||||
public class LicenseEncry
|
||||
{
|
||||
|
||||
private SymmetricAlgorithm _algthm;
|
||||
private string _key;
|
||||
private string _IV;
|
||||
|
||||
/// <summary>
|
||||
/// 对称加密算法构造函数
|
||||
/// </summary>
|
||||
public LicenseEncry()
|
||||
{
|
||||
Inital();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 对称加密算法构造函数,需要传入密钥字符串
|
||||
/// </summary>
|
||||
/// <param name="Key">密钥字符串</param>
|
||||
public LicenseEncry(string Key)
|
||||
{
|
||||
Inital();
|
||||
_key = EncryKey(Key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 对称加密算法构造函数,需要传入密钥字符串和初始化向量字符串
|
||||
/// </summary>
|
||||
/// <param name="Key">密钥字符串</param>
|
||||
/// <param name="IV">初始向量字符串</param>
|
||||
public LicenseEncry(string Key,string IV)
|
||||
{
|
||||
Inital();
|
||||
_key = EncryKey(Key);
|
||||
_IV = IV;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 基本配置初始化
|
||||
/// </summary>
|
||||
private void Inital()
|
||||
{
|
||||
_algthm = new RijndaelManaged();
|
||||
//128位的key
|
||||
_key = "*)L7uaDdsl#@(*&873JKLF(*#klj*)L7uaDdsl#@(*&873JKLF(*#kljsdsfhjskldf789#()RU*(h9oa9#*w7R(Q#U(*)L7*)L7uaDdsl#@(*&873JKLF(*#kljsdsfhjskldf789#()RU*(h9oa9#*w7R(Q#U(uaDdsl#@(*&873JKLF(*#kljsdsfhjskldf789#()RU*(h9oa9#*w7R(Q#U(sdsfhjskldf789#()RU*(h9oa9#*w7R(Q#U(";
|
||||
_IV = "FG&^lsdlkjsdojslkad3240897&^9o34ly89f(*&#(!@_@_dfskldfhjks5d4f";
|
||||
//_algthm.KeySize = 256;
|
||||
}
|
||||
|
||||
private string EncryKey(string key)
|
||||
{
|
||||
//32位MD5加密
|
||||
string longKey = MD5Encrp.MD5(key);
|
||||
//将key变成64位的长密钥
|
||||
while(longKey.Length<256)
|
||||
{
|
||||
longKey += longKey;
|
||||
}
|
||||
return longKey;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 对原文进行加密后返回加密文
|
||||
/// </summary>
|
||||
/// <param name="Source"></param>
|
||||
/// <returns></returns>
|
||||
public string Encrypt(string Source)
|
||||
{
|
||||
//原文字节
|
||||
byte[] byteIn = UTF8Encoding.UTF8.GetBytes(Source);
|
||||
//密文流
|
||||
MemoryStream ms = new MemoryStream();
|
||||
|
||||
//加密算法转换器生成
|
||||
_algthm.Key = GetLegalKey();
|
||||
_algthm.IV = GetLegalIV();
|
||||
ICryptoTransform crypto = _algthm.CreateEncryptor();
|
||||
|
||||
//执行加密转换
|
||||
CryptoStream cs = new CryptoStream(ms, crypto, CryptoStreamMode.Write);
|
||||
cs.Write(byteIn, 0, byteIn.Length);
|
||||
cs.FlushFinalBlock();
|
||||
ms.Close();
|
||||
cs.Close();
|
||||
|
||||
//返回编码后的字符串结果
|
||||
byte[] byteOut = ms.ToArray();
|
||||
return Convert.ToBase64String(byteOut);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 对密文进行解密后返回加原文
|
||||
/// </summary>
|
||||
/// <param name="Source"></param>
|
||||
/// <returns></returns>
|
||||
public string Decrypt(string Source)
|
||||
{
|
||||
//密文字节
|
||||
byte[] byteIn = Convert.FromBase64String(Source);
|
||||
//密文流
|
||||
MemoryStream ms = new MemoryStream(byteIn, 0, byteIn.Length);
|
||||
|
||||
//解密算法转换器生成
|
||||
_algthm.Key = GetLegalKey();
|
||||
_algthm.IV = GetLegalIV();
|
||||
ICryptoTransform crypto = _algthm.CreateDecryptor();
|
||||
|
||||
//执行解密转换
|
||||
CryptoStream cs = new CryptoStream(ms, crypto, CryptoStreamMode.Read);
|
||||
|
||||
//返回编码后的字符串结果
|
||||
StreamReader sr = new StreamReader(cs);
|
||||
return sr.ReadToEnd();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 比较现有密钥与算法密钥的长度,使得现有密钥此成为合法长度
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private byte[] GetLegalKey()
|
||||
{
|
||||
string tmpKey = _key;
|
||||
_algthm.GenerateKey();
|
||||
|
||||
int okLen = _algthm.Key.Length;
|
||||
if (tmpKey.Length > okLen)
|
||||
{
|
||||
//_key过长则截取
|
||||
tmpKey = tmpKey.Substring(0, okLen);
|
||||
}
|
||||
else if (tmpKey.Length < okLen)
|
||||
{
|
||||
//_key过短则补充
|
||||
tmpKey = tmpKey.PadRight(okLen, ' ');
|
||||
}
|
||||
|
||||
return ASCIIEncoding.ASCII.GetBytes(tmpKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 比较现有IV与算法IV的长度,使得现有密钥此成为合法长度
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private byte[] GetLegalIV()
|
||||
{
|
||||
string tmpIV = _IV;
|
||||
_algthm.GenerateIV();
|
||||
int okLen = _algthm.IV.Length;
|
||||
if (tmpIV.Length > okLen)
|
||||
{
|
||||
//_IV过长则截取
|
||||
tmpIV = tmpIV.Substring(0, okLen);
|
||||
}
|
||||
else if (tmpIV.Length < okLen)
|
||||
{
|
||||
//_IV过短则补充
|
||||
tmpIV = tmpIV.PadRight(okLen, ' ');
|
||||
}
|
||||
return ASCIIEncoding.ASCII.GetBytes(tmpIV);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// MD5加密算法
|
||||
/// </summary>
|
||||
public static class MD5Encrp
|
||||
{
|
||||
/// <summary>
|
||||
/// MD5函数
|
||||
/// </summary>
|
||||
/// <param name="str">原始字符串</param>
|
||||
/// <returns>MD5结果</returns>
|
||||
public static string MD5(string str)
|
||||
{
|
||||
byte[] b = Encoding.Default.GetBytes(str);
|
||||
b = new MD5CryptoServiceProvider().ComputeHash(b);
|
||||
string ret = "";
|
||||
for (int i = 0; i < b.Length; i++)
|
||||
ret += b[i].ToString("x").PadLeft(2, '0');
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user