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

推荐订阅源

T
Threat Research - Cisco Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
V
Vulnerabilities – Threatpost
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
S
Secure Thoughts
Microsoft Azure Blog
Microsoft Azure Blog
Blog — PlanetScale
Blog — PlanetScale
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Tailwind CSS Blog
S
SegmentFault 最新的问题
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
AWS News Blog
AWS News Blog
美团技术团队
G
Google Developers Blog
宝玉的分享
宝玉的分享
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
I
InfoQ
小众软件
小众软件
Google DeepMind News
Google DeepMind News
P
Privacy & Cybersecurity Law Blog
Stack Overflow Blog
Stack Overflow Blog
Webroot Blog
Webroot Blog
D
DataBreaches.Net
IT之家
IT之家
PCI Perspectives
PCI Perspectives
人人都是产品经理
人人都是产品经理
Hacker News: Ask HN
Hacker News: Ask HN
L
LangChain Blog
SecWiki News
SecWiki News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cisco Blogs
T
Threatpost
P
Proofpoint News Feed
Y
Y Combinator Blog
Cloudbric
Cloudbric
T
Tor Project blog
量子位
博客园_首页
B
Blog
Hugging Face - Blog
Hugging Face - Blog
GbyAI
GbyAI
D
Darknet – Hacking Tools, Hacker News & Cyber Security

博客园 - Francis Liang

Windows Azure存储容器私有,公共容器,公共Blob的区别 关于Windows Azure 地缘组(Affinity Groups) 使用Windows Azure创建和发布ASP.NET应用程序 创建并使用Windows Azure虚拟机模板 Windows Azure 设置虚拟机静态外网IP地址 Windows Azure 配置多个站点的虚拟网络连接 Windows Azure 虚拟网络配置(Site to Site) Windows Azure 虚拟网络配置(Point to Site) Microsoft Azure云计算第一步—试用帐户申请 拥抱云计算 《Microsoft Office SharePoint Server 2007 Best Practices》书评 修改MOSS2007内容查询部件实现自定义格式显示 [.NET Active Directory开发]根据NativeGuid获取DirectoryEntry实例 IISRESET命令功能新发现 动态改变ASP.net页面标题和动态指定页面样式表 Biztalk Server 2006安装配置 Web开发中对元素样式使用过滤器以增强其可视化效果 使用DIME协议上传文件 使用MSAgent代替传统的MessageBox提示来增用客户端用户体验
使用证书来做RSA非对称式加密
Francis Liang · 2005-11-30 · via 博客园 - Francis Liang

本示例中使用了WSE(Web Service Enhancement)中对证书相关操作的功能,本文中所使用的WSE版本为2.0TP。基于.net framework 1.1

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;
        }



        
    }

}