ITeye博客

.net 使用pfx签名

xilingyan · 2015-11-03 19:35 · PKI · 编程语言 · Markdown

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;
using Org.BouncyCastle.Utilities.Encoders;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            X509Certificate2 cert = new X509Certificate2("d:/server.pfx", "123456", X509KeyStorageFlags.Exportable);
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            string privatekey = cert.PrivateKey.ToXmlString(true);
            provider.FromXmlString(privatekey);
            byte[] abc = provider.SignData(Encoding.UTF8.GetBytes("abc"), new SHA1CryptoServiceProvider());
            Base64Encoder encoder = new Base64Encoder();
            MemoryStream stream = new MemoryStream();
            int a = encoder.Encode(abc, 0, abc.Length, stream);
            stream.Position = 0;
            StreamReader reader = new StreamReader(stream);
            string signValue = reader.ReadToEnd();
            textBox1.Text = signValue;
        }
    }
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。