【加密与解密】C#如何读取pem的KEY文件

本文介绍了如何使用C#读取PEM格式的RSA密钥文件,并通过openssl工具将其转换为DER格式,进一步转换为C#可使用的XML Key,以便进行加密解密操作。详细步骤包括使用openssl的命令行工具进行转换,并提供了openssl的下载链接。
摘要由CSDN通过智能技术生成

1、第一步先用openssl将pem的key转换为der的key //E:\01Doc\bin>openssl.exe rsa -in rsakeydec.pem -outform der -out pri.der
2、调用下面的程序直接读取der转换为c#所需要的xml Key,之后进行密文解密
3、openssl下载地址
http://download.csdn.net/download/jiayanhui2877/4089521

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.ComponentModel;
using System.Runtime.InteropServices;


/*refer: http://q.cnblogs.com/q/70822/ 
 http://blog.chinaunix.net/uid-26729093-id-4449165.html*/
namespace ConsoleApplication1
{


    class Program
    {
        private static int GetIntegerSize(BinaryReader binr)
        {
            byte bt = 0;
            byte lowbyte = 0x00;
            byte highbyte = 0x00;
            int count = 0;
            bt = binr.ReadByte();
            if (bt != 0x02)        //expect integer
                return 0;
            bt = binr.ReadByte();


            if (bt == 0x81)
                count = binr.ReadByte();    // data size in next byte
            else
                if (bt == 0x82)
                {
                    highbyte = binr.ReadByte();    // data size in next 2 bytes
                    lowbyte = binr.ReadByte();
                    byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
                    count = BitConverter.ToInt32(modint, 0);
      
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值