///
/// 對(duì)字符串進(jìn)行SHA1加密
///
/// 需要加密的字符串
/// 密文
public string SHA1_Encrypt(string Source_String)
{
byte[] StrRes = Encoding.Default.GetBytes(Source_String);
HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
StrRes = iSHA.ComputeHash(StrRes);
StringBuilder EnText = new StringBuilder();
foreach (byte iByte in StrRes)
{
EnText.AppendFormat("{0:x2}", iByte);
}
return EnText.ToString();
}
///
/// SHA256加密,不可逆轉(zhuǎn)
///
/// string str:被加密的字符串
/// 返回加密后的字符串
private string SHA256Encrypt(string str)
{
System.Security.Cryptography.SHA256 s256 = new System.Security.Cryptography.SHA256Managed();
byte[] byte1;
byte1 = s256.ComputeHash(Encoding.Default.GetBytes(str));
s256.Clear();
return Convert.ToBase64String(byte1);
}
///
/// SHA384加密,不可逆轉(zhuǎn)
///
/// string str:被加密的字符串
/// 返回加密后的字符串
private string SHA384Encrypt(string str)
{
System.Security.Cryptography.SHA384 s384 = new System.Security.Cryptography.SHA384Managed();
byte[] byte1;
byte1 = s384.ComputeHash(Encoding.Default.GetBytes(str));
s384.Clear();
return Convert.ToBase64String(byte1);
}
///
/// SHA512加密,不可逆轉(zhuǎn)
///
/// string str:被加密的字符串
/// 返回加密后的字符串
private string SHA512Encrypt(string str)
{
System.Security.Cryptography.SHA512 s512 = new System.Security.Cryptography.SHA512Managed();
byte[] byte1;
byte1 = s512.ComputeHash(Encoding.Default.GetBytes(str));
s512.Clear();
return Convert.ToBase64String(byte1);
}
//默認(rèn)密鑰向量
private byte[] Keys = { 0xEF, 0xAB, 0x56, 0x78, 0x90, 0x34, 0xCD, 0x12 };
///
/// DES加密字符串
///
/// 待加密的字符串
/// 加密密鑰,要求為8位
/// 加密成功返回加密后的字符串,失敗返回源串
public string EncryptDES(string encryptString, string encryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
byte[] rgbIV = Keys;
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;
}
}
///
/// DES解密字符串
///
/// 待解密的字符串
/// 解密密鑰,要求為8位,和加密密鑰相同
/// 解密成功返回解密后的字符串,失敗返源串
public string DecryptDES(string decryptString, string decryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
byte[] rgbIV = Keys;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
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());
}
catch
{
return decryptString;
}
}