如何对文档进行加密

winnjp 2003-04-23 02:44:10
如何对文档上传加密,使阅读时解密。
...全文
55 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
nightsunman 2003-04-23
  • 打赏
  • 举报
回复
应该上传后把文件按某种加密算法进行加密,阅读时先解密了,怎么还如何??
vikey 2003-04-23
  • 打赏
  • 举报
回复
给一份源码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Security.Cryptography;
using System.Data.SqlClient;
using System.IO.IsolatedStorage;
namespace OAOffice.Contract.upload
{
/// <summary>
/// enc 的摘要说明。
/// </summary>
public class enc : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
public byte[] _key;
public byte[] _iv;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
RijndaelManaged RMCrypto = new RijndaelManaged();
//ViewState.Add(key,RMCrypto.Key);
//ViewState.Add(iv,RMCrypto.IV);
_key = new byte[16];
_iv = new byte[16];
_key=RMCrypto.Key;
_iv=RMCrypto.IV;
ViewState.Add("key",_key);
ViewState.Add("iv",_iv);

IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, null, null);
if(File.Exists(Path.Combine(Server.MapPath("upload"),"en.txt")))
{
//isoStore.DeleteFile(tempfile);
File.Delete(Path.Combine(Server.MapPath("upload"),"en.txt"));
}
IsolatedStorageFileStream isoStream1 = new IsolatedStorageFileStream("en.txt" , FileMode.Create, isoStore);
isoStream1.Close();

EncryptData(Server.MapPath("abc.txt"),Server.MapPath("en.txt"),_key,_iv);
}

}
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 stroage for the encryption
long rdlen=0; //This is the total number fo 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.

RijndaelManaged des = new RijndaelManaged();

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();

}



#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
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 stroage for the encryption
long rdlen=0; //This is the total number fo 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.

RijndaelManaged des = new RijndaelManaged();

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();

}

private void Button1_Click(object sender, System.EventArgs e)
{

IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, null, null);
if(File.Exists(Path.Combine(Server.MapPath("upload"),"de.txt")))
{
//isoStore.DeleteFile(tempfile);
File.Delete(Path.Combine(Server.MapPath("upload"),"de.txt"));
}
IsolatedStorageFileStream isoStream1 = new IsolatedStorageFileStream("de.txt" , FileMode.Create, isoStore);
isoStream1.Close();

DecryptData(Server.MapPath("en.txt"),Server.MapPath("de.txt"),(byte[]) ViewState["key"],(byte[])ViewState["iv"]);


}
}
}

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧