基础类库积累--Encrypt类
前言:
我们在数据传输的过程中,肯定不能使用明文传输,这样太容易被人截获信息,一般会对传输的数据进行加密操作。
/// <summary>
/// DES加密
/// </summary>
/// <param name="value">需要加密的字符串(明文)</param>
/// <returns>加密之后的字符串(密文)</returns>
public static string DesEncrypt(string value)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(value);
string key = "s1u&n*dy";
des.Key = ASCIIEncoding.ASCII.GetBytes(key);
des.IV = ASCIIEncoding.ASCII.GetBytes(key);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
/// <summary>
/// MD5加密
/// </summary>
/// <param name="value">需要加密的字符串(明文)</param>
/// <param name="value">密钥</param>
/// <returns>加密之后的字符串(密文)</returns>
public static string Md5Encrypt(string value ,string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(value);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
/// <summary>
/// Base64加密
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public static string Base64Encrypt(Encoding encode,string msg)
{
byte[] encbuff = encode.GetBytes(msg);
return Convert.ToBase64String(encbuff);
}
/// <summary>
/// AES加密(无向量)
/// </summary>
/// <param name="plainBytes">被加密的明文</param>
/// <param name="key">密钥</param>
/// <returns>密文</returns>
public static string AESEncrypt(String Data, String Key)
{
MemoryStream mStream = new MemoryStream();
RijndaelManaged aes = new RijndaelManaged();
byte[] plainBytes = Encoding.UTF8.GetBytes(Data);
Byte[] bKey = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
aes.KeySize = 128;
//aes.Key = _key;
aes.Key = bKey;
//aes.IV = _iV;
CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
try
{
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
cryptoStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
finally
{
cryptoStream.Close();
mStream.Close();
aes.Clear();
}
}
/// <summary>
/// RSA产生密钥
/// </summary>
/// <param name="xmlKeys">私钥</param>
/// <param name="xmlPublicKey">公钥</param>
public static void RSAKey(out string xmlKeys, out string xmlPublicKey)
{
try
{
System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
xmlKeys = rsa.ToXmlString(true);
xmlPublicKey = rsa.ToXmlString(false);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// RSA加密
/// </summary>
/// <param name="publickey"></param>
/// <param name="content"></param>
/// <returns></returns>
public static string RSAEncrypt(string publickey, string content)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.FromXmlString(publickey);
cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
return Convert.ToBase64String(cipherbytes);
}
/// <summary>SHA256加密
/// </summary>
/// <param name="publicKey">明文</param>
/// <returns>密文字符串</returns>
public static string SHA256(string str)
{
byte[] buffer = Encoding.UTF8.GetBytes(str);
SHA256CryptoServiceProvider SHA256 = new SHA256CryptoServiceProvider();
byte[] byteArr = SHA256.ComputeHash(buffer);
return BitConverter.ToString(byteArr);
}
/// <summary>SHA128加密【20字节,160位】
/// </summary>
/// <param name="publicKey">明文</param>
/// <returns>密文字符串</returns>
public static string SHA128(string str)
{
byte[] buffer = Encoding.UTF8.GetBytes(str);
SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider();
byte[] byteArr = SHA1.ComputeHash(buffer);
return BitConverter.ToString(byteArr);
}
/// <summary>SHA384加密【48字节,384位】
/// </summary>
/// <param name="publicKey">明文</param>
/// <returns>密文字符串</returns>
public static string SHA384(string str)
{
byte[] buffer = Encoding.UTF8.GetBytes(str);
SHA384CryptoServiceProvider SHA384 = new SHA384CryptoServiceProvider();
byte[] byteArr = SHA384.ComputeHash(buffer);
return BitConverter.ToString(byteArr);
}
/// <summary>SHA512加密【64字节,512位】
/// </summary>
/// <param name="publicKey">明文</param>
/// <returns>密文字符串</returns>
public static string SHA512(string str)
{
byte[] buffer = Encoding.UTF8.GetBytes(str);
SHA512CryptoServiceProvider SHA512 = new SHA512CryptoServiceProvider();
byte[] byteArr = SHA512.ComputeHash(buffer);
return BitConverter.ToString(byteArr);
}
简介:
- 加密的方式有很多:MD5,DES,AES,RSA,SHA128,SHA256,SHA384,SHA512,Base64等等
- 加密的简单介绍:
- MD5:对字符或密码进行的16位或32位加密【不可逆的加密】
- AES:采用对称分组密码体制,密钥长度可以为128、192或256位,分组长度128位【对称加密】
- DES:明文长度为1-7之间,则密文长度为8;明文长度为8-15之间时,加密后的密文长度为16,一般做金融数据加密【对称加密】
- RSA:明文只有117字节,公钥加密算法【非对称加密】,你需要先用RSAKey生成一个公钥和一个私钥,加密的使用公钥,解密使用私钥
- SHA128,SHA256,SHA384:安全哈希算法(Secure Hash Algorithm)【非对称加密】
- Base64:从二进制到字符的过程,可用于在HTTP环境下传递较长的标识信息,保密电子邮件密码
- 对称加密:对称加密指的就是加密和解密使用同一个秘钥,所以叫做对称加密。对称加密只有一个秘钥,作为私钥。 常见的对称加密算法:DES,AES,3DES等等。
- 非对称加密:非对称加密指的是:加密和解密使用不同的秘钥,一把作为公开的公钥,另一把作为私钥。公钥加密的信息,只有私钥才能解密。私钥加密的信息,只有公钥才能解密。
- 对称加密和非对称的区别:对称加密算法相比非对称加密算法来说,加解密的效率要高得多。但是缺陷在于对于秘钥的管理上,以及在非安全信道中通讯时,密钥交换的安全性不能保障。所以在实际的网络环境中,会将两者混合使用.
参考:
C#加密解密(DES,AES,Base64,md5,SHA256,RSA,RC4)
加密原理介绍,代码实现DES、AES、RSA、Base64、MD5
总结:
在百度的海洋中,了解RC4加密不安全,就没有贴有关的代码。其他加密原理有些还不是很清楚,博客园里面有不少讲原理,我慢慢看,这篇主要是做积累,以后如果用到需要加密的需求,而我是有的选择,而不至于一脸懵逼。