MD5的加密处理应用还是比较多,由于破解难度很大,基本上大型网站或者软件商,密码加密一般采用这种方式居多。
而MD5可以用来获得32、 16、8等全部部分内容加密内容,也可以获取其加密后的哈希值。
-
/// 获得32位的MD5加密
-
///
-
///
-
///
-
public static string GetMD5_32(string input)
-
{
-
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
-
byte[] data = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(input));
-
StringBuilder sb = new StringBuilder();
-
for (int i = 0; i < data.Length; i++)
-
{
-
sb.Append(data[i].ToString("x2"));
-
}
-
return sb.ToString();
-
}
-
-
///
-
/// 获得16位的MD5加密
-
///
-
///
-
///
-
public static string GetMD5_16(string input)
-
{
-
return GetMD5_32(input).Substring(8, 16);
-
}
-
///
-
/// 获得8位的MD5加密
-
///
-
///
-
///
-
public static string GetMD5_8(string input)
-
{
-
return GetMD5_32(input).Substring(8, 8);
-
}
-
///
-
/// 获得4位的MD5加密
-
///
-
///
-
///
-
public static string GetMD5_4(string input)
-
{
-
return GetMD5_32(input).Substring(8, 4);
-
}
-
-
public static string MD5EncryptHash(String input)
-
{
-
MD5 md5 = new MD5CryptoServiceProvider();
-
//the GetBytes method returns byte array equavalent of a string
-
byte[] res = md5.ComputeHash(Encoding.Default.GetBytes(input), 0, input.Length);
-
char[] temp = new char[res.Length];
-
//copy to a char array which can be passed to a String constructor
-
Array.Copy(res, temp, res.Length);
-
//return the result as a string
-
return new String(temp);
-
}
对文件添加MD5标签及验证
这种方式比较有趣,我也是最近才发现其中的奥妙,其实我们为了防止文件被修改,可以采用这种方式预先添加MD5码,然后在程序代码中进行验证,这样至少可以减少部分篡改的行为吧,因为只要文件有些少的修改,Md5码将会发生变化的,呵呵。
-
///
-
/// 对给定文件路径的文件加上标签
-
///
-
/// 要加密的文件的路径
-
/// 标签的值
-
public static bool AddMD5(string path)
-
{
-
bool IsNeed = true;
-
-
if (CheckMD5(path)) //已进行MD5处理
-
IsNeed = false;
-
-
try
-
{
-
FileStream fsread = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
-
byte[] md5File = new byte[fsread.Length];
-
fsread.Read(md5File, 0, (int)fsread.Length); // 将文件流读取到Buffer中
-
fsread.Close();
-
-
if (IsNeed)
-
{
-
string result = MD5Buffer(md5File, 0, md5File.Length); // 对Buffer中的字节内容算MD5
-
byte[] md5 = System.Text.Encoding.ASCII.GetBytes(result); // 将字符串转换成字节数组以便写人到文件中
-
FileStream fsWrite = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
-
fsWrite.Write(md5File, 0, md5File.Length); // 将文件,MD5值 重新写入到文件中。
-
fsWrite.Write(md5, 0, md5.Length);
-
fsWrite.Close();
-
}
-
else
-
{
-
FileStream fsWrite = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
-
fsWrite.Write(md5File, 0, md5File.Length);
-
fsWrite.Close();
-
}
-
}
-
catch
-
{
-
return false;
-
}
-
-
return true;
-
}
-
-
///
-
/// 对给定路径的文件进行验证
-
///
-
///
-
/// 是否加了标签或是否标签值与内容值一致
-
public static bool CheckMD5(string path)
-
{
-
try
-
{
-
FileStream get_file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
-
byte[] md5File = new byte[get_file.Length]; // 读入文件
-
get_file.Read(md5File, 0, (int)get_file.Length);
-
get_file.Close();
-
-
string result = MD5Buffer(md5File, 0, md5File.Length - 32); // 对文件除最后32位以外的字节计算MD5,这个32是因为标签位为32位。
-
string md5 = System.Text.Encoding.ASCII.GetString(md5File, md5File.Length - 32, 32); //读取文件最后32位,其中保存的就是MD5值
-
return result == md5;
-
}
-
catch
-
{
-
return false;
-
}
-
}
-
-
///
-
/// 计算文件的MD5值
-
///
-
/// MD5签名文件字符数组
-
/// 计算起始位置
-
/// 计算终止位置
-
/// 计算结果
-
private static string MD5Buffer(byte[] MD5File, int index, int count)
-
{
-
System.Security.Cryptography.MD5CryptoServiceProvider get_md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
-
byte[] hash_byte = get_md5.ComputeHash(MD5File, index, count);
-
string result = System.BitConverter.ToString(hash_byte);
-
-
result = result.Replace("-", "");
-
return result;
-
}
-
#endregion
SHA256加密算法
-
/// SHA256函数
-
///
-
/// 原始字符串
-
/// SHA256结果(返回长度为44字节的字符串)
-
public static string SHA256(string str)
-
{
-
byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
-
SHA256Managed Sha256 = new SHA256Managed();
-
byte[] Result = Sha256.ComputeHash(SHA256Data);
-
return Convert.ToBase64String(Result); //返回长度为44字节的字符串
-
}
以下是应用在开发中的实例:
-
using System;
-
using System.Collections.Generic;
-
using System.Configuration;
-
using System.Data;
-
using System.Data.SqlClient;
-
using System.Linq;
-
using System.Security.Cryptography;
-
using System.Text;
-
-
namespace MIS
-
{
-
class Encrypt
-
{
-
-
//数据存入,直接将byte[]保存入binary字段
-
public static int AccountRegister(string accountName, string password,string firstname,string position,string phone)
-
{
-
//string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
-
string connstr = configxml.xmlconn.readxml();
-
SqlConnection conn = new SqlConnection(connstr);
-
string bytePassword = Encrypt.GetMD5_32(password);//使用32位MD5加密
-
conn.Open();
-
-
SqlCommand cmd = new SqlCommand("Account_Add", conn);
-
cmd.CommandType = CommandType.StoredProcedure;
-
SqlParameter prmAccountName = cmd.Parameters.Add(new SqlParameter("@accountName", SqlDbType.NVarChar, 50));
-
prmAccountName.Value = accountName;
-
SqlParameter prmPassword = cmd.Parameters.Add(new SqlParameter("@password", SqlDbType.NVarChar, 50));
-
prmPassword.Value = bytePassword;
-
SqlParameter prmFirstName = cmd.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.NVarChar, 50));
-
prmFirstName.Value = firstname;
-
SqlParameter prmPosition = cmd.Parameters.Add(new SqlParameter("@position", SqlDbType.NVarChar, 50));
-
prmPosition.Value = position;
-
SqlParameter prmPhone = cmd.Parameters.Add(new SqlParameter("@phone", SqlDbType.NVarChar, 50));
-
prmPhone.Value = phone;
-
int myInt = cmd.ExecuteNonQuery();
-
cmd.Dispose();
-
conn.Close();
-
return myInt;
-
}
-
//更新账户信息
-
public static int AccountUpdate(string accountName, string password)
-
{
-
string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
-
string bytePassword = Encrypt.GetMD5_32(password);
-
SqlConnection conn = new SqlConnection(connstr);
-
conn.Open();
-
SqlCommand cmd = new SqlCommand("Account_Update", conn);
-
cmd.CommandType = CommandType.StoredProcedure;
-
SqlParameter prmAccountName = cmd.Parameters.Add(new SqlParameter("@accountName", SqlDbType.NVarChar, 50));
-
prmAccountName.Value = accountName;
-
SqlParameter prmPassword = cmd.Parameters.Add(new SqlParameter("@password", SqlDbType.NVarChar, 50));
-
prmPassword.Value = bytePassword;
-
int myInt = cmd.ExecuteNonQuery();
-
cmd.Dispose();
-
conn.Close();
-
return myInt;
-
}
-
/// SHA256函数
-
///
-
/// 原始字符串
-
/// SHA256结果(返回长度为44字节的字符串)
-
///
-
//哈系散列转换
-
public static byte[] GetSaltedPassword(string password)
-
{
-
SHA1 sha1 = SHA1.Create();
-
//应用System.Text空间下的Unicode.GetBytes方法获得byte.
-
byte[] bytePassword = sha1.ComputeHash(Encoding.Unicode.GetBytes(password));
-
return bytePassword;
-
}
-
public static string SHA256(string str)
-
{
-
byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
-
SHA256Managed Sha256 = new SHA256Managed();
-
byte[] Result = Sha256.ComputeHash(SHA256Data);
-
return Convert.ToBase64String(Result); //返回长度为44字节的字符串
-
}
-
/// 获得32位的MD5加密
-
///
-
///
-
///
-
public static string GetMD5_32(string input)
-
{
-
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
-
byte[] data = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(input));
-
StringBuilder sb = new StringBuilder();
-
for (int i = 0; i < data.Length; i++)
-
{
-
sb.Append(data[i].ToString("x2"));
-
}
-
return sb.ToString();
-
}
-
///
-
/// 获得16位的MD5加密
-
///
-
///
-
///
-
public static string GetMD5_16(string input)
-
{
-
return GetMD5_32(input).Substring(8, 16);
-
}
-
///
-
/// 获得8位的MD5加密
-
///
-
///
-
///
-
public static string GetMD5_8(string input)
-
{
-
return GetMD5_32(input).Substring(8, 8);
-
}
-
///
-
/// 获得4位的MD5加密
-
///
-
///
-
///
-
public static string GetMD5_4(string input)
-
{
-
return GetMD5_32(input).Substring(8, 4);
-
}
-
-
public static string MD5EncryptHash(String input)
-
{
-
MD5 md5 = new MD5CryptoServiceProvider();
-
//the GetBytes method returns byte array equavalent of a string
-
byte[] res = md5.ComputeHash(Encoding.Default.GetBytes(input), 0, input.Length);
-
char[] temp = new char[res.Length];
-
//copy to a char array which can be passed to a String constructor
-
Array.Copy(res, temp, res.Length);
-
//return the result as a string
-
return new String(temp);
-
}
-
}
-
}
阅读(4127) | 评论(0) | 转发(0) |