为了应用程序的安全,连接字符串通常可以加密以后保存到Web.config文件中,请问各位高手,如何将连接字符串加密,然后,又如何保存在web.config中的呢?忘不吝赐教.谢谢
解决方案 »
- richTextBox1.rtf+="" 不追加
- 关于控件重绘,一直没解决
- 在webbrowser中填写表单,如何选中select中的一项呢?附代码
- vs2005中的sql问题,很简单,非常简单
- combobox 的items 如何从文件中载入.
- 当numericUpDown内容用BackSpace键去除变为空白时,引发CausesValidation,让程序停顿数秒无反应??
- 求助:TreeView!!!
- WPF在导出图片到word的时候 怎么设置图片的大小
- 恳求中大型 项目源码!大哥帮忙啊 !
- 请教filesystemwatcher组件的使用方法
- 用Report Service做报表时,现在的报表设计器是整合到vs.net环境的,能否单独使用?
- 有关函数调用的问题!
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;/// <summary>
/// 一个简单的使用.NET非对称加密算法的例子
/// 本例的程序很简单,仅用于说明如何在.NET里面使用非对称(RSA)算法。
/// Kwanhong 2005.9
/// </summary>
class Class1
{
public static void Main(string[] args)
{
Class1 c=new Class1();
c.StartDemo();
} public void StartDemo()
{
//RSA的加解密过程:
// 有 rsa1 和 rsa2 两个RSA对象。
// 现在要 rsa2 发送一段信息给 rsa1, 则先由 rsa1 发送“公钥”给 rsa2
// rsa2 获取得公钥之后,用来加密要发送的数据内容。
// rsa1 获取加密后的内容后,用自己的私钥解密,得出原始的数据内容。 RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider();
RSACryptoServiceProvider rsa2 = new RSACryptoServiceProvider(); string publickey;
publickey=rsa1.ToXmlString(false); //导出 rsa1 的公钥 string plaintext;
plaintext="你好吗?这是用于测试的字符串。"; //原始数据
Console.WriteLine("原始数据是:\n{0}\n",plaintext); rsa2.FromXmlString(publickey); //rsa2 导入 rsa1 的公钥,用于加密信息 //rsa2开始加密
byte[] cipherbytes;
cipherbytes=rsa2.Encrypt(
Encoding.UTF8.GetBytes(plaintext),
false); /*//////////////////////////////////////////////*/
Console.WriteLine("加密后的数据是:");
for(int i=0; i< cipherbytes.Length; i++)
{
Console.Write("{0:X2} ",cipherbytes[i]);
}
Console.WriteLine("\n");
/*//////////////////////////////////////////////*/ //rsa1开始解密
byte[] plaintbytes;
plaintbytes = rsa1.Decrypt(cipherbytes,false); Console.WriteLine("解密后的数据是:");
Console.WriteLine(Encoding.UTF8.GetString(plaintbytes)); Console.ReadLine();
}
}
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;/// <summary>
/// 使用对称加密的例子
/// </summary>
class Class2
{ static void Main(string[] args)
{
Class2 c=new Class2();
c.StartDemo();
} public void StartDemo()
{
//establish symmetric algorithm
SymmetricAlgorithm sa = Rijndael.Create(); //key and iv
sa.GenerateKey(); //产生随机的 (32*8) 位的密钥
//sa.GenerateIV(); //初始向量,在ECB模式里面可以不用IV
sa.Mode = CipherMode.ECB; //块处理模式
sa.Padding = PaddingMode.Zeros; //末尾数据块的填充模式
Console.WriteLine("密钥是:"); ///////////
for (int i=0; i<sa.Key.Length; i++) ///////////
{ ///////////
Console.Write("{0:X2} ",sa.Key[i]); ///////////
} ///////////
Console.WriteLine("\n"); ///////////
//establish crypto stream
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms,sa.CreateEncryptor(),CryptoStreamMode.Write); //
string plaintext; //原始文本
byte[] cipherbytes; //加密后的数据
byte[] finalbytes; //解密后的数据 plaintext="How are you? 这是一行文字。";
byte[] plainbytes = Encoding.UTF8.GetBytes(plaintext);
Console.WriteLine("原始文本是:\n{0}\n",plaintext);
//display plaint text byte array in hex format
Console.WriteLine("原始数据是:"); ///////////
for (int i=0; i<plainbytes.Length; i++) ///////////
{ ///////////
Console.Write("{0:X2} ",plainbytes[i]); ///////////
} ///////////
Console.WriteLine("\n"); /////////// //加密过程
cs.Write(plainbytes, 0, plainbytes.Length);
cs.Close();
cipherbytes = ms.ToArray();
ms.Close(); //display ciphertext byte array in hex format
Console.WriteLine("加密后的数据是:"); ///////////
for (int i=0; i<cipherbytes.Length; i++) ///////////
{ ///////////
Console.Write("{0:X2} ",cipherbytes[i]);///////////
} ///////////
Console.WriteLine("\n"); ///////////
//下面的为加密过程
ms=new MemoryStream(cipherbytes);
cs=new CryptoStream(ms,sa.CreateDecryptor(),CryptoStreamMode.Read);
finalbytes=new byte[plainbytes.Length];
cs.Read(finalbytes,0,plainbytes.Length); Console.WriteLine("解密后的数据是:"); ///////////
for (int i=0; i<finalbytes.Length; i++) ///////////
{ ///////////
Console.Write("{0:X2} ",finalbytes[i]); ///////////
} ///////////
Console.WriteLine("\n"); /////////// string finaltext=Encoding.UTF8.GetString(finalbytes); Console.WriteLine("解密后的文本是:\n{0}\n\n",finaltext );
Console.WriteLine("按任意键继续......");
Console.ReadLine();
}
}
* Copyright(C) 2004-2005 ******** All Rights Reserved.
************************************************************************/using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.Windows.Forms;
/// <summary>
/// Triple Data Encryption Standard algorithms implementations
/// </summary>
/// <Author>Yao</Author>
/// <Date>2005/4/20</Date>public class CryptionData
{
// The length of Encryptionstring should be 8 byte and not be a weak key
private string EncryptionString;// The length of initialization vector should be 8 byte
private static Byte[] EncryptionIV = Encoding.Default.GetBytes("abcdefgh");/// <summary>
/// Constructor
/// </summary>
public CryptionData()
{
}/// <summary>
/// Constructor
/// </summary>
/// <param name="EncryptionString">SecureKey</param>
public CryptionData(string EncryptionString)
{
this.EncryptionString = EncryptionString;
}/// <summary>
/// Encryption method for byte array
/// </summary>
/// <param name="SourceData">source data</param>
/// <returns>byte array</returns>
public byte[] EncryptionByteData(byte[] SourceData)
{
byte[] returnData = null;
try
{
// Create DESCryptoServiceProvider object
DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();// Set SecureKey and IV of desProvider
byte[] byteKey = Encoding.Default.GetBytes(EncryptionString);
desProvider.Key = byteKey;
desProvider.IV = EncryptionIV;// A MemoryStream object
MemoryStream ms = new MemoryStream();// Create Encryptor
ICryptoTransform encrypto = desProvider.CreateEncryptor();// Create CryptoStream object
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);// Encrypt SourceData
cs.Write(SourceData,0,SourceData.Length);
cs.FlushFinalBlock();// Get Encryption result
returnData = ms.ToArray();
}
catch(Exception ex)
{
throw ex;
}return returnData;}/// <summary>
/// Decryption method for byte array
/// </summary>
/// <param name="SourceData">source data</param>
/// <returns>byte array</returns>
public byte[] DecryptionByteData(byte[] SourceData)
{
byte[] returnData = null;
try
{
// Create DESCryptoServiceProvider object
DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();// Set SecureKey and IV of desProvider
byte[] byteKey = Encoding.Default.GetBytes(EncryptionString);
desProvider.Key = byteKey;
desProvider.IV = EncryptionIV;// A MemoryStream object
MemoryStream ms = new MemoryStream();// Create Decryptor
ICryptoTransform encrypto = desProvider.CreateDecryptor();// Create CryptoStream object
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);// Decrypt SourceData
cs.Write(SourceData, 0, SourceData.Length);
cs.FlushFinalBlock();// Get Decryption result
returnData = ms.ToArray();
}
catch(Exception ex)
{
throw ex;
}
return returnData;}/// <summary>
/// Encryption method for string
/// </summary>
/// <param name="SourceData">source data</param>
/// <returns>string</returns>
public string EncryptionStringData(string SourceData)
{
try
{
// Convert source data from string to byte array
byte[] SourData = Encoding.Default.GetBytes(SourceData);// Encrypt byte array
byte[] retData = EncryptionByteData(SourData);// Convert encryption result from byte array to Base64String
return Convert.ToBase64String(retData, 0, retData.Length);
}
catch(Exception ex)
{
throw ex;
}
}/// <summary>
/// Decryption method for string
/// </summary>
/// <param name="SourceData">source data</param>
/// <returns>string</returns>
public string DecryptionStringdata(string SourceData)
{
try
{
// Convert source data from Base64String to byte array
byte[] SourData = Convert.FromBase64String(SourceData);// Decrypt byte array
byte[] retData = DecryptionByteData(SourData);// Convert Decryption result from byte array to string
return Encoding.Default.GetString(retData, 0, retData.Length);
}
catch(Exception ex)
{
throw ex;
}
}
}我写的这个类,加密字符串和字节数组的。
这是最初开发的一个类,加密字节数组这个方法是用来加密小的图像文件的。把图像读到字节数组中,就可以解密解密了。后来改造过一次,加密字符串用的是TripleDES,加密图像,因为速度的关系采用DES算法的。