惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
D
Docker
腾讯CDC
J
Java Code Geeks
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
博客园 - 三生石上(FineUI控件)
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
IT之家
IT之家
WordPress大学
WordPress大学
M
MIT News - Artificial intelligence
爱范儿
爱范儿
Microsoft Azure Blog
Microsoft Azure Blog
Vercel News
Vercel News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
N
Netflix TechBlog - Medium
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
博客园 - 【当耐特】

博客园 - tonyYe

请求筛选模块被配置为拒绝包含双重转义序列的请求。HTTP 错误 404.11 - Not Found asp.net页面传值方法汇总 - tonyYe - 博客园 Syntax error on line 198 httpd.conf: ServerAdmin takes one argument ASP.NET 2.0中Page事件的执行顺序 {转} EM 无法启动,重新完全配置EM ToString - tonyYe - 博客园 快捷方式制作 ListView导出到Excel TreeView的NodeCheck联动 利用js去除打印时的页眉页脚 用证书实现windows 2003下IIS的SSL安全通信 Windows命令提示符大全 学习.net应该知道什么 C# 3.0新特性之扩展方法 针对构架师的.NET 3.0介绍 Microsoft .net 框架开发平台体系架构 MapGuide公共静态方法 查询保存在outlook里的用户名和密码 ASP.NET 2.0 GridView
使用证书来做RSA非对称式加密
tonyYe · 2008-07-29 · via 博客园 - tonyYe

using System;
using System.Security.Cryptography;
using X509=Microsoft.Web.Services.Security.X509;

namespace Util
{
    
///// <summary>
    
/// EncryptionWithRSA 的摘要说明。
    
/// </summary>

    public class EncryptionWithRSA
    
{
        
///// <summary>
        
/// CertificateName的内部变量
         
/// </summary>

        private string _CertificateName="";

        
///// <summary>
        
/// 构造函数
         
/// </summary>

        public EncryptionWithRSA()
        
{
        }


        
///// <summary>
        
/// 构造函数
         
/// </summary>
        
/// <param name="CertificateName">证书名称</param>

        public EncryptionWithRSA(string CertificateName)
        
{
            
this._CertificateName=CertificateName;
        }


        
///// <summary>
        
/// 证书名称
         
/// </summary>

        public string CertificateName
        
{
            
get
            
{
                
return _CertificateName;
            }

            
set
            
{
                _CertificateName
=value;
            }

        }


        
/**//// <summary>
        
/// 使用WSE的功能来查找证书
        
/// </summary>
        
/// <returns>X509Certificate</returns>

        private X509.X509Certificate GetCertificate(X509.X509CertificateStore store)
        
{

            X509.X509CertificateStore store;
            X509.X509CertificateCollection certs;
            X509.X509Certificate cert;
            store
=X509.X509CertificateStore.CurrentUserStore(store.MyStore);
            
if(!store.Open())
                
throw new System.Exception("CertificateStore can't open!");
            certs
=store.FindCertificateBySubjectString(this._CertificateName);
            
if(certs.Count==0)
                
throw new System.Exception("Can not find certificate");
            cert
=certs[0];
            
return cert;

        }



        
/**//// <summary>
        
/// 获取证书的密钥信息以XML的形式返回
        
/// </summary>
        
/// <param name="cert">Certificate证书</param>
        
/// <param name="PrivateKey">是否获取私钥信息</param>
        
/// <returns>密钥信息</returns>

        private string GetRSAParameters(X509.X509Certificate cert,bool PrivateKey)
        
{
            AsymmetricAlgorithm _key;
            
string xml="";
            
if(!PrivateKey)
            
{
                _key
=cert.PublicKey;
                xml
=_key.ToXmlString(false);
            }

            
else
            
{
                _key
=cert.Key;
                xml
=_key.ToXmlString(true);
            }

            
return xml;
        }




        
/**//// <summary>
        
/// 加密数据
        
/// </summary>
        
/// <param name="data">待加密的数据</param>
        
/// <returns>加密后的数据</returns>

        public string EncryptionData(byte[] data)
        
{
            X509.X509Certificate cert;
            
byte[] output;
            
string msg;
            cert
=GetCertificate(X509.X509CertificateStore.CAStore);
            
string xml=this.GetRSAParameters(cert,false);
            RSACryptoServiceProvider rsa
=new RSACryptoServiceProvider(1024);
            rsa.FromXmlString(xml);
            output
=rsa.Encrypt(data,false);
            msg
=Convert.ToBase64String(output);
            
return msg;
        }


        
/**//// <summary>
        
/// 解密数据
        
/// </summary>
        
/// <param name="EncodeData">待解密的数据</param>
        
/// <returns>解密后的数据</returns>

        public byte[] DecryptionData(string EncodeData)
        
{
            X509.X509Certificate cert;
            
byte[] output,btencode;
            cert
=GetCertificate(X509.X509CertificateStore.MyStore);
            
string xml=this.GetRSAParameters(cert,true);
            btencode
=Convert.FromBase64String(EncodeData);
            RSACryptoServiceProvider rsa
=new RSACryptoServiceProvider(1024);
            rsa.FromXmlString(xml);
            output
=rsa.Decrypt(btencode,false);
            
return output;
        }



        
    }

}