码迷,mamicode.com
首页 > Windows程序 > 详细

C#安全API

时间:2015-11-17 01:42:21      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:

Bouncycastle

官网地址为:http://www.bouncycastle.org/csharp/。

 

http://blog.csdn.net/popozhu/article/details/5812662

http://www.xuebuyuan.com/301023.html

 

从PEM文件读取秘钥

PemReader r = new PemReader(new StreamReader("Files/pubkey.pem"));
AsymmetricKeyParameter pubKey = (AsymmetricKeyParameter)r.ReadObject();
PemReader r
= new PemReader(new StreamReader("Files/prvkey.pem")); AsymmetricKeyParameter prvKey = (AsymmetricKeyParameter)r.ReadObject();

 

 

加载pfx证书库、读取证书、获取秘钥

FileStream ms = new FileStream("store.p12",OpenMode.Open);
Pkcs12Store store = new Pkcs12StoreBuilder().Build();
store.Load(ms, "123456".ToCharArray());

//获取第一个别名
IEnumerator itor = store.Aliases.GetEnumerator();
itor.MoveNext();
string alins = itor.Current.ToString();

AsymmetricKeyParameter prvKey=null;
AsymmetricKeyParameter pubKey=null;

if(sotre.IsKeyEntry(alins))
  prvKey = store.GetKey(alins).Key;

cert = store.GetCertificate(alins).Certificate;
pubkey=cert.GetPublicKey();

 

加密、解密

AsymmetricKeyParameter prvKey=...;
AsymmetricKeyParameter pubKey=...;

//公钥加密
string origin="abcd1234";
byte[] originbytes=Encoding.Default.getBytes(origin);
IBufferedCipher c = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
c.Init(true, pubKey);
byte[] encryBytes = c.DoFinal(origin);
string encryStr=Encoding.Default.GetString(encryBytes);

//私钥解密
IBufferedCipher c = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
c.Init(false, prvKey);
byte[] recoverBytes=c.DoFinal(encryBytes );
string recover=Encoding.Default.GetString(recoverBytes);

 

签名、验证

 

生成与导出秘钥、证书、库

 

C#安全API

标签:

原文地址:http://www.cnblogs.com/pixy/p/4970398.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!