C# Security 常用加密方法(针对string 的处理)
2008-02-28 14:59
561 查看
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace StringHandling
{
/// <summary>
/// String encrypt/decrypt.
/// </summary>
public class Security
{
/// <summary>
/// MD5 encrypt.
/// </summary>
/// <param name="passWord">String to encrypt</param>
/// <returns>String encrypted</returns>
public static string Md5(string passWord)
{
Byte[] clearBytes = new UnicodeEncoding().GetBytes(passWord);
Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
return BitConverter.ToString(hashedBytes).Replace("-", "");
}
/// <summary>
/// SHA1 encrypt.
/// </summary>
/// <param name="passWord">String to encrypt</param>
/// <returns>String encrypted</returns>
public static string Sha1(string passWord)
{
Byte[] clearBytes = new UnicodeEncoding().GetBytes(passWord);
Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("SHA1")).ComputeHash(clearBytes);
return BitConverter.ToString(hashedBytes);
}
/// <summary>
/// Rijndael(AES) Encrypt.
/// </summary>
/// <param name="source">Message to encrypt</param>
/// <param name="aesKey">Key</param>
/// <returns>Message encrypted</returns>
public static string AesEncrypt(string source, string aesKey)
{
byte[] bytIn = Encoding.UTF8.GetBytes(source);
//Bulid the Rijndael Key.
byte[] bKey = new byte[32];
Array.Copy(Encoding.UTF8.GetBytes(aesKey.PadRight(bKey.Length)), bKey, bKey.Length);
//Build the vector.
string aesIV = "KnXmC0KXxB3aQ4Hb";
byte[] bVector = new byte[16];
Array.Copy(Encoding.UTF8.GetBytes(aesIV.PadRight(bVector.Length)), bVector, bVector.Length);
//Create the Rijndael Object.
Rijndael aes = Rijndael.Create();
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream,
aes.CreateEncryptor(bKey, bVector),
CryptoStreamMode.Write))
{
cStream.Write(bytIn, 0, bytIn.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
}
}
/// <summary>
/// Rijndael(AES) Decrypt.
/// </summary>
/// <param name="source">Message to decrypt</param>
/// <param name="aesKey">Key</param>
/// <returns>Message decrypted</returns>
public static string AesDecrypt(string source, string aesKey)
{
byte[] bytIn = Convert.FromBase64String(source);
//Bulid the Rijndael Key.
byte[] bKey = new byte[32];
Array.Copy(Encoding.UTF8.GetBytes(aesKey.PadRight(bKey.Length)), bKey, bKey.Length);
//Build the vector.
string aesIV = "KnXmC0KXxB3aQ4Hb";
byte[] bVector = new byte[16];
Array.Copy(Encoding.UTF8.GetBytes(aesIV.PadRight(bVector.Length)), bVector, bVector.Length);
//Create the Rijndael Object.
Rijndael aes = Rijndael.Create();
using (MemoryStream mStraem = new MemoryStream(bytIn))
{
using (CryptoStream cStream = new CryptoStream(mStraem,
aes.CreateDecryptor(bKey, bVector),
CryptoStreamMode.Read))
{
using (StreamReader sReader = new StreamReader(cStream))
{
return sReader.ReadToEnd();
}
}
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace StringHandling
{
/// <summary>
/// String encrypt/decrypt.
/// </summary>
public class Security
{
/// <summary>
/// MD5 encrypt.
/// </summary>
/// <param name="passWord">String to encrypt</param>
/// <returns>String encrypted</returns>
public static string Md5(string passWord)
{
Byte[] clearBytes = new UnicodeEncoding().GetBytes(passWord);
Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
return BitConverter.ToString(hashedBytes).Replace("-", "");
}
/// <summary>
/// SHA1 encrypt.
/// </summary>
/// <param name="passWord">String to encrypt</param>
/// <returns>String encrypted</returns>
public static string Sha1(string passWord)
{
Byte[] clearBytes = new UnicodeEncoding().GetBytes(passWord);
Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("SHA1")).ComputeHash(clearBytes);
return BitConverter.ToString(hashedBytes);
}
/// <summary>
/// Rijndael(AES) Encrypt.
/// </summary>
/// <param name="source">Message to encrypt</param>
/// <param name="aesKey">Key</param>
/// <returns>Message encrypted</returns>
public static string AesEncrypt(string source, string aesKey)
{
byte[] bytIn = Encoding.UTF8.GetBytes(source);
//Bulid the Rijndael Key.
byte[] bKey = new byte[32];
Array.Copy(Encoding.UTF8.GetBytes(aesKey.PadRight(bKey.Length)), bKey, bKey.Length);
//Build the vector.
string aesIV = "KnXmC0KXxB3aQ4Hb";
byte[] bVector = new byte[16];
Array.Copy(Encoding.UTF8.GetBytes(aesIV.PadRight(bVector.Length)), bVector, bVector.Length);
//Create the Rijndael Object.
Rijndael aes = Rijndael.Create();
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream,
aes.CreateEncryptor(bKey, bVector),
CryptoStreamMode.Write))
{
cStream.Write(bytIn, 0, bytIn.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
}
}
/// <summary>
/// Rijndael(AES) Decrypt.
/// </summary>
/// <param name="source">Message to decrypt</param>
/// <param name="aesKey">Key</param>
/// <returns>Message decrypted</returns>
public static string AesDecrypt(string source, string aesKey)
{
byte[] bytIn = Convert.FromBase64String(source);
//Bulid the Rijndael Key.
byte[] bKey = new byte[32];
Array.Copy(Encoding.UTF8.GetBytes(aesKey.PadRight(bKey.Length)), bKey, bKey.Length);
//Build the vector.
string aesIV = "KnXmC0KXxB3aQ4Hb";
byte[] bVector = new byte[16];
Array.Copy(Encoding.UTF8.GetBytes(aesIV.PadRight(bVector.Length)), bVector, bVector.Length);
//Create the Rijndael Object.
Rijndael aes = Rijndael.Create();
using (MemoryStream mStraem = new MemoryStream(bytIn))
{
using (CryptoStream cStream = new CryptoStream(mStraem,
aes.CreateDecryptor(bKey, bVector),
CryptoStreamMode.Read))
{
using (StreamReader sReader = new StreamReader(cStream))
{
return sReader.ReadToEnd();
}
}
}
}
}
}
相关文章推荐
- C#编程技巧之常用文件名、路径处理方法(ZT)
- C#常用加密方法解析
- C# 常用字符串加密解密方法
- C#中关于String类型的常用方法
- C#编程技巧之常用文件名、路径处理方法
- String常用方法(处理api)
- C#中,常用的加密解密方法解析
- C#编程技巧之常用文件名、路径处理方法
- c#中处理字符串常用的函数及方法详细说明
- C#中常用的字符串处理方法
- 一些常用的String方法 C#
- c#常用的加密解密方法
- C#学习常用方法(2000)---String.Format方法
- C# 常用加密方法
- 解析Exception和C#处理Exception的常用方法总结
- string字符串常用处理方法
- C# 字符串String的常用使用方法
- c#中处理字符串常用的函数及方法详细说明
- 解析Exception和C#处理Exception的常用方法总结
- C#常用加密方法分析