DES,MD5,SHA1等加密
private byte[] Keys = { 0xEF, 0xAB, 0x56, 0x78, 0x90, 0x34, 0xCD, 0x12 };
/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
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;
}
}
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;
}
}
加密方法
private byte[] Keys = { 0xEF, 0xAB, 0x56, 0x78, 0x90, 0x34, 0xCD, 0x12 };
/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
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;
}
}
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;
}
}
加密方法
解决方案 »
- vs调试的时候有时候报错System.Data.SqlClient.SqlException: 拒绝了对对象 'sp_sdidebug'这个怎么会是啊?怎么而解决?
- 在后台调用前台脚本该怎么做?
- 请教高手:有关ADO.NET中sql表达式的问题
- 关于datalist的样式问题
- 正则里面的[星号]*和+[加号]的区别是什么?
- datagridview的列宽能不能按照百分比设置?要动手设置类库,有没什么好的办法
- 散分,征求想法
- 请帮忙,调用windows 窗体
- 怎样在DataColumn.Expression表达式中使用四舍五入,或者只舍不入,或者只入不舍的函数
- 关于反射的一个问题
- 请求看看 为什么提示我 当前不存在名称 TextBox1
- winform跨线程问题
楼主可以去下载
/// 传入明文,返回用MD%加密后的字符串
/// </summary>
/// <param name="str">要加密的字符串</param>
/// <returns>用MD5加密后的字符串</returns>
public static string ToMD5(string str)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5");
}
/// <summary>
/// 截取字符串。
/// </summary>
/// <param name="str">要接取得字符串</param>
/// <param name="number">保留的字节数。按半角计算</param>
/// <returns>指定长度的字符串</returns>
public static string StringCal(string str, int number)
{
Byte[] tempStr = System.Text.Encoding.Default.GetBytes(str);
if (tempStr.Length > number)
{
return System.Text.Encoding.Default.GetString(tempStr, 0, number - 2) + "..";
}
else
return str;
} #region 加密,固定密钥
/// <summary>
/// 加密
/// </summary>
/// <param name="sourceData">原文</param>
/// <returns></returns>
public static string Encrypt(string sourceData)
{
//set key and initialization vector values
//Byte[] key = new byte[] {0x21, 2, 0x88, 4, 5, 0x56, 7, 0x99};
//Byte[] iv = new byte[] {0x21, 2, 0x88, 4, 5, 0x56, 7, 0x99};
Byte[] key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
Byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
try
{
//convert data to byte array
Byte[] sourceDataBytes = System.Text.ASCIIEncoding.UTF8.GetBytes(sourceData);
//get target memory stream
MemoryStream tempStream = new MemoryStream();
//get encryptor and encryption stream
DESCryptoServiceProvider encryptor = new DESCryptoServiceProvider();
CryptoStream encryptionStream = new CryptoStream(tempStream, encryptor.CreateEncryptor(key, iv), CryptoStreamMode.Write); //encrypt data
encryptionStream.Write(sourceDataBytes, 0, sourceDataBytes.Length);
encryptionStream.FlushFinalBlock(); //put data into byte array
Byte[] encryptedDataBytes = tempStream.GetBuffer();
//convert encrypted data into string
return System.Convert.ToBase64String(encryptedDataBytes, 0, (int)tempStream.Length);
}
catch (Exception ex)
{
throw (ex);
}
}
#endregion #region 加密,可以设置密钥
/// <summary>
/// 可以设置密钥的加密方式
/// </summary>
/// <param name="sourceData">原文</param>
/// <param name="key">密钥,8位数字,字符串方式</param>
/// <returns></returns>
public static string Encrypt(string sourceData, string key)
{
//set key and initialization vector values
//Byte[] key = new byte[] {0x21, 2, 0x88, 4, 5, 0x56, 7, 0x99};
//Byte[] iv = new byte[] {0x21, 2, 0x88, 4, 5, 0x56, 7, 0x99}; #region 检查密钥是否符合规定
if (key.Length > 8)
{
key = key.Substring(0, 8);
}
#endregion char[] tmp = key.ToCharArray();
Byte[] keys = new byte[8];
//Byte[] keys = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
Byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; //设置密钥
for (int i = 0; i < 8; i++)
{
if (tmp.Length > i)
{
keys[i] = (byte)tmp[i];
}
else
{
keys[i] = (byte)i;
}
} try
{
//convert data to byte array
Byte[] sourceDataBytes = System.Text.ASCIIEncoding.UTF8.GetBytes(sourceData);
//get target memory stream
MemoryStream tempStream = new MemoryStream();
//get encryptor and encryption stream
DESCryptoServiceProvider encryptor = new DESCryptoServiceProvider();
CryptoStream encryptionStream = new CryptoStream(tempStream, encryptor.CreateEncryptor(keys, iv), CryptoStreamMode.Write); //encrypt data
encryptionStream.Write(sourceDataBytes, 0, sourceDataBytes.Length);
encryptionStream.FlushFinalBlock(); //put data into byte array
Byte[] encryptedDataBytes = tempStream.GetBuffer();
//convert encrypted data into string
return System.Convert.ToBase64String(encryptedDataBytes, 0, (int)tempStream.Length);
}
catch (Exception ex)
{
throw (ex);
}
}
#endregion #region 解密,固定密钥
/// <summary>
/// 解密
/// </summary>
/// <param name="sourceData">密文</param>
/// <returns></returns>
public static string Decrypt(string sourceData)
{
//set key and initialization vector values
//Byte[] key = new byte[] {0x21, 2, 0x88, 4, 5, 0x56, 7, 0x99};
//Byte[] iv = new byte[] {0x21, 2, 0x88, 4, 5, 0x56, 7, 0x99};
Byte[] keys = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
Byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
try
{
//convert data to byte array
Byte[] encryptedDataBytes = System.Convert.FromBase64String(sourceData);
//get source memory stream and fill it
MemoryStream tempStream = new MemoryStream(encryptedDataBytes, 0, encryptedDataBytes.Length);
//get decryptor and decryption stream
DESCryptoServiceProvider decryptor = new DESCryptoServiceProvider();
CryptoStream decryptionStream = new CryptoStream(tempStream, decryptor.CreateDecryptor(keys, iv), CryptoStreamMode.Read); //decrypt data
StreamReader allDataReader = new StreamReader(decryptionStream); return allDataReader.ReadToEnd();
}
catch (Exception ex)
{
throw (ex);
}
}
#endregion #region 解密,固定密钥
/// <summary>
/// 解密
/// </summary>
/// <param name="sourceData">密文</param>
/// <param name="key">密钥,8位数字,字符串方式</param>
/// <returns></returns>
public static string Decrypt(string sourceData, string key)
{
//set key and initialization vector values
//Byte[] key = new byte[] {0x21, 2, 0x88, 4, 5, 0x56, 7, 0x99};
//Byte[] iv = new byte[] {0x21, 2, 0x88, 4, 5, 0x56, 7, 0x99}; #region 检查密钥是否符合规定
if (key.Length > 8)
{
key = key.Substring(0, 8);
}
#endregion char[] tmp = key.ToCharArray();
Byte[] keys = new byte[8];
//Byte[] keys = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
Byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; //设置密钥
for (int i = 0; i < 8; i++)
{
if (tmp.Length > i)
{
keys[i] = (byte)tmp[i];
}
else
{
keys[i] = (byte)i;
}
}
try
{
//convert data to byte array
Byte[] encryptedDataBytes = System.Convert.FromBase64String(sourceData);
//get source memory stream and fill it
MemoryStream tempStream = new MemoryStream(encryptedDataBytes, 0, encryptedDataBytes.Length);
//get decryptor and decryption stream
DESCryptoServiceProvider decryptor = new DESCryptoServiceProvider();
CryptoStream decryptionStream = new CryptoStream(tempStream, decryptor.CreateDecryptor(keys, iv), CryptoStreamMode.Read); //decrypt data
StreamReader allDataReader = new StreamReader(decryptionStream); return allDataReader.ReadToEnd();
}
catch (Exception ex)
{
throw (ex);
}
}
#endregion
加密解密只有两种:对称和非对称MD5不属于他们其中的任何一种,所以不是加密算法,只是编码算法,这种编码过程是不可逆的,因为MD5之后的内容非常短(依赖算法本身)。至于C#里面提供了一套完整的对称和非对称加密算法的实现,当然你可以基于这个框架实现自己的加密算法,同样也提供了一组签名算法MD5之类的类库。所有的这些类都在System.Security.Cryptography命名空间下面。