文件加密&字符加密的代码
来源:互联网 发布:mac os 删除分区 编辑:程序博客网 时间:2024/05/01 10:57
///<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();
}
- 文件加密&字符加密的代码
- 文件加密&字符加密的代码
- 文件加密&字符加密的代码
- RSA加密的类文件代码
- openssl 加密文件的完整实现代码
- OpenSSL 加密文件的完整实现代码
- openssl 加密文件的完整实现代码
- 字符加密
- 字符加密
- 字符加密
- 字符加密
- 字符加密
- 字符加密
- 字符加密
- 字符加密
- 字符加密
- 字符加密
- 字符加密
- 用 .NET 实现插件机制
- 微软资深经理人的项目管理经验 第一章.有效团队的基础
- 第二章 有效的作业方式
- replication(数据复制)中CCD目标表的属性参数-complete, condensed
- 基于性能的编程技巧点滴
- 文件加密&字符加密的代码
- Forensic Discovery --前言
- 郑翔明blog!!
- Linux最新稳定内核2.4.x的网络接口源码的结构(一)
- 使用ADSI来操作IIS的时的路径
- 所谓亲戚
- 802.1x兼容实现
- 摩托罗拉校园招聘及简介
- 由朗讯的一道笔试题想到的!