怎样在C#中实现文件加密 我想用C#做一个文件加密的程序,可不知该怎么做,哪位高手进来指点一下.小弟在此先谢了!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用RSA技术加解密。。可以参考很多网上资料的 文件加密类,128位加密http://dev.csdn.net/develop/article/48/48745.shtm///<summary>文件加密类 使用DES加密文件流</summary>///<param>desKey: DES的密钥;desIV: DES向量</param>http://dev.csdn.net/develop/article/74/74063.shtm 这是我在项目中实际用到的/************************************************************************** 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 keyprivate string EncryptionString;// The length of initialization vector should be 8 byteprivate 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 objectDESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();// Set SecureKey and IV of desProviderbyte[] byteKey = Encoding.Default.GetBytes(EncryptionString);desProvider.Key = byteKey;desProvider.IV = EncryptionIV;// A MemoryStream objectMemoryStream ms = new MemoryStream();// Create EncryptorICryptoTransform encrypto = desProvider.CreateEncryptor();// Create CryptoStream objectCryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);// Encrypt SourceDatacs.Write(SourceData,0,SourceData.Length);cs.FlushFinalBlock();// Get Encryption resultreturnData = 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 objectDESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();// Set SecureKey and IV of desProviderbyte[] byteKey = Encoding.Default.GetBytes(EncryptionString);desProvider.Key = byteKey;desProvider.IV = EncryptionIV;// A MemoryStream objectMemoryStream ms = new MemoryStream();// Create DecryptorICryptoTransform encrypto = desProvider.CreateDecryptor();// Create CryptoStream objectCryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);// Decrypt SourceDatacs.Write(SourceData, 0, SourceData.Length);cs.FlushFinalBlock();// Get Decryption resultreturnData = 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 arraybyte[] SourData = Encoding.Default.GetBytes(SourceData);// Encrypt byte arraybyte[] retData = EncryptionByteData(SourData);// Convert encryption result from byte array to Base64Stringreturn 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 arraybyte[] SourData = Convert.FromBase64String(SourceData);// Decrypt byte arraybyte[] retData = DecryptionByteData(SourData);// Convert Decryption result from byte array to stringreturn Encoding.Default.GetString(retData, 0, retData.Length);}catch(Exception ex){throw ex;}}}我写的这个类,加密字符串和字节数组的。这是最初开发的一个类,加密字节数组这个方法是用来加密小的图像文件的。把图像读到字节数组中,就可以解密解密了。后来改造过一次,加密字符串用的是TripleDES,加密图像,因为速度的关系采用DES算法的。 winfrom 如何在Datagridview显示的数据中隔行插入一行自定义的行 Socke编程如何获取客户端IP和端口?? VS2005中文版装在英文系统上 【急,高分】 treenode 的fullpath 问题 超时时间已到。在从池中获取连接之前超时时间已过? 服务器上的XML文件如何保存到本地 删除课程表课程的问题 学C#是不是非得有C的基础 在用户选择“打开文件”时,如何生成新的窗体。 有没有函数可以把一般的路径变成UNC路径?比如“\\机器名(IP)\C$\winnt\...” 安装问题 请问各位高手,怎样把doc文件转成bmp文件?这是我工作的第一件事,请多多指教
http://dev.csdn.net/develop/article/48/48745.shtm///<summary>文件加密类 使用DES加密文件流</summary>
///<param>desKey: DES的密钥;desIV: DES向量</param>
http://dev.csdn.net/develop/article/74/74063.shtm
* 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算法的。