using System;
using System.Web.Security;
using System.Text;
using System.Security.Cryptography;
namespace EncryptAndDecode
{
/// <summary>
/// Functions 的摘要说明。
/// </summary>
public class EncryptAndDecode
{
public EncryptAndDecode()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region Encrypt 单向加密函数
/// <summary>
/// 加密函数
/// </summary>
/// <param name="PasswordString">待加密的字符串</param>
/// <param name="PasswordFormat">加密的方式</param>
/// <returns></returns>
public static string Encrypt(string PasswordString,string PasswordFormat)
{
string EncryptPassword;
if (PasswordFormat.ToUpper() =="SHA1")
{
EncryptPassword=FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString ,"SHA1");
}
else if (PasswordFormat=="MD5")
{
EncryptPassword=FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString ,"MD5");
}
else
{
EncryptPassword="";
}
return EncryptPassword;
}
#endregion
#region Encode 可逆加密解密函数
/// <summary>
/// 加密函数
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Encode(string str)
{
int i=0;
StringBuilder sb = new StringBuilder();
foreach(char a in str)
{
switch(i % 5)
{
case 0:
sb.Append((char)(a+1));
break;
case 1:
sb.Append((char)(a+2));
break;
case 2:
sb.Append((char)(a+3));
break;
case 3:
sb.Append((char)(a+4));
break;
case 4:
sb.Append((char)(a+5));
break;
}
i++;
}
return sb.ToString();
}
/// <summary>
/// 解密函数
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Decode(string str)
{
int i=0;
StringBuilder sb = new StringBuilder();
foreach(char a in str)
{
switch(i % 5)
{
case 0:
sb.Append((char)(a-1));
break;
case 1:
sb.Append((char)(a-2));
break;
case 2:
sb.Append((char)(a-3));
break;
case 3:
sb.Append((char)(a-4));
break;
case 4:
sb.Append((char)(a-5));
break;
}
i++;
}
return sb.ToString();
}
#endregion
/// <summary>
/// md5加密字符串
/// </summary>
/// <param name="source">源字符串</param>
/// <param name="key">密钥</param>
/// <returns>加密后的字符串</returns>
public static string Encode(string source, string key)
{
MD5 md5 = new MD5CryptoServiceProvider();
ASCIIEncoding code = new ASCIIEncoding();
string str = source + key.ToLower();
byte[] bit = code.GetBytes(str);
str = Convert.ToBase64String(bit);
bit = code.GetBytes(str);
bit = md5.ComputeHash(bit);
str = Convert.ToBase64String(bit);
return str;
}
}
}
using System.Web.Security;
using System.Text;
using System.Security.Cryptography;
namespace EncryptAndDecode
{
/// <summary>
/// Functions 的摘要说明。
/// </summary>
public class EncryptAndDecode
{
public EncryptAndDecode()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region Encrypt 单向加密函数
/// <summary>
/// 加密函数
/// </summary>
/// <param name="PasswordString">待加密的字符串</param>
/// <param name="PasswordFormat">加密的方式</param>
/// <returns></returns>
public static string Encrypt(string PasswordString,string PasswordFormat)
{
string EncryptPassword;
if (PasswordFormat.ToUpper() =="SHA1")
{
EncryptPassword=FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString ,"SHA1");
}
else if (PasswordFormat=="MD5")
{
EncryptPassword=FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString ,"MD5");
}
else
{
EncryptPassword="";
}
return EncryptPassword;
}
#endregion
#region Encode 可逆加密解密函数
/// <summary>
/// 加密函数
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Encode(string str)
{
int i=0;
StringBuilder sb = new StringBuilder();
foreach(char a in str)
{
switch(i % 5)
{
case 0:
sb.Append((char)(a+1));
break;
case 1:
sb.Append((char)(a+2));
break;
case 2:
sb.Append((char)(a+3));
break;
case 3:
sb.Append((char)(a+4));
break;
case 4:
sb.Append((char)(a+5));
break;
}
i++;
}
return sb.ToString();
}
/// <summary>
/// 解密函数
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Decode(string str)
{
int i=0;
StringBuilder sb = new StringBuilder();
foreach(char a in str)
{
switch(i % 5)
{
case 0:
sb.Append((char)(a-1));
break;
case 1:
sb.Append((char)(a-2));
break;
case 2:
sb.Append((char)(a-3));
break;
case 3:
sb.Append((char)(a-4));
break;
case 4:
sb.Append((char)(a-5));
break;
}
i++;
}
return sb.ToString();
}
#endregion
/// <summary>
/// md5加密字符串
/// </summary>
/// <param name="source">源字符串</param>
/// <param name="key">密钥</param>
/// <returns>加密后的字符串</returns>
public static string Encode(string source, string key)
{
MD5 md5 = new MD5CryptoServiceProvider();
ASCIIEncoding code = new ASCIIEncoding();
string str = source + key.ToLower();
byte[] bit = code.GetBytes(str);
str = Convert.ToBase64String(bit);
bit = code.GetBytes(str);
bit = md5.ComputeHash(bit);
str = Convert.ToBase64String(bit);
return str;
}
}
}