zoukankan      html  css  js  c++  java
  • C#安全API

    Bouncycastle库C#版

    官网地址为: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);

    签名、验证

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

  • 相关阅读:
    Apache Maven(三):POM
    Mysql的数据库引擎
    一文搞懂tcp和udp
    OSI七层协议大白话解读
    模型的部分知识及ajax的后端应用
    Django之模型层第二篇:多表操作
    Django基础之模型(models)层之单表及各种查询
    Django基础之视图(views)层、模板层
    Django之路由(urls)层
    创建Django项目基础
  • 原文地址:https://www.cnblogs.com/pixy/p/4970398.html
Copyright © 2011-2022 走看看