}
DES
加密
public class DES
{
private static readonly byte[] s_vector = { 0x21, 0x34, 0x65, 0x87, 0x90, 0xAB, 0xCD, 0xEF };
//public static string EncryptDES(string encryptString, string encryptKey)
//{
// try
// {
// byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
// byte[] rgbIV = s_vector;
// byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
// DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
// MemoryStream mStream = new MemoryStream();
// CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
// cStream.Write(inputByteArray, 0, inputByteArray.Length);
// cStream.FlushFinalBlock();
// return Convert.ToBase64String(mStream.ToArray());
// }
// catch
// {
// return encryptString;
// }
//}
public static string Encode(string encryptString, string encryptKey)
{
encryptKey = GetLegalKey(encryptKey);
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey);
byte[] rgbIV = s_vector;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return HEX.BytesToHexString(mStream.ToArray());
}
public static string Decode(string decryptString, string decryptKey)
{
decryptKey = GetLegalKey(decryptKey);
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
byte[] rgbIV = s_vector;
byte[] inputByteArray = HEX.HexStringToBytes(decryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
dCSP.Mode = CipherMode.CBC;
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
private static string GetLegalKey(string key)
{
if (key.Length < 8)
key = key.PadRight(8, ' ');
if (key.Length > 8)
key = key.Substring(0, 8);
return key;
}
}
解密
public class ApiAuthAttribute : AuthorizeAttribute
{
static bool IsTest = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["IsTest"]);
public static string TokenEncryptionCode = "YFK89568";
private static string TokenKey = "X-YF-Token";
public override void OnAuthorization(HttpActionContext actionContext)
{
string Token = null;
//获取token
if (actionContext.Request.Headers.Contains(TokenKey))
{
Token = actionContext.Request.Headers.GetValues(TokenKey).First();
}
//测试token
if (IsTest && string.IsNullOrWhiteSpace(Token))
{
if (actionContext.Request.Headers.GetCookies().Count > 0)
{
System.Net.Http.Headers.CookieHeaderValue obj = actionContext.Request.Headers.GetCookies().First();
Token = obj[TokenKey].Value;
}
}
if (string.IsNullOrWhiteSpace(Token))
{
ReturnNode returnNode = new ReturnNode();
returnNode.Code = -10;
returnNode.Data = "";
returnNode.Msg = "身份验证失败";
string Msg = Newtonsoft.Json.JsonConvert.SerializeObject(returnNode);
actionContext.Response = actionContext.Response = new HttpResponseMessage { Content = new StringContent(Msg, System.Text.Encoding.UTF8, "application/json") };
return;
}
//解密
CommonUserMsg user = null;
try
{
string Destr = DES.Decode(Token, TokenEncryptionCode);
user = (CommonUserMsg)JsonConvert.DeserializeObject(Destr, typeof(CommonUserMsg));
}
catch (Exception ex)
{
ReturnNode returnNode = new ReturnNode();
returnNode.Code = -11;
returnNode.Data = "";
returnNode.Msg = "身份验证失败";
string Msg = Newtonsoft.Json.JsonConvert.SerializeObject(returnNode);
actionContext.Response = actionContext.Response = new HttpResponseMessage { Content = new StringContent(Msg, System.Text.Encoding.UTF8, "application/json") };
return;
}
WebApiPrincipal principal = new WebApiPrincipal(new WebApiIdentity
{
UserId = user.UserId.ToString(),
Name = user.UserName,
Role = user.Role,
});
HttpContext.Current.User = principal;
}
}
建议学会用mvc的过滤器