当前位置: 首页 > 编程日记 > 正文

.NET使用OpenSSL生成的pem密钥文件[1024位]

using System;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.IO;namespace Thinhunan.Cnblogs.Com.RSAUtility
{public class PemConverter{/// <summary>/// 将pem格式公钥转换为RSAParameters/// </summary>/// <param name="pemFileConent">pem公钥内容</param>/// <returns>转换得到的RSAParamenters</returns>public static RSAParameters ConvertFromPemPublicKey(string pemFileConent){if (string.IsNullOrEmpty(pemFileConent)){throw new ArgumentNullException("pemFileConent", "This arg cann't be empty.");}pemFileConent = pemFileConent.Replace("-----BEGIN PUBLIC KEY-----", "").Replace("-----END PUBLIC KEY-----", "").Replace("\n", "").Replace("\r", "");byte[] keyData = Convert.FromBase64String(pemFileConent);if (keyData.Length < 162){throw new ArgumentException("pem file content is incorrect.");}byte[] pemModulus = new byte[128];byte[] pemPublicExponent = new byte[3];Array.Copy(keyData, 29, pemModulus, 0, 128);Array.Copy(keyData, 159, pemPublicExponent, 0, 3);RSAParameters para = new RSAParameters();para.Modulus = pemModulus;para.Exponent = pemPublicExponent;return para;}/// <summary>/// 将pem格式私钥转换为RSAParameters/// </summary>/// <param name="pemFileConent">pem私钥内容</param>/// <returns>转换得到的RSAParamenters</returns>public static RSAParameters ConvertFromPemPrivateKey(string pemFileConent){if (string.IsNullOrEmpty(pemFileConent)){throw new ArgumentNullException("pemFileConent", "This arg cann't be empty.");}pemFileConent = pemFileConent.Replace("-----BEGIN RSA PRIVATE KEY-----", "").Replace("-----END RSA PRIVATE KEY-----", "").Replace("\n", "").Replace("\r","");byte[] keyData = Convert.FromBase64String(pemFileConent);if (keyData.Length < 609){throw new ArgumentException("pem file content is incorrect.");}int index = 11;byte[] pemModulus = new byte[128];Array.Copy(keyData, index, pemModulus, 0, 128);index += 128;index += 2;//141byte[] pemPublicExponent = new byte[3];Array.Copy(keyData, index, pemPublicExponent, 0, 3);index += 3;index += 4;//148byte[] pemPrivateExponent = new byte[128];Array.Copy(keyData, index , pemPrivateExponent, 0, 128);index += 128;index += ((int)keyData[index+1] == 64?2: 3);//279byte[] pemPrime1 = new byte[64];Array.Copy(keyData, index, pemPrime1, 0, 64);index += 64;index += ((int)keyData[index + 1] == 64 ? 2 : 3);//346byte[] pemPrime2 = new byte[64];Array.Copy(keyData, index , pemPrime2, 0, 64);index += 64;index += ((int)keyData[index + 1] == 64 ? 2 : 3);//412/413byte[] pemExponent1 = new byte[64];Array.Copy(keyData,index, pemExponent1, 0, 64);index += 64;index += ((int)keyData[index + 1] == 64 ? 2 : 3);//479/480byte[] pemExponent2 = new byte[64];Array.Copy(keyData, index, pemExponent2, 0, 64);index += 64;index += ((int)keyData[index + 1] == 64 ? 2 : 3);//545/546byte[] pemCoefficient = new byte[64];Array.Copy(keyData, index, pemCoefficient, 0, 64);RSAParameters para = new RSAParameters();para.Modulus = pemModulus;para.Exponent = pemPublicExponent;para.D = pemPrivateExponent;para.P = pemPrime1;para.Q = pemPrime2;para.DP = pemExponent1;para.DQ = pemExponent2;para.InverseQ = pemCoefficient;return para;}}
}测试pem导成RSAParameters成功,使用通过:
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Web;namespace Thinhunan.Cnblogs.Com.RSAUtility
{class Program{#region keysconst string PUBLICKEY =
@"-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDpsDr+W45aFHIkvotZaGK/THlF
FpuZfUtghhWkHAm3H7yvL42J4xHrTr6IeUDCl4eKe6qiIgvYSNoL3u4SERGOeYmV
1F+cocu9IMGnNoicbh1zVW6e8/iGT3xaYQizJoVuWA/TC/zdds2ihCJfHDBDsouO
CXecPapyWCGQNsH5sQIDAQAB
-----END PUBLIC KEY-----";const string PRIVATEKEY =
@"-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDpsDr+W45aFHIkvotZaGK/THlFFpuZfUtghhWkHAm3H7yvL42J
4xHrTr6IeUDCl4eKe6qiIgvYSNoL3u4SERGOeYmV1F+cocu9IMGnNoicbh1zVW6e
8/iGT3xaYQizJoVuWA/TC/zdds2ihCJfHDBDsouOCXecPapyWCGQNsH5sQIDAQAB
AoGBAM/JbFs4y5WbMncrmjpQj+UrOXVOCeLrvrc/4kQ+zgCvTpWywbaGWiuRo+cz
cXrVQ6bGGU362e9hr8f4XFViKemDL4SmJbgSDa1K71i+/LnnzF6sjiDBFQ/jA9SK
4PYrY7a3IkeBQnJmknanykugyQ1xmCjbuh556fOeRPaHnhx1AkEA/flrxJSy1Z+n
Y1RPgDOeDqyG6MhwU1Jl0yJ1sw3Or4qGRXhjTeGsCrKqV0/ajqdkDEM7FNkqnmsB
+vPd116J6wJBAOuNY3oOWvy2fQ32mj6XV+S2vcG1osEUaEuWvEgkGqJ9co6100Qp
j15036AQEEDqbjdqS0ShfeRSwevTJZIap9MCQCeMGDDjKrnDA5CfB0YiQ4FrchJ7
a6o90WdAHW3FP6LsAh59MZFmC6Ea0xWHdLPz8stKCMAlVNKYPRWztZ6ctQMCQQC8
iWbeAy+ApvBhhMjg4HJRdpNbwO6MbLEuD3CUrZFEDfTrlU2MeVdv20xC6ZiY3Qtq
/4FPZZNGdZcSEuc3km5RAkApGkZmWetNwDJMcUJbSBrQMFfrQObqMPBPe+gEniQq
Ttwu1OULHlmUg9eW31wRI2uiXcFCJMHuro6iOQ1VJ4Qs
-----END RSA PRIVATE KEY-----";#endregionstatic void Main(string[] args){           TestSignAndVerify();}public static void TestSignAndVerify(){//signRSAParameters para = PemConverter.ConvertFromPemPrivateKey(PRIVATEKEY);RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();rsa.ImportParameters(para);byte[] testData = Encoding.UTF8.GetBytes("hello");MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();byte[] signData = rsa.SignData(testData, md5);//verifyRSAParameters paraPub = PemConverter.ConvertFromPemPublicKey(PUBLICKEY);RSACryptoServiceProvider rsaPub = new RSACryptoServiceProvider();rsaPub.ImportParameters(paraPub);if (rsaPub.VerifyData(testData, md5, signData)){Console.WriteLine("ok");}else{Console.WriteLine("no");}}}
}

原文地址:http://www.itstrike.cn/Question/-NET-generates-pem-using-OpenSSL-key-file

转载于:https://www.cnblogs.com/JuneZhang/p/7424139.html

相关文章:

[2014百度之星资格赛]

第一个问题&#xff1a; Energy Conversion Problem Description魔法师百小度也有遇到难题的时候——如今。百小度正在一个古老的石门面前&#xff0c;石门上有一段古老的魔法文字&#xff0c;读懂这样的魔法文字须要耗费大量的能量和大量的脑力。过了许久。百小度最终读懂魔法…

视频录制,压缩实现源码

实现代码&#xff1a; <!DOCTYPE html> <html><head><meta charset"utf-8"><title></title><meta name"viewport" content"widthdevice-width, initial-scale1.0"><!-- <script src"./js…

alexa技能个数_如何在您的技能中使用Alexa演示语言

alexa技能个数by Garrett Vargas通过Garrett Vargas 如何在您的技能中使用Alexa演示语言 (How to use Alexa Presentation Language in your skill) Amazon recently released the Alexa Presentation Language (APL). APL provides a richer display for multimodal skills. …

HTML与XML总结

阅览《孙欣HTML》和《刘炜XML》过了一段时间&#xff0c;在这里学到的内容用思维导图来概括。HTML与XML都是标记语言。 同样点&#xff1a; HTML文档与XML文档有类似的结构。前者是&#xff08;head和body&#xff09;后者是&#xff08;声明和主体&#xff09;&#xff0c;大致…

ant PageHeaderWrapper 返回上一页

PageHeaderWrapper 返回上一页实现代码&#xff1a; <PageHeaderWrappertitle{false}content{<a onClick{() > router.goBack()}><Icon type"left" />返回</a>}breadcrumb{{routes: [{ path: /, breadcrumbName: 首页 },{ path: /pay_orde…

ruby 新建对象_Ruby面向对象编程的简介

ruby 新建对象by Saul Costa由Saul Costa Object-oriented programming (OOP) is a programming paradigm organized around objects. At a high level, OOP is all about being able to structure code so that its functionality can be shared throughout the application.…

ASP.NET导出文件FileResult的使用

本文给大家讲一下ASP.NET MVC中如何使用FileResult来导出文件&#xff0c;首先网上相关例子有很多大神都有讲&#xff0c;我在这只是稍微说一点不同——为什么我的导出没有反应呢&#xff1f; 这个问题&#xff0c;我找了半天也没有找到&#xff0c;最后是在一个网友的评论中体…

【AHOI 2016初中组】 自行车比赛 - 贪心

题目描述 小雪非常关注自行车比赛&#xff0c;尤其是环滨湖自行车赛。一年一度的环滨湖自行车赛&#xff0c;需要选手们连续比赛数日&#xff0c;最终按照累计得分决出冠军。今年一共有 N 位参赛选手。每一天的比赛总会决出当日的排名&#xff0c;第一名的选手会获得 N 点得分&…

【Ant Design Pro 三】样式动态绑定 react样式绑定

第一步&#xff0c;创建样式文件&#xff0c;在页面目录下根据自己习惯创建一个less文件&#xff0c;用来写样式类 第二部&#xff0c;引用该文件 import styles from ./details.less; //details.less 代码&#xff1a; .menu {width: 95%; } .navigation-menu{width: 90%; …

react hooks使用_如何使用React和Hooks检测外部点击

react hooks使用by Andrei Cacio通过安德烈卡西奥(Andrei Cacio) 如何使用React和Hooks检测外部点击 (How to detect an outside click with React and Hooks) “外部点击”是什么意思&#xff1f; (What does “Outside Click” mean?) You can think of it as the “anti-b…

正则表达式(1)

正则表达式的概念 正则表达式是一个字符串&#xff0c;使用单个字符串来描述、用来定义匹配规则&#xff0c;匹配一系列符合某个句法规则的字符串。在开发中&#xff0c;正则表达式通常被用来检索、替换那些符合某个规则的文本。 正则表达式的匹配规则 字符类&#xff1a;[abc]…

Android 曲线动画animation,类似加入购物车动画

按照惯例先放效果图&#xff1a;图中小球做抛物线运动 资源图片 1.首先布局文件activity_main.xml&#xff0c;布局很简单&#xff0c;就一个测试按钮 1 <RelativeLayout xmlns:android"http://schemas.android.com/apk/res/android"2 xmlns:tools"http:…

小程序音频播放报10001 解决方案 errCode:10001, errMsg:errCode:602,err:error,not found param

音频播放有两种方式&#xff1a; 第一种&#xff1a; innerAudioContext.src audioSrc;innerAudioContext.play(); 第二种&#xff1a; innerAudioContext.autoplay true;innerAudioContext.src audioSrc; 之前使用第一种&#xff0c;华为手机不能正常播放&#xff0c;…

在线学位课程_您在四年制计算机科学学位课程中学到的知识

在线学位课程by Colin Smith通过科林史密斯 您在四年制计算机科学学位课程中学到的知识 (What you learn in a 4 year Computer Science degree) I recently wrote an article on whether you need a computer science degree to get a job in tech. I thought that going ove…

Swift学习笔记-协议(Protocols)

1.0 翻译&#xff1a;geek5nan 校对&#xff1a;dabing1022 2.0 翻译&#xff1a;futantan 校对&#xff1a;小铁匠Linus 定稿&#xff1a;shanksyang 本页包含内容&#xff1a; 协议的语法&#xff08;Protocol Syntax&#xff09;对属性的规定&#xff08;Property Requireme…

JavaScript简单重写构造器的原型

1 //简单重写原型对象&#xff1a;2 3 //一个构造函数Person4 function Person(){5 6 }7 //重写Person的原型8 //把Person的原型赋值给一个新的对象 是我们重写的过程9 Person.prototype{ 10 // 对于构造器 如果我们不给他写&#xff0c;则构造器就是Object的构造器了 …

node.js cannot find module

找不到模块的解决方案 &#xff1a; 把node_module整个文件夹删掉,然后npm clean cache,看下package.json里有没有express的依赖项,有的话直接npm install,没有的话 npm install express --save。 有人说 npm clean cache 命令变成了 npm cache clean&#xff0c;可以都试一下

我在React Native中构建时获得的经验教训

by Amanda Bullington通过阿曼达布林顿(Amanda Bullington) 我在React Native中构建时获得的经验教训 (Lessons I learned while building in React Native) When I received an offer for a software engineering role to build an app in React Native, I wasn’t sure what…

【Ant Design Pro 五】箱套路由在菜单栏显示返回上一页

效果图&#xff1a; 场景&#xff1a;从菜单栏进入子页面&#xff0c;但是子页面默认不在路由显示&#xff0c;完成操作后需要返回上级页面。所以要在菜单栏中加返回的功能。 实现代码&#xff1a; import React from react; import { Button, Card, Icon } from antd; impor…

[002] The Perks of Being a Wallflower - 读后记

The Perks of Being a Wallflower 今天(2015年10月30日 18:26:17)读完"The Perks of Being a Wallflower". 本书290页,我是在小米pad上完成阅读的,epub格式,花费四天时间,每天至少5小时. 生词很多,就不一一列出了. 使用透析法并不强求完全的正确理解原文.强调完整的阅…

ios集成firebase_如何将Firebase与您的应用程序集成

ios集成firebaseYou’ve probably heard about Firebase, but may not know much about how it works and how it fits in with your application. Well, you’ve come to the right place. We’ll go over what Firebase is and how to integrate it with your Android projec…

PLSQL创建Oracle定时任务

转自&#xff1a;http://www.cnblogs.com/yx007/p/6519544.html#_label0转载于:https://www.cnblogs.com/good-tomorrow/p/7443817.html

判断h5是不是在小程序中

执行代码&#xff1a; if (ua.indexOf(MicroMessenger) -1) {//说明不在微信中// 走不在小程序的逻辑 } else {wx.miniProgram.getEnv(function(res) {if (res.miniprogram) {// 走在小程序的逻辑} else {// 走不在小程序的逻辑}}) }

CSS3关于过渡效果的问题

首先trasition:transform只是单单表示后面只要有含有的tranform的所有属性可以参与动画&#xff0c;而trasition:all表示后面所有动画属性都可以参动画&#xff0c;当父容器有relative时&#xff0c;子容器有absolute&#xff0c;子容器会跟着父容器相对定位。当你想要然后一个…

在线学位课程_如何选择计算机科学学位课程

在线学位课程by Colin Smith通过科林史密斯 如何选择计算机科学学位课程 (How to choose a Computer Science degree program) I remember combing through the countless computer science programs online and feeling a bit lost on what I should be looking for. I ended…

Dubbo 入门实例 本地伪集群测试Demo

1. 概述 Dubbo是一个分布式服务框架&#xff0c;致力于提供高性能和透明化的RPC远程服务调用方案&#xff0c;以及SOA服务治理方案 Dubbo是阿里巴巴SOA服务化治理方案的核心框架&#xff0c;每天为2,000个服务提供3,000,000,000次访问量支持&#xff0c;并被广泛应用于阿里巴…

Vue源码终笔-VNode更新与diff算法初探

写完这个就差不多了&#xff0c;准备干新项目了。 确实挺不擅长写东西&#xff0c;感觉都是罗列代码写点注释的感觉&#xff0c;这篇就简单阐述一下数据变动时DOM是如何更新的&#xff0c;主要讲解下其中的diff算法。 先来个正常的html模板&#xff1a; <body><div id…

JS获取当月每天的日期,JS获取本周每天的日期

获取当前月每天的日期&#xff0c;获取当前周每天的日期实现代码&#xff1a; 调用代码&#xff1a; console.log(-----------------, getNowM(), getWeekDay()) 结果&#xff1a;我今天是2020-2-28日 封装方法&#xff1a; function getDay(num, str) {var today new Dat…

@Scheduled注解的scheduler属性什么作用

注解是 Spring Framework 提供的一种机制,用于定义计划任务,即周期性执行的任务。 注解可以应用于方法上,以指示 Spring 容器在特定的时间间隔或按照某种调度规则来调用该方法。 属性是 注解的一个可选属性,它的作用是允许开发者指定一个自定义的 对象来控制任务的调度方式。默认情况下, 注解使用 Spring 内部的 来执行任务,但如果需要更高级的定制化需求,可以通过 属性指定一个自定义的 实现。自定义调度器:共享调度器资源:高级调度需求:假设你想使用 作为调度器,并且希望所有带有

自行车车把会吧车刮坏吗_花10分钟即可开始使用车把

自行车车把会吧车刮坏吗by Wing Puah永帕(Wing Puah) 花10分钟即可开始使用车把 (Take 10 minutes to get started with Handlebars) Nowadays front-end development is no longer about building static HTML markup and compiling SASS files. The rise of Single Page App…