RSA加密

     < div >
        
< asp:Button  ID ="btnBuildKey"  runat ="server"  Text ="生成密钥"  
            onclick
="btnBuildKey_Click"   />
        
< br  />
        
< br  />
        
< asp:TextBox  ID ="txtData"  runat ="server" ></ asp:TextBox >
        
< br  />
        
< asp:Button  ID ="btnEnCrypt"  runat ="server"  Text ="加密"  
            onclick
="btnEnCrypt_Click"   />
        
< br  />
        
< asp:Label  ID ="lblEnCryptData"  runat ="server"  Text ="加密后的数据"  BackColor ="AliceBlue" ></ asp:Label >
        
< br  />
        
< asp:Button  ID ="btnDeCrypt"  runat ="server"  Text ="解密"  
            onclick
="btnDeCrypt_Click"   />
        
< br  />
        
< asp:Label  ID ="lblData"  runat ="server"  Text ="原始数据" ></ asp:Label >
    
</ div >

 

     protected   void  btnEnCrypt_Click( object  sender, EventArgs e)
    {
        RSACryptoServiceProvider rsa 
=   new  RSACryptoServiceProvider();
        System.Xml.XmlDocument doc 
=   new  System.Xml.XmlDocument();
        doc.Load(Server.MapPath(
" rsaPublicKey.xml " ));
        rsa.FromXmlString(doc.OuterXml);
        
byte [] PlainTextBArray  =  ( new  UnicodeEncoding()).GetBytes(txtData.Text.Trim());
        
byte [] CypherTextBArray  =  rsa.Encrypt(PlainTextBArray,  false );
        
string  strResult  =  Convert.ToBase64String(CypherTextBArray);
        lblEnCryptData.Text 
=  strResult;
    }

    
protected   void  btnDeCrypt_Click( object  sender, EventArgs e)
    {
        RSACryptoServiceProvider rsa 
=   new  RSACryptoServiceProvider();
        System.Xml.XmlDocument doc 
=   new  System.Xml.XmlDocument();
        doc.Load(Server.MapPath(
" rsaPrivateKey.xml " ));
        rsa.FromXmlString(doc.OuterXml);
        
byte [] PlainTextBArray  =  Convert.FromBase64String(lblEnCryptData.Text);
        
byte [] DypherTextBArray  =  rsa.Decrypt(PlainTextBArray,  false );
        
string  strResult  =  ( new  UnicodeEncoding()).GetString(DypherTextBArray);
        lblData.Text 
=  strResult;
    }
    
protected   void  btnBuildKey_Click( object  sender, EventArgs e)
    {
        RSACryptoServiceProvider rsa 
=   new  RSACryptoServiceProvider();
        
string  strPrivateKey  =  rsa.ToXmlString( false );
        System.Xml.XmlDocument doc 
=   new  System.Xml.XmlDocument();
        doc.LoadXml(strPrivateKey);
        doc.Save(Server.MapPath(
" rsaPublicKey.xml " ));

        
string  strPublicKey  =  rsa.ToXmlString( true );
        doc 
=   new  System.Xml.XmlDocument();
        doc.LoadXml(strPublicKey);
        doc.Save(Server.MapPath(
" rsaPrivateKey.xml " ));
    }

 

转载于:https://www.cnblogs.com/yuji/archive/2011/03/22/1991565.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值