使用证书来做RSA非对称式加密
本示例中使用了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;
}
}
}
绿色通道:
好文要顶
关注我
收藏该文
与我联系
posted on 2005-11-30 09:29
lcybest
阅读(1705)
评论(4)
编辑
收藏
评论
301820
#1楼
2005-11-30 14:47
keytoo[未注册用户]
回复
引用
好文
#2楼
2005-12-01 13:03
upto
回复
引用
查看
证书要去买~这个比较麻烦,用微软的Certmaker.exe(在Windows SDK自带)生成的证书性能是一个问题,买来的证书没有这个性能问题。
#3楼
2005-12-08 13:52
冰冷
回复
引用
查看
certsrv.msc这个也可以颁发证书的
#4楼
2005-12-21 16:48
taotao1009[未注册用户]
回复
引用
_key=cert.Key;
xml=_key.ToXmlString(true);
为什么我去的私钥和公钥是一样的呢
注册用户登录后才能发表评论,请
登录
或
注册
,
返回博客园首页
。
首页
博问
闪存
新闻
园子
招聘
知识库
最新IT新闻
:
·
谷歌将出售Clearwire股份 不到收购价1/10
·
为什么Google比苹果更令微软恐惧?
·
思科拟2.71亿美元收购私有公司Lightwire
·
社交商务公司Bazaarvoice将于2月24日进行IPO
·
戴尔收购备份软件公司AppAssure
»
更多新闻...
最新知识库文章
:
·
领域模型管理与AOP
·
编程的艺术:漂亮的代码和漂亮的软件
·
GIT分支管理是一门艺术
·
编程:是一门艺术
·
编程是一门艺术吗?
»
更多知识库文章...
China-pub 2011秋季教材巡展
China-Pub 计算机绝版图书按需印刷服务
Winner
公告
计数器
昵称:
lcybest
园龄:
7年3个月
粉丝:
0
关注:
0
导航
博客园
首页
新随笔
联系
订阅
管理
统计
随笔 - 15
文章 - 0
评论 - 86
引用 - 0
搜索
常用链接
我的随笔
我的评论
我的参与
最新评论
我的标签
随笔分类
■
.net framework(6)
(rss)
■
Asp.net(3)
(rss)
■
Biztalk(3)
(rss)
■
Others(4)
(rss)
■
Remoting
(rss)
■
Sharepoint(1)
(rss)
■
Web Service && WSE(1)
(rss)
■
Windows Forms(1)
(rss)
随笔档案
■
2008年12月 (1)
■
2007年11月 (1)
■
2007年7月 (1)
■
2006年5月 (1)
■
2006年2月 (1)
■
2005年12月 (3)
■
2005年11月 (2)
■
2005年7月 (1)
■
2004年12月 (1)
■
2004年11月 (3)
友情链接
积分与排名
积分 - 27483
排名 - 3867
最新评论
阅读排行榜
评论排行榜
推荐排行榜