浅析用OpenSSL生成pem密钥文件的方法

开发 后端
这里将介绍如何使用.NET来令OpenSSL生成的pem密钥文件,用以保护电子商务平台的数据安全,希望本文能对大家有所帮助。

对于电子商务平台的安全性来说,进行相关加密是很重要的工作。这里将介绍利用OpenSSL生成的pem密钥文件,顺便将讲解相关格式的转换。

.NET要使用OpenSSL生成的pem密钥文件,网上资料很少(http://www.faqs.org/rfcs/rfc1421.html,RFC1421文件又老长老长),仅有的资料还是有错误的,所以今天干了件体力活,把PEM密钥文件610个字节一个个看过来,终于搞清了它的格式。

  1. using System;  
  2. using System.Text;  
  3. using System.Security.Cryptography;  
  4. using System.Web;  
  5. using System.IO;  
  6.  
  7. namespace Thinhunan.Cnblogs.Com.RSAUtility  
  8. {  
  9.     public class PemConverter  
  10.     {  
  11.         ///   
  12.         /// 将pem格式公钥转换为RSAParameters  
  13.         ///   
  14.         /// pem公钥内容  
  15.         /// 转换得到的RSAParamenters  
  16.         public static RSAParameters ConvertFromPemPublicKey(string pemFileConent)  
  17.         {  
  18.             if (string.IsNullOrEmpty(pemFileConent))  
  19.             {  
  20.                 throw new ArgumentNullException("pemFileConent""This arg cann't be empty.");  
  21.             }  
  22.             pemFileConent = pemFileConent.Replace("-----BEGIN PUBLIC KEY-----""").Replace("-----END PUBLIC KEY-----""").Replace("\n""").Replace("\r""");  
  23.             byte[] keyData = Convert.FromBase64String(pemFileConent);  
  24.             if (keyData.Length < 162)  
  25.             {  
  26.                 throw new ArgumentException("pem file content is incorrect.");  
  27.             }  
  28.             byte[] pemModulus = new byte[128];  
  29.             byte[] pemPublicExponent = new byte[3];  
  30.             Array.Copy(keyData, 29, pemModulus, 0, 128);  
  31.             Array.Copy(keyData, 159, pemPublicExponent, 0, 3);  
  32.             RSAParameters para = new RSAParameters();  
  33.             para.Modulus = pemModulus;  
  34.             para.Exponent = pemPublicExponent;  
  35.             return para;  
  36.         }  
  37.  
  38.         ///   
  39.         /// 将pem格式私钥转换为RSAParameters  
  40.         ///   
  41.         /// pem私钥内容  
  42.         /// 转换得到的RSAParamenters  
  43.         public static RSAParameters ConvertFromPemPrivateKey(string pemFileConent)  
  44.         {  
  45.             if (string.IsNullOrEmpty(pemFileConent))  
  46.             {  
  47.                 throw new ArgumentNullException("pemFileConent""This arg cann't be empty.");  
  48.             }  
  49.             pemFileConent = pemFileConent.Replace("-----BEGIN RSA PRIVATE KEY-----""").Replace("-----END RSA PRIVATE KEY-----""").Replace("\n""").Replace("\r","");  
  50.             byte[] keyData = Convert.FromBase64String(pemFileConent);  
  51.             if (keyData.Length < 609)  
  52.             {  
  53.                 throw new ArgumentException("pem file content is incorrect.");  
  54.             }  
  55.  
  56.             int index = 11;  
  57.             byte[] pemModulus = new byte[128];  
  58.             Array.Copy(keyData, index, pemModulus, 0, 128);  
  59.  
  60.             index += 128;  
  61.             index += 2;//141  
  62.             byte[] pemPublicExponent = new byte[3];  
  63.             Array.Copy(keyData, index, pemPublicExponent, 0, 3);  
  64.  
  65.             index += 3;  
  66.             index += 4;//148  
  67.             byte[] pemPrivateExponent = new byte[128];  
  68.             Array.Copy(keyData, index , pemPrivateExponent, 0, 128);  
  69.  
  70.             index += 128;  
  71.             index += ((int)keyData[index+1] == 64?2: 3);//279  
  72.             byte[] pemPrime1 = new byte[64];  
  73.             Array.Copy(keyData, index, pemPrime1, 0, 64);  
  74.  
  75.             index += 64;  
  76.             index += ((int)keyData[index + 1] == 64 ? 2 : 3);//346  
  77.             byte[] pemPrime2 = new byte[64];  
  78.             Array.Copy(keyData, index , pemPrime2, 0, 64);  
  79.  
  80.             index += 64;  
  81.             index += ((int)keyData[index + 1] == 64 ? 2 : 3);//412/413  
  82.             byte[] pemExponent1 = new byte[64];  
  83.             Array.Copy(keyData,index, pemExponent1, 0, 64);  
  84.  
  85.             index += 64;  
  86.             index += ((int)keyData[index + 1] == 64 ? 2 : 3);//479/480  
  87.             byte[] pemExponent2 = new byte[64];  
  88.             Array.Copy(keyData, index, pemExponent2, 0, 64);  
  89.  
  90.             index += 64;  
  91.             index += ((int)keyData[index + 1] == 64 ? 2 : 3);//545/546  
  92.             byte[] pemCoefficient = new byte[64];  
  93.             Array.Copy(keyData, index, pemCoefficient, 0, 64);  
  94.  
  95.             RSAParameters para = new RSAParameters();  
  96.             para.Modulus = pemModulus;  
  97.             para.Exponent = pemPublicExponent;  
  98.             para.D = pemPrivateExponent;  
  99.             para.P = pemPrime1;  
  100.             para.Q = pemPrime2;  
  101.             para.DP = pemExponent1;  
  102.             para.DQ = pemExponent2;  
  103.             para.InverseQ = pemCoefficient;  
  104.             return para;  
  105.         }  
  106.           
  107.     }  
测试pem导成RSAParameters成功,使用通过:
  1. using System;  
  2. using System.Security.Cryptography;  
  3. using System.Text;  
  4. using System.IO;  
  5. using System.Web;  
  6.  
  7.  
  8. namespace Thinhunan.Cnblogs.Com.RSAUtility  
  9. {  
  10.     class Program  
  11.     {  
  12.         #region keys  
  13.  
  14.         const string PUBLICKEY =  
  15. @"-----BEGIN PUBLIC KEY-----  
  16. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDpsDr+W45aFHIkvotZaGK/THlF  
  17. FpuZfUtghhWkHAm3H7yvL42J4xHrTr6IeUDCl4eKe6qiIgvYSNoL3u4SERGOeYmV  
  18. 1F+cocu9IMGnNoicbh1zVW6e8/iGT3xaYQizJoVuWA/TC/zdds2ihCJfHDBDsouO  
  19. CXecPapyWCGQNsH5sQIDAQAB  
  20. -----END PUBLIC KEY-----";  
  21.  
  22.  
  23.         const string PRIVATEKEY =  
  24. @"-----BEGIN RSA PRIVATE KEY-----  
  25. MIICXQIBAAKBgQDpsDr+W45aFHIkvotZaGK/THlFFpuZfUtghhWkHAm3H7yvL42J  
  26. 4xHrTr6IeUDCl4eKe6qiIgvYSNoL3u4SERGOeYmV1F+cocu9IMGnNoicbh1zVW6e  
  27. 8/iGT3xaYQizJoVuWA/TC/zdds2ihCJfHDBDsouOCXecPapyWCGQNsH5sQIDAQAB  
  28. AoGBAM/JbFs4y5WbMncrmjpQj+UrOXVOCeLrvrc/4kQ+zgCvTpWywbaGWiuRo+cz  
  29. cXrVQ6bGGU362e9hr8f4XFViKemDL4SmJbgSDa1K71i+/LnnzF6sjiDBFQ/jA9SK  
  30. 4PYrY7a3IkeBQnJmknanykugyQ1xmCjbuh556fOeRPaHnhx1AkEA/flrxJSy1Z+n  
  31. Y1RPgDOeDqyG6MhwU1Jl0yJ1sw3Or4qGRXhjTeGsCrKqV0/ajqdkDEM7FNkqnmsB  
  32. +vPd116J6wJBAOuNY3oOWvy2fQ32mj6XV+S2vcG1osEUaEuWvEgkGqJ9co6100Qp  
  33. j15036AQEEDqbjdqS0ShfeRSwevTJZIap9MCQCeMGDDjKrnDA5CfB0YiQ4FrchJ7  
  34. a6o90WdAHW3FP6LsAh59MZFmC6Ea0xWHdLPz8stKCMAlVNKYPRWztZ6ctQMCQQC8  
  35. iWbeAy+ApvBhhMjg4HJRdpNbwO6MbLEuD3CUrZFEDfTrlU2MeVdv20xC6ZiY3Qtq  
  36. /4FPZZNGdZcSEuc3km5RAkApGkZmWetNwDJMcUJbSBrQMFfrQObqMPBPe+gEniQq  
  37. Ttwu1OULHlmUg9eW31wRI2uiXcFCJMHuro6iOQ1VJ4Qs  
  38. -----END RSA PRIVATE KEY-----";  
  39.  
  40.         #endregion  
  41.  
  42.         static void Main(string[] args)  
  43.         {              
  44.               
  45.             TestSignAndVerify();  
  46.               
  47.         }  
  48.  
  49.  
  50.  
  51.         public static void TestSignAndVerify()  
  52.         {  
  53.             //sign  
  54.             RSAParameters para = PemConverter.ConvertFromPemPrivateKey(PRIVATEKEY);  
  55.             RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();  
  56.             rsa.ImportParameters(para);  
  57.             byte[] testData = Encoding.UTF8.GetBytes("hello");  
  58.             MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();  
  59.             byte[] signData = rsa.SignData(testData, md5);  
  60.  
  61.             //verify  
  62.             RSAParameters paraPub = PemConverter.ConvertFromPemPublicKey(PUBLICKEY);  
  63.             RSACryptoServiceProvider rsaPub = new RSACryptoServiceProvider();  
  64.             rsaPub.ImportParameters(paraPub);  
  65.             if (rsaPub.VerifyData(testData, md5, signData))  
  66.             {  
  67.                 Console.WriteLine("ok");  
  68.             }  
  69.             else 
  70.             {  
  71.                 Console.WriteLine("no");  
  72.             }  
  73.  
  74.         }  
  75.  
  76.     }  

.NET使用OpenSSL生成的pem密钥文件就介绍到这里。

原文标题:.NET使用OpenSSL生成的pem密钥文件【做电子商务的朋友可能需要】

链接:http://www.cnblogs.com/thinhunan/archive/2009/09/09/ConvertPem2RSAParemeters.html

【编辑推荐】

  1. 简述C# XML解析方法的特点及应用
  2. .NET对象的XML序列化和反序列化概念浅析
  3. .NET对象的XML序列化和反序列化实例详解
  4. C# XML序列化实例浅析
  5. .NET序列化和反序列化基础知识总结
责任编辑:彭凡 来源: 博客园
相关推荐

2009-09-24 10:07:21

Hibernate M

2009-07-16 11:11:39

PowerDesign

2021-05-11 12:46:32

OpenSSLTelnet

2014-04-10 18:52:22

2009-08-12 17:27:11

C#读取文件

2011-03-02 18:16:14

预共享密钥

2009-07-20 16:09:39

2021-09-11 22:54:32

Windows 11Windows微软

2022-08-31 12:57:58

PythonTemplate文件报告

2011-03-10 15:03:40

SQL Server主密钥

2009-07-28 10:36:37

ASP.NET读取Ex

2016-11-11 20:54:37

2009-07-15 10:06:54

Swing实现MDI

2021-02-07 22:59:55

JavaScript编程方法链

2011-03-15 09:51:09

2021-05-08 05:56:15

加密OpenSSL密钥

2009-06-25 17:24:06

Hibernate主键

2011-07-06 17:11:41

ASP

2011-11-17 13:04:58

JDOMJavaXML

2017-11-03 10:40:25

Python复制文件方法
点赞
收藏

51CTO技术栈公众号