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

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

10年积累的网站建设、网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站后付款的网站建设流程,更有井陉免费网站建设让你可以放心的选择与我们合作。

.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密钥文件就介绍到这里。

本文名称:浅析用OpenSSL生成pem密钥文件的方法
URL标题:http://www.zyruijie.cn/qtweb/news47/2197.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联