当前位置:  编程技术>.net/c#/asp.net

C#加密算法 六种C#加密算法分享

    来源: 互联网  发布时间:2014-08-30

    本文导语:  C#实现加密算法的六种方法与实现代码,供大家参考。   方法一:   代码示例: //添加对System.Web的引用 using System.Web.Security; /// /// SHA1加密字符串 /// /// 源字符串 /// 加密后的字符串 public string SHA1(string source) { r...

C#实现加密算法的六种方法与实现代码,供大家参考。
 
方法一:
 

代码示例:
//添加对System.Web的引用
using System.Web.Security;
///
/// SHA1加密字符串
///
/// 源字符串
/// 加密后的字符串
public string SHA1(string source)
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(source, "SHA1");
}
///
/// MD5加密字符串
///
/// 源字符串
/// 加密后的字符串
public string MD5(string source)
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(source, "MD5");;
}

方法二(可逆加密解密):
 

代码示例:
using System.Security.Cryptography;
public string Encode(string data)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = cryptoProvider.KeySize;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cst);
sw.Write(data);
sw.Flush();
cst.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
}
public string Decode(string data)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
byte[] byEnc;
try
{
byEnc = Convert.FromBase64String(data);
}
catch
{
return null;
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cst);
return sr.ReadToEnd();
}

方法三(MD5不可逆):
 

代码示例:
using System.Security.Cryptography;
//MD5不可逆加密
//32位加密
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();
}
//16位加密
public static string GetMd5_16(string ConvertString)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
t2 = t2.Replace("-", "");
return t2;
}

方法四(对称加密):
 

代码示例:
using System.IO;
using System.Security.Cryptography;
private SymmetricAlgorithm mobjCryptoService;
private string Key;
///
/// 对称加密类的构造函数
///
public SymmetricMethod()
{
mobjCryptoService = new RijndaelManaged();
Key = "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";
}
///
/// 获得密钥
///
/// 密钥
private byte[] GetLegalKey()
{
string sTemp = Key;
mobjCryptoService.GenerateKey();
byte[] bytTemp = mobjCryptoService.Key;
int KeyLength = bytTemp.Length;
if (sTemp.Length > KeyLength)
sTemp = sTemp.Substring(0, KeyLength);
else if (sTemp.Length < KeyLength)
sTemp = sTemp.PadRight(KeyLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
///
/// 获得初始向量IV
///
/// 初试向量IV
private byte[] GetLegalIV()
{
string sTemp = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk";
mobjCryptoService.GenerateIV();
byte[] bytTemp = mobjCryptoService.IV;
int IVLength = bytTemp.Length;
if (sTemp.Length > IVLength)
sTemp = sTemp.Substring(0, IVLength);
else if (sTemp.Length < IVLength)
sTemp = sTemp.PadRight(IVLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
///
/// 加密方法
///
/// 待加密的串
/// 经过加密的串
public string Encrypto(string Source)
{
byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
MemoryStream ms = new MemoryStream();
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
byte[] bytOut = ms.ToArray();
return Convert.ToBase64String(bytOut);
}
///
/// 解密方法
///
/// 待解密的串
/// 经过解密的串
public string Decrypto(string Source)
{
byte[] bytIn = Convert.FromBase64String(Source);
MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}

方法五:
 

代码示例:
using System.IO;
using System.Security.Cryptography;
using System.Text;
//默认密钥向量
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
///
/// DES加密字符串
///
/// 待加密的字符串
/// 加密密钥,要求为8位
/// 加密成功返回加密后的字符串,失败返回源串
public static 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 static string DecryptDES(string decryptString, string decryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
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;
}
}

方法六(文件加密):
 

代码示例:
using System.IO;
using System.Security.Cryptography;
using System.Text;
//加密文件
private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV)
{
//Create the file streams to handle the input and output files.
FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
//Create variables to help with read and write.
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
long rdlen = 0; //This is the total number of bytes written.
long totlen = fin.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time.
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
//Read from the input file, then encrypt and write to the output file.
while (rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
}
encStream.Close();
fout.Close();
fin.Close();
}
//解密文件
private static void DecryptData(String inName, String outName, byte[] desKey, byte[] desIV)
{
//Create the file streams to handle the input and output files.
FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
//Create variables to help with read and write.
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
long rdlen = 0; //This is the total number of bytes written.
long totlen = fin.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time.
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);
//Read from the input file, then encrypt and write to the output file.
while (rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
}
encStream.Close();
fout.Close();
fin.Close();
}

代码#------
 

代码示例:
using System;
using System.Security.Cryptography;//这个是处理文字编码的前提
using System.Text;
using System.IO;
///
/// DES加密方法
///
/// 明文
/// 密钥
/// 向量
/// 密文
public string DESEncrypt(string strPlain,string strDESKey,string strDESIV)
{
//把密钥转换成字节数组
byte[] bytesDESKey=ASCIIEncoding.ASCII.GetBytes(strDESKey);
//把向量转换成字节数组
byte[] bytesDESIV=ASCIIEncoding.ASCII.GetBytes(strDESIV);
//声明1个新的DES对象
DESCryptoServiceProvider desEncrypt=new DESCryptoServiceProvider();
//开辟一块内存流
MemoryStream msEncrypt=new MemoryStream();
//把内存流对象包装成加密流对象
CryptoStream csEncrypt=new CryptoStream(msEncrypt,desEncrypt.CreateEncryptor(bytesDESKey,bytesDESIV),CryptoStreamMode.Write);
//把加密流对象包装成写入流对象
StreamWriter swEncrypt=new StreamWriter(csEncrypt);
//写入流对象写入明文
swEncrypt.WriteLine(strPlain);
//写入流关闭
swEncrypt.Close();
//加密流关闭
csEncrypt.Close();
//把内存流转换成字节数组,内存流现在已经是密文了
byte[] bytesCipher=msEncrypt.ToArray();
//内存流关闭
msEncrypt.Close();
//把密文字节数组转换为字符串,并返回
return UnicodeEncoding.Unicode.GetString(bytesCipher);
}
///
/// DES解密方法
///
/// 密文
/// 密钥
/// 向量
/// 明文
public string DESDecrypt(string strCipher,string strDESKey,string strDESIV)
{
//把密钥转换成字节数组
byte[] bytesDESKey=ASCIIEncoding.ASCII.GetBytes(strDESKey);
//把向量转换成字节数组
byte[] bytesDESIV=ASCIIEncoding.ASCII.GetBytes(strDESIV);
//把密文转换成字节数组
byte[] bytesCipher=UnicodeEncoding.Unicode.GetBytes(strCipher);
//声明1个新的DES对象
DESCryptoServiceProvider desDecrypt=new DESCryptoServiceProvider();
//开辟一块内存流,并存放密文字节数组
MemoryStream msDecrypt=new MemoryStream(bytesCipher);
//把内存流对象包装成解密流对象
CryptoStream csDecrypt=new CryptoStream(msDecrypt,desDecrypt.CreateDecryptor(bytesDESKey,bytesDESIV),CryptoStreamMode.Read);
//把解密流对象包装成读出流对象
StreamReader srDecrypt=new StreamReader(csDecrypt);
//明文=读出流的读出内容
string strPlainText=srDecrypt.ReadLine();
//读出流关闭
srDecrypt.Close();
//解密流关闭
csDecrypt.Close();
//内存流关闭
msDecrypt.Close();
//返回明文
return strPlainText;
}

    
 
 

您可能感兴趣的文章:

  • c#实现md5加密示例
  • c# MD5加密的代码(简单示例,仅供学习参考)
  • C# DES加密算法中向量的作用详细解析
  • C#生成加密的16位随机数的代码
  • C#中字符串的加密的源码
  • C# 256 位 AES 加密与解密文件的实现代码
  • C#加密在实际中的应用
  • c# rsa注册实现加密文字
  • c#编写可逆加密类(3DES加密)的教程
  • c#实现加密与解密的实例代码
  • C#对字符串加密与解密的一例代码
  • C#使用伪随机数实现加密用户密码的方法
  • C#实现对AES加密和解密的方法
  • c#实现MD5 加密与解密的代码
  • C#简单的加密类实例
  • 基于私钥加密公钥解密的RSA算法C#实现方法
  • c# 图片加密解密的实例代码
  • des加密解密源码 C# key值问题分析
  • C#的3DES加密解密算法实例代码
  • c#文本加密程序代码示例
  • 求对称加密DES算法与非对称加密RSA算法!(可用)
  • php加密算法之实现可逆加密算法和解密分享
  • 关于加密算法的效率问题
  • 关于RC4加密算法的疑问
  • java不可逆加密算法之md5加密算法使用示例
  • java异或加密算法
  • 对于网络传输的数据,一般可以采用什么算法进行加密
  • 寻求java加密算法及实例
  • JAVA有用于数据校验的类吗?象加密算法那样的.
  • 加密算法扩展库 MCrypt
  • linux c 怎样实现 md5 ,des 加密算法
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 实用的PHP带公钥加密类分享(每次加密结果都不一样哦)
  • 一组PHP加密解密函数分享
  • 带密匙的php加密解密示例分享
  • php aes加密类代码分享
  • php加密解密实用类分享
  • php实现aes加密类分享
  • java加密算法分享(rsa解密、对称加密、md5加密)
  • java使用des加密解密示例分享
  • 一个Java配置文件加密解密工具类分享
  • 字符串desede 3des加密示例分享
  • asp.net EncryptHelper 加密帮助类代码分享
  • 利用MySQL加密函数保护Web网站敏感数据的方法分享
  • php写的AES加密解密类分享
  • 兼容PHP和Java的des加密解密代码分享
  • python字符串加密解密的三种方法分享(base64 win32com)
  • php使用base64加密解密图片示例分享
  • 2个比较经典的PHP加密解密函数分享
  • 128进制加密数据示例分享
  • java加密解密示例分享
  • Java、JavaScript、Oracle、MySQL中实现的MD5加密算法分享
  • WinDows8最新版文件夹加密
  • linux和unix怎么样加密口令的?为什么密码一样,加密后的不一样?
  • C#/.NET字符串加密和解密实现(AES和RSA代码举例)
  • 请问,如何在作数据备份的时候,对打包的数据文件自动加密?然后这个密码又如何加密呢?
  • 在unix系统(freebsd)下面用md5加密中文,和win2000下用md5加密后的结果是否一样?
  • 使用java自带des加密算法实现文件加密和字符串加密
  • 非常着急,关于DES加密的,用java加密过的字符串,药用Linux下的C语言来解密,涉及到补位的问题,弄了几天都没有实现,有高手会的,请指点一二!!!!!!!!
  • 关于linux下的文件加密传输。
  • Oracle定义DES加密解密及MD5加密函数示例
  • DES加密的问题!!
  • 求指导~~关于文件加密的


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3