日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

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

發(fā)布時(shí)間:2023/11/27 生活经验 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .NET使用OpenSSL生成的pem密钥文件[1024位] 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

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格式公鑰轉(zhuǎn)換為RSAParameters/// </summary>/// <param name="pemFileConent">pem公鑰內(nèi)容</param>/// <returns>轉(zhuǎn)換得到的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格式私鑰轉(zhuǎn)換為RSAParameters/// </summary>/// <param name="pemFileConent">pem私鑰內(nèi)容</param>/// <returns>轉(zhuǎn)換得到的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;}}
}測(cè)試pem導(dǎo)成RSAParameters成功,使用通過(guò):
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

轉(zhuǎn)載于:https://www.cnblogs.com/JuneZhang/p/7424139.html

總結(jié)

以上是生活随笔為你收集整理的.NET使用OpenSSL生成的pem密钥文件[1024位]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。