使用DESCryptoServiceProvider类对数据或者文件进行加密解密

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Security.Cryptography;
None.gif
using  System.IO;
None.gif
using  System.Text;
None.gif
namespace  CommonFunction
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// secret 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public partial class secrettest : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected string txtFilePath,fileExtName;
InBlock.gif
InBlock.gif        Secret jiami 
= new Secret();
InBlock.gif
InBlock.gif        
protected void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
ExpandedSubBlockEnd.gif
        }

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

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
protected void btnJia_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            txtNew.Text 
= jiami.DesEncrypt(txtValue.Text,"songxiud");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected void btnJie_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            txtValue.Text 
= jiami.DesDecrypt(txtNew.Text.Trim(),"songxiud");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected void btnOpen_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(filePath.PostedFile.FileName != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                txtFilePath 
=filePath.PostedFile.FileName;
InBlock.gif                fileExtName 
= txtFilePath.Substring(txtFilePath.LastIndexOf(".")+1,3);
InBlock.gif
InBlock.gif                
if(fileExtName !="txt" && fileExtName != "TXT")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Response.Write(
"请选择文本文件");
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    StreamReader fileStream 
= new StreamReader(txtFilePath,Encoding.Default);
InBlock.gif                    txtContent.Text 
= fileStream.ReadToEnd();
InBlock.gif                    fileStream.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected void btnFile_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            jiami.DesEncrypt(Server.MapPath(
"txtFile")+"\\work.txt",Server.MapPath("txtFile")+"\\work1.txt","songxiud");
InBlock.gif            StreamReader fileStream 
= new StreamReader(Server.MapPath("txtFile")+"\\work1.txt",Encoding.Default);
InBlock.gif            txtContent.Text 
= fileStream.ReadToEnd();
InBlock.gif            fileStream.Close();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected void btnFileDes_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            jiami.DesDecrypt(Server.MapPath(
"txtFile")+"\\work1.txt",Server.MapPath("txtFile")+"\\workjie.txt","songxiud");
InBlock.gif            StreamReader fileStream 
= new StreamReader(Server.MapPath("txtFile")+"\\workjie.txt",Encoding.Default);
InBlock.gif            txtContent.Text 
= fileStream.ReadToEnd();
InBlock.gif            fileStream.Close();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif    
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" Inherits="CommonFunction.secrettest" CodeFile="secret.aspx.cs"  %>
None.gif
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< title > secret </ title >
None.gif        
< meta  content ="Microsoft Visual Studio 7.0"  name ="GENERATOR" >
None.gif        
< meta  content ="C#"  name ="CODE_LANGUAGE" >
None.gif        
< meta  content ="JavaScript"  name ="vs_defaultClientScript" >
None.gif        
< meta  content ="http://schemas.microsoft.com/intellisense/ie5"  name ="vs_targetSchema" >
None.gif    
</ HEAD >
None.gif    
< body  bgColor ="#ccccff" >
None.gif        
< form  id ="usersecret"  method ="post"  encType ="multipart/form-data"  runat ="server" >
None.gif            
< FONT  face ="宋体" >
None.gif                
< asp:textbox  id ="txtValue"  style ="Z-INDEX: 101; LEFT: 168px; POSITION: absolute; TOP: 72px"  runat ="server" ></ asp:textbox >< asp:button  id ="btnJia"  style ="Z-INDEX: 102; LEFT: 344px; POSITION: absolute; TOP: 72px"  runat ="server"
None.gif                    Text
="数据加密"  onclick ="btnJia_Click" ></ asp:button >< asp:button  id ="btnJie"  style ="Z-INDEX: 103; LEFT: 344px; POSITION: absolute; TOP: 104px"  runat ="server"
None.gif                    Text
="数据解密"  onclick ="btnJie_Click" ></ asp:button >< asp:textbox  id ="txtNew"  style ="Z-INDEX: 104; LEFT: 168px; POSITION: absolute; TOP: 112px"  runat ="server" ></ asp:textbox >< asp:textbox  id ="txtContent"  style ="Z-INDEX: 105; LEFT: 168px; POSITION: absolute; TOP: 160px"
None.gif                    runat
="server"  Width ="200px"  Height ="296px"  TextMode ="MultiLine" ></ asp:textbox >< asp:button  id ="btnFile"  style ="Z-INDEX: 106; LEFT: 408px; POSITION: absolute; TOP: 312px"  runat ="server"
None.gif                    Text
="文件加密"  onclick ="btnFile_Click" ></ asp:button >< asp:button  id ="btnFileDes"  style ="Z-INDEX: 107; LEFT: 408px; POSITION: absolute; TOP: 384px"
None.gif                    runat
="server"  Text ="文件解密"  onclick ="btnFileDes_Click" ></ asp:button >< INPUT  id ="filePath"  style ="Z-INDEX: 108; LEFT: 408px; POSITION: absolute; TOP: 160px"
None.gif                    type
="file"  name ="File1"  runat ="server" >
None.gif                
< asp:button  id ="btnOpen"  style ="Z-INDEX: 109; LEFT: 408px; POSITION: absolute; TOP: 240px"  runat ="server"
None.gif                    Text
="打开文件"  onclick ="btnOpen_Click" ></ asp:button >
None.gif                
< asp:Label  id ="Label1"  style ="Z-INDEX: 110; LEFT: 168px; POSITION: absolute; TOP: 32px"  runat ="server" > 该示例以txtFile文件夹下的Work.txt文件为例 </ asp:Label ></ FONT ></ form >
None.gif    
</ body >
None.gif
</ HTML >
None.gif
None.gif using  System;
None.gif
using  System.Security.Cryptography;
None.gif
using  System.IO;
None.gif
using  System.Text;
None.gif
namespace  CommonFunction
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
///数据加密、解密,文件的加密解密
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Secret
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public Secret()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public string DesEncrypt(string strText, string strEncrKey)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
byte[] byKey=null;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
byte[] IV= dot.gif{0x120x340x560x780x900xAB0xCD0xEF};
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                byKey 
= System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,strEncrKey.Length));
InBlock.gif                DESCryptoServiceProvider des 
= new DESCryptoServiceProvider();
InBlock.gif                
byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
InBlock.gif                MemoryStream ms 
= new MemoryStream();
InBlock.gif                CryptoStream cs 
= new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ;
InBlock.gif                cs.Write(inputByteArray, 
0, inputByteArray.Length);
InBlock.gif                cs.FlushFinalBlock();
InBlock.gif                
return Convert.ToBase64String(ms.ToArray());
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(System.Exception error)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return "error:" +error.Message+"\r";
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string DesDecrypt(string strText,string sDecrKey)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
byte[] byKey = null;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
byte[] IV= dot.gif{0x120x340x560x780x900xAB0xCD0xEF};
InBlock.gif            
byte[] inputByteArray = new Byte[strText.Length];
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                byKey 
= System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8));
InBlock.gif                DESCryptoServiceProvider des 
= new DESCryptoServiceProvider();
InBlock.gif                inputByteArray 
= Convert.FromBase64String(strText);            
InBlock.gif                MemoryStream ms 
= new MemoryStream();
InBlock.gif                CryptoStream cs 
= new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); 
InBlock.gif                cs.Write(inputByteArray, 
0, inputByteArray.Length); 
InBlock.gif                cs.FlushFinalBlock();
InBlock.gif                System.Text.Encoding encoding 
= new System.Text.UTF8Encoding();
InBlock.gif                
return encoding.GetString(ms.ToArray());
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(System.Exception error)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return "error:"+error.Message+"\r";
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public void DesEncrypt(string m_InFilePath,string m_OutFilePath,string strEncrKey)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
byte[] byKey=null;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
byte[] IV= dot.gif{0x120x340x560x780x900xAB0xCD0xEF};
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                byKey 
= System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,8));
InBlock.gif                FileStream fin 
= new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read);
InBlock.gif                FileStream fout 
= new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write);
InBlock.gif                fout.SetLength(
0);
InBlock.gif                
byte[] bin = new byte[100]; 
InBlock.gif                
long rdlen = 0;
InBlock.gif                
long totlen = fin.Length;
InBlock.gif                
int len; 
InBlock.gif                DES des 
= new DESCryptoServiceProvider();
InBlock.gif                CryptoStream encStream 
= new CryptoStream(fout, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
InBlock.gif                
while(rdlen < totlen)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    len 
= fin.Read(bin, 0100);
InBlock.gif                    encStream.Write(bin, 
0, len);
InBlock.gif                    rdlen 
= rdlen + len;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                encStream.Close();
InBlock.gif                fout.Close();
InBlock.gif                fin.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void DesDecrypt(string m_InFilePath,string m_OutFilePath,string sDecrKey)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
byte[] byKey = null;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
byte[] IV= dot.gif{0x120x340x560x780x900xAB0xCD0xEF};
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                byKey 
= System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8));
InBlock.gif                FileStream fin 
= new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read);
InBlock.gif                FileStream fout 
= new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write);
InBlock.gif                fout.SetLength(
0);
InBlock.gif                
InBlock.gif                
byte[] bin = new byte[100]; 
InBlock.gif                
long rdlen = 0
InBlock.gif                
long totlen = fin.Length; 
InBlock.gif                
int len; 
InBlock.gif
InBlock.gif                DES des 
= new DESCryptoServiceProvider();
InBlock.gif                CryptoStream encStream 
= new CryptoStream(fout, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
InBlock.gif
InBlock.gif                
while(rdlen < totlen)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    len 
= fin.Read(bin, 0100);
InBlock.gif                    encStream.Write(bin, 
0, len);
InBlock.gif                    rdlen 
= rdlen + len;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                encStream.Close();
InBlock.gif                fout.Close();
InBlock.gif                fin.Close();
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string MD5Encrypt(string strText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MD5 md5 
= new MD5CryptoServiceProvider();
InBlock.gif            
byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strText));
InBlock.gif            
return System.Text.Encoding.Default.GetString(result);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string MD5Decrypt(string strText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MD5 md5 
= new MD5CryptoServiceProvider();
InBlock.gif            
byte[] result = md5.TransformFinalBlock(System.Text.Encoding.Default.GetBytes(strText),0,strText.Length);
InBlock.gif            
return System.Text.Encoding.Default.GetString(result);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/caviare/archive/2006/12/28/606252.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值