文件加密&字符加密的代码
2006-02-27 22:29
363 查看
///<summary>文件加密类 使用DES加密文件流</summary>
///<param>desKey: DES的密钥;desIV: DES向量</param>
class encrypfile{
public byte[] desKey;
public byte[] desIV;
public encrypfile(byte[] inputKey,byte[] inputIV){
desKey=inputKey;
desIV=inputIV;
}
///<summary>加密主方法</summary>
///<param>inName:被加密文件名;outName: 加密后文件名</param>
public void begintoencry(string inName,string outName){
FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
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);
while(rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
}
encStream.Close();
fout.Close();
fin.Close();
}
}
加密字符流
//pToEncrypt为需要加密字符串,sKey为密钥
public string Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte数组中
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//建立加密对象的密钥和向量
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);
}
return ret.ToString();
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=497212
///<param>desKey: DES的密钥;desIV: DES向量</param>
class encrypfile{
public byte[] desKey;
public byte[] desIV;
public encrypfile(byte[] inputKey,byte[] inputIV){
desKey=inputKey;
desIV=inputIV;
}
///<summary>加密主方法</summary>
///<param>inName:被加密文件名;outName: 加密后文件名</param>
public void begintoencry(string inName,string outName){
FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
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);
while(rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
}
encStream.Close();
fout.Close();
fin.Close();
}
}
加密字符流
//pToEncrypt为需要加密字符串,sKey为密钥
public string Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte数组中
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//建立加密对象的密钥和向量
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);
}
return ret.ToString();
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=497212
相关文章推荐
- 利用feof()函数复制文件的C代码(解决复制后文件多一字符的问题)
- C++ 哈夫曼树对文件压缩、加密实现代码
- 如何将文件通过base64字符加密以及解码保存到目标文件
- 字符设备驱动3:驱动代码与文件系统中的设备信息的关系
- 用数组做5组数的加法运算,大小写字母自由转化,文件加密原创代码
- Microsoft Script Encoder解密代码,oask加密文件解密
- flash加载脚本文件导致IE脚本错误 ,行53 ,字符3,缺少对象,代码0 , 怎么解决?
- MATLAB代码加密生成.p文件
- 小代码JAVA文件加密(DES3)
- 最简单有效也是最适合程序员的代码文件安全加密保护方法——EFS
- openssl 加密文件的完整实现代码
- 过滤驱动加密文件(代码) 分类: windows驱动程序WDM 2013-09-25 14:40 611人阅读 评论(0) 收藏
- C语言 以字符形式读写文件详解及示例代码
- C# DES 加密/解密类库,支持文件和中文/UNICODE字符,返回BASE64编码字符串
- android使用AES加密和解密文件实例代码
- 黄聪:C# DES 加密/解密类库,支持文件和中文/UNICODE字符,返回BASE64编码字符串
- BMP图片文件加密代码
- C#对文件进行加密解密代码
- 过滤驱动加密文件(代码)
- [转]C# DES 加密/解密类库,支持文件和中文/UNICODE字符,返回BASE64编码字符串