谈谈数据加密的处理--提供各种算法处理
7、 MD5及Base64结合加密
这种结合方式也是比较不错的算法,至少是四不像,一般分析比较困难,比较有迷惑性,就是挂羊头,卖狗肉的行为,其实Base64加密方式,也可以做的比较复杂,不一定是上面的那种解决方式的,你可以添加一些较为复杂的算法在里面的。
/// <summary>
/// 加密字符串
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string EncryptString(string input)
{
return MD5Util.AddMD5Profix(Base64Util.Encrypt(MD5Util.AddMD5Profix(input)));
//return Base64.Encrypt(MD5.AddMD5Profix(Base64.Encrypt(input)));
}
/// <summary>
/// 解密加过密的字符串
/// </summary>
/// <param name="input"></param>
/// <param name="throwException">解密失败是否抛异常</param>
/// <returns></returns>
public static string DecryptString(string input, bool throwException)
{
string res = "";
try
{
res = input;// Base64.Decrypt(input);
if (MD5Util.ValidateValue(res))
{
return MD5Util.RemoveMD5Profix(Base64Util.Decrypt(MD5Util.RemoveMD5Profix(res)));
}
else
{
throw new Exception("字符串无法转换成功!");
}
}
catch
{
if (throwException)
{
throw;
}
else
{
return "";
}
}
}
//-----------下面是MD5Util下面的函数
/// <summary>
/// 添加MD5的前缀,便于检查有无篡改
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string AddMD5Profix(string input)
{
return GetMD5_4(input) + input;
}
/// <summary>
/// 移除MD5的前缀
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string RemoveMD5Profix(string input)
{
return input.Substring(4);
}
/// <summary>
/// 验证MD5前缀处理的字符串有无被篡改
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool ValidateValue(string input)
{
bool res = false;
if (input.Length >= 4)
{
string tmp = input.Substring(4);
if (input.Substring(0, 4) == GetMD5_4(tmp))
{
res = true;
}
}
return res;
}
/// 加密字符串
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string EncryptString(string input)
{
return MD5Util.AddMD5Profix(Base64Util.Encrypt(MD5Util.AddMD5Profix(input)));
//return Base64.Encrypt(MD5.AddMD5Profix(Base64.Encrypt(input)));
}
/// <summary>
/// 解密加过密的字符串
/// </summary>
/// <param name="input"></param>
/// <param name="throwException">解密失败是否抛异常</param>
/// <returns></returns>
public static string DecryptString(string input, bool throwException)
{
string res = "";
try
{
res = input;// Base64.Decrypt(input);
if (MD5Util.ValidateValue(res))
{
return MD5Util.RemoveMD5Profix(Base64Util.Decrypt(MD5Util.RemoveMD5Profix(res)));
}
else
{
throw new Exception("字符串无法转换成功!");
}
}
catch
{
if (throwException)
{
throw;
}
else
{
return "";
}
}
}
//-----------下面是MD5Util下面的函数
/// <summary>
/// 添加MD5的前缀,便于检查有无篡改
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string AddMD5Profix(string input)
{
return GetMD5_4(input) + input;
}
/// <summary>
/// 移除MD5的前缀
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string RemoveMD5Profix(string input)
{
return input.Substring(4);
}
/// <summary>
/// 验证MD5前缀处理的字符串有无被篡改
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool ValidateValue(string input)
{
bool res = false;
if (input.Length >= 4)
{
string tmp = input.Substring(4);
if (input.Substring(0, 4) == GetMD5_4(tmp))
{
res = true;
}
}
return res;
}
如下面代码就是上面提到的Base64Util中的内容,好像算法处理还是比较麻烦,也贴出来大家研读一下吧。
/// <summary>
/// 基于Base64的加密编码,
/// 可以设置不同的密码表来获取不同的编码合解码
/// </summary>
public class Base64Util
{
public Base64Util()
{
this.InitDict();
}
protected static Base64Util s_b64 = new Base64Util();
/// <summary>
/// 使用默认的密码表加密字符串
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string Encrypt(string input)
{
return s_b64.Encode(input);
}
/// <summary>
/// 使用默认的密码表解密字符串
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string Decrypt(string input)
{
return s_b64.Decode(input);
}
/// <summary>
/// 获取具有标准的Base64密码表的加密类
/// </summary>
/// <returns></returns>
public static Base64Util GetStandardBase64()
{
Base64Util b64 = new Base64Util();
b64.Pad = "=";
b64.CodeTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
return b64;
}
protected string m_codeTable = @"ABCDEFGHIJKLMNOPQRSTUVWXYZbacdefghijklmnopqrstu_wxyz0123456789*-";
protected string m_pad = "v";
protected Dictionary<int, char> m_t1 = new Dictionary<int, char>();
protected Dictionary<char, int> m_t2 = new Dictionary<char, int>();
/// <summary>
/// 密码表
/// </summary>
public string CodeTable
{
get { return m_codeTable; }
set
{
if (value == null)
{
throw new Exception("密码表不能为null");
}
else if (value.Length < 64)
{
throw new Exception("密码表长度必须至少为64");
}
else
{
this.ValidateRepeat(value);
this.ValidateEqualPad(value, m_pad);
m_codeTable = value;
this.InitDict();
}
}
}
/// <summary>
/// 补码
/// </summary>
public string Pad
{
get { return m_pad; }
set
{
if (value == null)
{
throw new Exception("密码表的补码不能为null");
}
else if (value.Length != 1)
{
throw new Exception("密码表的补码长度必须为1");
}
else
{
this.ValidateEqualPad(m_codeTable, value);
m_pad = value;
this.InitDict();
}
}
}
/// <summary>
/// 返回编码后的字符串
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public string Encode(string source)
{
if (source == null || source == "")
{
return "";
}
else
{
StringBuilder sb = new StringBuilder();
byte[] tmp = System.Text.UTF8Encoding.UTF8.GetBytes(source);
int remain = tmp.Length % 3;
int patch = 3 - remain;
if (remain != 0)
{
Array.Resize(ref tmp, tmp.Length + patch);
}
int cnt = (int)Math.Ceiling(tmp.Length * 1.0 / 3);
for (int i = 0; i < cnt; i++)
{
sb.Append(this.EncodeUnit(tmp[i * 3], tmp[i * 3 + 1], tmp[i * 3 + 2]));
}
if (remain != 0)
{
sb.Remove(sb.Length - patch, patch);
for (int i = 0; i < patch; i++)
{
sb.Append(m_pad);
}
}
return sb.ToString();
}
}
protected string EncodeUnit(params byte[] unit)
{
int[] obj = new int[4];
obj[0] = (unit[0] & 0xfc) >> 2;
obj[1] = ((unit[0] & 0x03) << 4) + ((unit[1] & 0xf0) >> 4);
obj[2] = ((unit[1] & 0x0f) << 2) + ((unit[2] & 0xc0) >> 6);
obj[3] = unit[2] & 0x3f;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < obj.Length; i++)
{
sb.Append(this.GetEC((int)obj[i]));
}
return sb.ToString();
}
protected char GetEC(int code)
{
return m_t1[code];//m_codeTable[code];
}
/// <summary>
/// 获得解码字符串
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public string Decode(string source)
{
if (source == null || source == "")
{
return "";
}
else
{
List<byte> list = new List<byte>();
char[] tmp = source.ToCharArray();
int remain = tmp.Length % 4;
if (remain != 0)
{
Array.Resize(ref tmp, tmp.Length - remain);
}
int patch = source.IndexOf(m_pad);
if (patch != -1)
{
patch = source.Length - patch;
}
int cnt = tmp.Length / 4;
for (int i = 0; i < cnt; i++)
{
this.DecodeUnit(list, tmp[i * 4], tmp[i * 4 + 1], tmp[i * 4 + 2], tmp[i * 4 + 3]);
}
for (int i = 0; i < patch; i++)
{
list.RemoveAt(list.Count - 1);
}
return System.Text.Encoding.UTF8.GetString(list.ToArray());
}
}
protected void DecodeUnit(List<byte> byteArr, params char[] chArray)
{
int[] res = new int[3];
byte[] unit = new byte[chArray.Length];
for (int i = 0; i < chArray.Length; i++)
{
unit[i] = this.FindChar(chArray[i]);
}
res[0] = (unit[0] << 2) + ((unit[1] & 0x30) >> 4);
res[1] = ((unit[1] & 0xf) << 4) + ((unit[2] & 0x3c) >> 2);
res[2] = ((unit[2] & 0x3) << 6) + unit[3];
for (int i = 0; i < res.Length; i++)
{
byteArr.Add((byte)res[i]);
}
}
protected byte FindChar(char ch)
{
int pos = m_t2[ch];//m_codeTable.IndexOf(ch);
return (byte)pos;
}
/// <summary>
/// 初始化双向哈西字典
/// </summary>
protected void InitDict()
{
m_t1.Clear();
m_t2.Clear();
m_t2.Add(m_pad[0], -1);
for (int i = 0; i < m_codeTable.Length; i++)
{
m_t1.Add(i, m_codeTable[i]);
m_t2.Add(m_codeTable[i], i);
}
}
/// <summary>
/// 检查字符串中的字符是否有重复
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
protected void ValidateRepeat(string input)
{
for (int i = 0; i < input.Length; i++)
{
if (input.LastIndexOf(input[i]) > i)
{
throw new Exception("密码表中含有重复字符:" + input[i]);
}
}
}
/// <summary>
/// 检查字符串是否包含补码字符
/// </summary>
/// <param name="input"></param>
/// <param name="pad"></param>
protected void ValidateEqualPad(string input, string pad)
{
if (input.IndexOf(pad) > -1)
{
throw new Exception("密码表中包含了补码字符:" + pad);
}
}
}
/// 基于Base64的加密编码,
/// 可以设置不同的密码表来获取不同的编码合解码
/// </summary>
public class Base64Util
{
public Base64Util()
{
this.InitDict();
}
protected static Base64Util s_b64 = new Base64Util();
/// <summary>
/// 使用默认的密码表加密字符串
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string Encrypt(string input)
{
return s_b64.Encode(input);
}
/// <summary>
/// 使用默认的密码表解密字符串
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string Decrypt(string input)
{
return s_b64.Decode(input);
}
/// <summary>
/// 获取具有标准的Base64密码表的加密类
/// </summary>
/// <returns></returns>
public static Base64Util GetStandardBase64()
{
Base64Util b64 = new Base64Util();
b64.Pad = "=";
b64.CodeTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
return b64;
}
protected string m_codeTable = @"ABCDEFGHIJKLMNOPQRSTUVWXYZbacdefghijklmnopqrstu_wxyz0123456789*-";
protected string m_pad = "v";
protected Dictionary<int, char> m_t1 = new Dictionary<int, char>();
protected Dictionary<char, int> m_t2 = new Dictionary<char, int>();
/// <summary>
/// 密码表
/// </summary>
public string CodeTable
{
get { return m_codeTable; }
set
{
if (value == null)
{
throw new Exception("密码表不能为null");
}
else if (value.Length < 64)
{
throw new Exception("密码表长度必须至少为64");
}
else
{
this.ValidateRepeat(value);
this.ValidateEqualPad(value, m_pad);
m_codeTable = value;
this.InitDict();
}
}
}
/// <summary>
/// 补码
/// </summary>
public string Pad
{
get { return m_pad; }
set
{
if (value == null)
{
throw new Exception("密码表的补码不能为null");
}
else if (value.Length != 1)
{
throw new Exception("密码表的补码长度必须为1");
}
else
{
this.ValidateEqualPad(m_codeTable, value);
m_pad = value;
this.InitDict();
}
}
}
/// <summary>
/// 返回编码后的字符串
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public string Encode(string source)
{
if (source == null || source == "")
{
return "";
}
else
{
StringBuilder sb = new StringBuilder();
byte[] tmp = System.Text.UTF8Encoding.UTF8.GetBytes(source);
int remain = tmp.Length % 3;
int patch = 3 - remain;
if (remain != 0)
{
Array.Resize(ref tmp, tmp.Length + patch);
}
int cnt = (int)Math.Ceiling(tmp.Length * 1.0 / 3);
for (int i = 0; i < cnt; i++)
{
sb.Append(this.EncodeUnit(tmp[i * 3], tmp[i * 3 + 1], tmp[i * 3 + 2]));
}
if (remain != 0)
{
sb.Remove(sb.Length - patch, patch);
for (int i = 0; i < patch; i++)
{
sb.Append(m_pad);
}
}
return sb.ToString();
}
}
protected string EncodeUnit(params byte[] unit)
{
int[] obj = new int[4];
obj[0] = (unit[0] & 0xfc) >> 2;
obj[1] = ((unit[0] & 0x03) << 4) + ((unit[1] & 0xf0) >> 4);
obj[2] = ((unit[1] & 0x0f) << 2) + ((unit[2] & 0xc0) >> 6);
obj[3] = unit[2] & 0x3f;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < obj.Length; i++)
{
sb.Append(this.GetEC((int)obj[i]));
}
return sb.ToString();
}
protected char GetEC(int code)
{
return m_t1[code];//m_codeTable[code];
}
/// <summary>
/// 获得解码字符串
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public string Decode(string source)
{
if (source == null || source == "")
{
return "";
}
else
{
List<byte> list = new List<byte>();
char[] tmp = source.ToCharArray();
int remain = tmp.Length % 4;
if (remain != 0)
{
Array.Resize(ref tmp, tmp.Length - remain);
}
int patch = source.IndexOf(m_pad);
if (patch != -1)
{
patch = source.Length - patch;
}
int cnt = tmp.Length / 4;
for (int i = 0; i < cnt; i++)
{
this.DecodeUnit(list, tmp[i * 4], tmp[i * 4 + 1], tmp[i * 4 + 2], tmp[i * 4 + 3]);
}
for (int i = 0; i < patch; i++)
{
list.RemoveAt(list.Count - 1);
}
return System.Text.Encoding.UTF8.GetString(list.ToArray());
}
}
protected void DecodeUnit(List<byte> byteArr, params char[] chArray)
{
int[] res = new int[3];
byte[] unit = new byte[chArray.Length];
for (int i = 0; i < chArray.Length; i++)
{
unit[i] = this.FindChar(chArray[i]);
}
res[0] = (unit[0] << 2) + ((unit[1] & 0x30) >> 4);
res[1] = ((unit[1] & 0xf) << 4) + ((unit[2] & 0x3c) >> 2);
res[2] = ((unit[2] & 0x3) << 6) + unit[3];
for (int i = 0; i < res.Length; i++)
{
byteArr.Add((byte)res[i]);
}
}
protected byte FindChar(char ch)
{
int pos = m_t2[ch];//m_codeTable.IndexOf(ch);
return (byte)pos;
}
/// <summary>
/// 初始化双向哈西字典
/// </summary>
protected void InitDict()
{
m_t1.Clear();
m_t2.Clear();
m_t2.Add(m_pad[0], -1);
for (int i = 0; i < m_codeTable.Length; i++)
{
m_t1.Add(i, m_codeTable[i]);
m_t2.Add(m_codeTable[i], i);
}
}
/// <summary>
/// 检查字符串中的字符是否有重复
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
protected void ValidateRepeat(string input)
{
for (int i = 0; i < input.Length; i++)
{
if (input.LastIndexOf(input[i]) > i)
{
throw new Exception("密码表中含有重复字符:" + input[i]);
}
}
}
/// <summary>
/// 检查字符串是否包含补码字符
/// </summary>
/// <param name="input"></param>
/// <param name="pad"></param>
protected void ValidateEqualPad(string input, string pad)
{
if (input.IndexOf(pad) > -1)
{
throw new Exception("密码表中包含了补码字符:" + pad);
}
}
}