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

推荐订阅源

V
Vulnerabilities – Threatpost
U
Unit 42
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
F
Full Disclosure
月光博客
月光博客
Engineering at Meta
Engineering at Meta
博客园_首页
The Register - Security
The Register - Security
G
Google Developers Blog
The Cloudflare Blog
博客园 - Franky
K
Kaspersky official blog
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
C
Check Point Blog
NISL@THU
NISL@THU
AI
AI
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
Vercel News
Vercel News
T
Tor Project blog
P
Privacy International News Feed
D
Docker
I
Intezer
L
LangChain Blog
P
Proofpoint News Feed
Security Latest
Security Latest
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
博客园 - 聂微东
AWS News Blog
AWS News Blog
Martin Fowler
Martin Fowler
P
Privacy & Cybersecurity Law Blog
V
V2EX
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
The Hacker News
The Hacker News
T
Tenable Blog
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog

博客园 - 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;
        }



        
    }

}