日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
淺析用OpenSSL生成pem密鑰文件的方法

對于電子商務平臺的安全性來說,進行相關加密是很重要的工作。這里將介紹利用OpenSSL生成的pem密鑰文件,順便將講解相關格式的轉(zhuǎn)換。

10年積累的網(wǎng)站建設、網(wǎng)站制作經(jīng)驗,可以快速應對客戶對網(wǎng)站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡服務。我雖然不認識你,你也不認識我。但先做網(wǎng)站后付款的網(wǎng)站建設流程,更有井陘免費網(wǎng)站建設讓你可以放心的選擇與我們合作。

.NET要使用OpenSSL生成的pem密鑰文件,網(wǎng)上資料很少(http://www.faqs.org/rfcs/rfc1421.html,RFC1421文件又老長老長),僅有的資料還是有錯誤的,所以今天干了件體力活,把PEM密鑰文件610個字節(jié)一個個看過來,終于搞清了它的格式。

  
 
 
  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格式公鑰轉(zhuǎn)換為RSAParameters  
  13.         ///   
  14.         /// pem公鑰內(nèi)容  
  15.         /// 轉(zhuǎn)換得到的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格式私鑰轉(zhuǎn)換為RSAParameters  
  40.         ///   
  41.         /// pem私鑰內(nèi)容  
  42.         /// 轉(zhuǎn)換得到的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密鑰文件的方法
文章起源:http://m.5511xx.com/article/codohes.html