提供一个加密的帮助类HashProvider

maiker 2010-01-29 04:08:04
提供一个加密的帮助类HashProvider 急用
...全文
191 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
polarissky 2010-01-30
  • 打赏
  • 举报
回复

楼主不厚道,倒分啊
maiker110 2010-01-30
  • 打赏
  • 举报
回复
public static class HashProvider
{
public enum HashType
{
MD5,
SHA1,
SHA256,
SHA384,
SHA512
}

public static string Hash(string str)
{
return Hash(str, HashType.MD5);
}

public static string Hash(string str, HashType type)
{
HashAlgorithm provider = GetHashProvider(type);
byte[] oldby = Encoding.Default.GetBytes(str);

byte[] newby = provider.ComputeHash(oldby);

StringBuilder bulider = new StringBuilder();
for (int i = 0; i < newby.Length; i++)
{
bulider.AppendFormat("{0:X2}", newby[i]);
}
return bulider.ToString();
}

private static HashAlgorithm GetHashProvider(HashType type)
{
HashAlgorithm provider = null;
switch (type)
{
case HashType.MD5:
provider = MD5.Create();
break;
case HashType.SHA1:
provider = SHA1.Create();
break;
case HashType.SHA256:
provider = SHA256.Create();
break;
case HashType.SHA384:
provider = SHA384.Create();
break;
case HashType.SHA512:
provider = SHA512.Create();
break;
default:
provider = MD5.Create();
break;
}
return provider;
}
}
wuyq11 2010-01-29
  • 打赏
  • 举报
回复
public string GetMD5_32(string s, string _input_charset)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s));
StringBuilder sb = new StringBuilder(32);
for (int i = 0; i < t.Length; i++)
{
sb.Append(t[i].ToString("x").PadLeft(2, '0'));
}
return sb.ToString();
}
public string SHA1Encrypt(string strIN)
{
byte[] tmpByte;
SHA1 sha1 = new SHA1CryptoServiceProvider();
tmpByte = sha1.ComputeHash(GetKeyByteArray(strIN));
sha1.Clear();
return GetStringValue(tmpByte);

}

public string SHA256Encrypt(string strIN)
{
byte[] tmpByte;
SHA256 sha256 = new SHA256Managed();
tmpByte = sha256.ComputeHash(GetKeyByteArray(strIN));
sha256.Clear();
return GetStringValue(tmpByte);
}
public string SHA512Encrypt(string strIN)
{
byte[] tmpByte;
SHA512 sha512 = new SHA512Managed();
tmpByte = sha512.ComputeHash(GetKeyByteArray(strIN));
sha512.Clear();
return GetStringValue(tmpByte);

}
xray2005 2010-01-29
  • 打赏
  • 举报
回复
.net 里面已经提供了MD5、SHA、等你 晓得你还想要什么HASH呢?

polarissky 2010-01-29
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace Lib
{
/// <summary>
/// 取得byte[]、string的哈希值
/// </summary>
public class Hash
{
/// <summary>
/// MD5(32bit)
/// </summary>
/// <param name="Input">Input</param>
public static string MD5(byte[] Input)
{
MD5 md5 = new MD5CryptoServiceProvider();
return Util.BytesToHexString(md5.ComputeHash(Input));
}
/// <summary>
/// MD5(32bit)
/// </summary>
/// <param name="Input">Input</param>
public static string MD5(string Input)
{
return MD5(Util.StringToBytes(Input));
}

/// <summary>
/// SHA1(160bit)
/// </summary>
/// <param name="Input">Input</param>
public static string SHA1(byte[] Input)
{
SHA1 sha1 = new SHA1Managed();
return Util.BytesToHexString(sha1.ComputeHash(Input));
}
/// <summary>
/// SHA1(160bit)
/// </summary>
/// <param name="Input">Input</param>
public static string SHA1(string Input)
{
return SHA1(Util.StringToBytes(Input));
}

/// <summary>
/// SHA256(256bit)
/// </summary>
/// <param name="Input">Input</param>
public static string SHA256(byte[] Input)
{
SHA256 sha256 = new SHA256Managed();
return Util.BytesToHexString(sha256.ComputeHash(Input));
}
/// <summary>
/// SHA256(256bit)
/// </summary>
/// <param name="Input">Input</param>
public static string SHA256(string Input)
{
return SHA256(Util.StringToBytes(Input));
}

/// <summary>
/// SHA384(384bit)
/// </summary>
/// <param name="Input">Input</param>
public static string SHA384(byte[] Input)
{
SHA384 sha384 = new SHA384Managed();
return Util.BytesToHexString(sha384.ComputeHash(Input));
}
/// <summary>
/// SHA384(384bit)
/// </summary>
/// <param name="Input">Input</param>
public static string SHA384(string Input)
{
return SHA384(Util.StringToBytes(Input));
}

/// <summary>
/// SHA512(512bit)
/// </summary>
/// <param name="Input">Input</param>
public static string SHA512(byte[] Input)
{
SHA512 sha512 = new SHA512Managed();
return Util.BytesToHexString(sha512.ComputeHash(Input));
}
/// <summary>
/// SHA512(512bit)
/// </summary>
/// <param name="Input">Input</param>
public static string SHA512(string Input)
{
return SHA512(Util.StringToBytes(Input));
}
}
}



using System;
using System.Collections.Generic;
using System.Text;

namespace Lib
{
/// <summary>
/// string与byte[]转换
/// </summary>
public class Util
{
/// <summary>
/// 根据缺省字符编码方案将string转换成byte[]
/// </summary>
/// <param name="s">目标字符串</param>
/// <returns>编码后的字节数组</returns>
public static byte[] StringToBytes(string s)
{
return System.Text.Encoding.Default.GetBytes(s);
}
/// <summary>
/// 根据自定义字符编码方案将string转换成byte[]
/// </summary>
/// <param name="s">目标字符串</param>
/// <param name="encoding">自定义字符编码方案</param>
/// <returns>编码后的字节数组</returns>
public static byte[] StringToBytes(string s, Encoding encoding)
{
return encoding.GetBytes(s);
}

/// <summary>
/// 根据缺省字符编码方案将byte[]转换成string
/// </summary>
/// <param name="b">目标字节数组</param>
/// <returns>编码后的字符串</returns>
public static string BytesToString(byte[] b)
{
return System.Text.Encoding.Default.GetString(b);
}
/// <summary>
/// 根据自定义字符编码方案将byte[]转换成string
/// </summary>
/// <param name="b">目标字节数组</param>
/// <param name="encoding">字符编码方案</param>
/// <returns>编码后的字符串</returns>
public static string BytesToString(byte[] b, Encoding encoding)
{
return encoding.GetString(b);
}

/// <summary>
/// 将字节数组转换成十六进制string
/// </summary>
/// <param name="b">目标字节数组</param>
/// <returns>转换后的字符串</returns>
public static string BytesToHexString(byte[] b)
{
if (b == null)
return null;

StringBuilder sb = new StringBuilder(b.Length * 2);

for (int i = 0; i < b.Length; i++)
{
sb.AppendFormat("{0:x2}", b[i]);
}

return sb.ToString();
}

/// <summary>
/// 将十六进制string转换成字节数组
/// </summary>
/// <param name="s">目标字符串</param>
/// <returns>转换后的字节数组</returns>
public static byte[] HexStringToBytes(string s)
{
if (s == null) return null;

int nLen = s.Length;
if ((nLen % 2) != 0) // 如果长度为奇数,则忽略最后一个十六位字符
{
nLen--;
}

byte[] buffer = new byte[nLen / 2];

for (int i = 0; i < nLen; i += 2)
{
buffer[i / 2] = Convert.ToByte(s.Substring(i, 2), 16);
}

return buffer;
}
}
}
kaukiyou 2010-01-29
  • 打赏
  • 举报
回复
楼主已经说了, 要用HASH, 其他算法就别贴了吧。
MD5不安全, 最安全的就是SHA512了。
ssy888 2010-01-29
  • 打赏
  • 举报
回复
往数据库里写密码加密用EncryptDBData,解密用DecryptDBData

public class Encryption
{
private static TripleDESCryptoServiceProvider TripleDes = new TripleDESCryptoServiceProvider();

private static byte[] TruncateHash(string key, int length)
{

SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();

// Hash the key.
byte[] keyBytes = System.Text.Encoding.Unicode.GetBytes(key);
byte[] hash = sha1.ComputeHash(keyBytes);

// Truncate or pad the hash.
Array.Resize(ref hash, length);
return hash;
}

private static string EncryptionKey()
{
string currentDateTime = null;
string key = null;
// Initialize the crypto provider.

currentDateTime = DateTime.Now.Year.ToString();
currentDateTime += DateTime.Now.Month.ToString();
currentDateTime += DateTime.Now.Day.ToString();
key = "QDer4g8" + currentDateTime;

TripleDes.Key = TruncateHash(key, TripleDes.KeySize / 8);
TripleDes.IV = TruncateHash("", TripleDes.BlockSize / 8);


return key;
}

private static string DBEncryptionKey()
{
string key = null;
// Initialize the crypto provider.
key = "QDer4f78";

TripleDes.Key = TruncateHash(key, TripleDes.KeySize / 8);
TripleDes.IV = TruncateHash("", TripleDes.BlockSize / 8);


return key;
}

public static string EncryptData(string plaintext)
{

//initialize the encryption key
EncryptionKey();
// Convert the plaintext string to a byte array.
byte[] plaintextBytes = System.Text.Encoding.Unicode.GetBytes(plaintext);

// Create the stream.
System.IO.MemoryStream ms = new System.IO.MemoryStream();
// Create the encoder to write to the stream.
CryptoStream encStream = new CryptoStream(ms, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);

// Use the crypto stream to write the byte array to the stream.
encStream.Write(plaintextBytes, 0, plaintextBytes.Length);
encStream.FlushFinalBlock();

// Convert the encrypted stream to a printable string.
return Convert.ToBase64String(ms.ToArray());
}

public static string EncryptDBData(string plaintext)
{

//initialize the encryption key
DBEncryptionKey();
// Convert the plaintext string to a byte array.
byte[] plaintextBytes = System.Text.Encoding.Unicode.GetBytes(plaintext);

// Create the stream.
System.IO.MemoryStream ms = new System.IO.MemoryStream();
// Create the encoder to write to the stream.
CryptoStream encStream = new CryptoStream(ms, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);

// Use the crypto stream to write the byte array to the stream.
encStream.Write(plaintextBytes, 0, plaintextBytes.Length);
encStream.FlushFinalBlock();

// Convert the encrypted stream to a printable string.
return Convert.ToBase64String(ms.ToArray());
}

public static string DecryptData(string encryptedtext)
{

//initialize the encryption key
EncryptionKey();
// Convert the encrypted text string to a byte array.
byte[] encryptedBytes = Convert.FromBase64String(encryptedtext);

// Create the stream.
System.IO.MemoryStream ms = new System.IO.MemoryStream();
// Create the decoder to write to the stream.
CryptoStream decStream = new CryptoStream(ms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);

// Use the crypto stream to write the byte array to the stream.
decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
decStream.FlushFinalBlock();

// Convert the plaintext stream to a string.
return System.Text.Encoding.Unicode.GetString(ms.ToArray());
}

public static string DecryptDBData(string encryptedtext)
{

try
{
//initialize the encryption key
DBEncryptionKey();
// Convert the encrypted text string to a byte array.
byte[] encryptedBytes = Convert.FromBase64String(encryptedtext);

// Create the stream.
System.IO.MemoryStream ms = new System.IO.MemoryStream();
// Create the decoder to write to the stream.
CryptoStream decStream = new CryptoStream(ms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);

// Use the crypto stream to write the byte array to the stream.
decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
decStream.FlushFinalBlock();

// Convert the plaintext stream to a string.
return System.Text.Encoding.Unicode.GetString(ms.ToArray());
}
catch (Exception ex)
{
throw new Exception("Error in Encryption.DecryptDBData");
}
}
}
绝代坏坏 2010-01-29
  • 打赏
  • 举报
回复
用3ds或者md5就成了。
kaukiyou 2010-01-29
  • 打赏
  • 举报
回复
salt自己随机个字符串出来。

public static string HashPassword(string password)
{
SHA512 sha512 = new SHA512Managed();
byte[] bytes = Encoding.UTF8.GetBytes(password);
byte[] src = Convert.FromBase64String(salt);
byte[] dst = new byte[bytes.Length + src.Length];
Buffer.BlockCopy(src, 0, dst, 0, src.Length);
Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);
byte[] hashedData = sha512.ComputeHash(dst);
sha512.Clear();
return BitConverter.ToString(hashedData).Replace("-", string.Empty);
}

62,254

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧