RsaHelper.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using TEAMModelOS.SDK.Helper.Common.JsonHelper;
  8. namespace TEAMModelOS.SDK.Extension.JwtAuth.JwtHelper
  9. {
  10. public static class RsaHelper
  11. {
  12. public static string RSASign(string data, string privateKeyPem)
  13. {
  14. RSACryptoServiceProvider rsaCsp = LoadCertificateFile(privateKeyPem);
  15. byte[] dataBytes = Encoding.UTF8.GetBytes(data);
  16. byte[] signatureBytes = rsaCsp.SignData(dataBytes, "SHA1");
  17. return Convert.ToBase64String(signatureBytes);
  18. }
  19. private static byte[] GetPem(string type, byte[] data)
  20. {
  21. string pem = Encoding.UTF8.GetString(data);
  22. string header = String.Format("-----BEGIN {0}-----\\n", type);
  23. string footer = String.Format("-----END {0}-----", type);
  24. int start = pem.IndexOf(header) + header.Length;
  25. int end = pem.IndexOf(footer, start);
  26. string base64 = pem.Substring(start, (end - start));
  27. return Convert.FromBase64String(base64);
  28. }
  29. public static string LoadCertificateFileToSting (string filename) {
  30. FileStream fs = System.IO.File.OpenRead(filename);
  31. byte[] data = new byte[fs.Length];
  32. byte[] res = null;
  33. fs.Read(data, 0, data.Length);
  34. if (data[0] != 0x30)
  35. {
  36. res = GetPem("RSA PRIVATE KEY", data);
  37. }
  38. return res.ToJson();
  39. }
  40. public static RSACryptoServiceProvider LoadCertificateFile(string filename)
  41. {
  42. FileStream fs = System.IO.File.OpenRead(filename);
  43. byte[] data = new byte[fs.Length];
  44. byte[] res = null;
  45. fs.Read(data, 0, data.Length);
  46. if (data[0] != 0x30)
  47. {
  48. res = GetPem("RSA PRIVATE KEY", data);
  49. }
  50. try
  51. {
  52. string ss= res.ToJson();
  53. RSACryptoServiceProvider rsa = DecodeRSAPrivateKey(res);
  54. string s = rsa.ToString();
  55. return rsa;
  56. }
  57. catch (Exception ex)
  58. {
  59. }
  60. return null;
  61. }
  62. private static RSACryptoServiceProvider DecodeRSAPrivateKey(byte[] privkey)
  63. {
  64. byte[] MODULUS, E, D, P, Q, DP, DQ, IQ;
  65. // --------- Set up stream to decode the asn.1 encoded RSA private key ------
  66. MemoryStream mem = new MemoryStream(privkey);
  67. BinaryReader binr = new BinaryReader(mem); //wrap Memory Stream with BinaryReader for easy reading
  68. byte bt = 0;
  69. ushort twobytes = 0;
  70. int elems = 0;
  71. try
  72. {
  73. twobytes = binr.ReadUInt16();
  74. if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
  75. binr.ReadByte(); //advance 1 byte
  76. else if (twobytes == 0x8230)
  77. binr.ReadInt16(); //advance 2 bytes
  78. else
  79. return null;
  80. twobytes = binr.ReadUInt16();
  81. if (twobytes != 0x0102) //version number
  82. return null;
  83. bt = binr.ReadByte();
  84. if (bt != 0x00)
  85. return null;
  86. //------ all private key components are Integer sequences ----
  87. elems = GetIntegerSize(binr);
  88. MODULUS = binr.ReadBytes(elems);
  89. elems = GetIntegerSize(binr);
  90. E = binr.ReadBytes(elems);
  91. elems = GetIntegerSize(binr);
  92. D = binr.ReadBytes(elems);
  93. elems = GetIntegerSize(binr);
  94. P = binr.ReadBytes(elems);
  95. elems = GetIntegerSize(binr);
  96. Q = binr.ReadBytes(elems);
  97. elems = GetIntegerSize(binr);
  98. DP = binr.ReadBytes(elems);
  99. elems = GetIntegerSize(binr);
  100. DQ = binr.ReadBytes(elems);
  101. elems = GetIntegerSize(binr);
  102. IQ = binr.ReadBytes(elems);
  103. // ------- create RSACryptoServiceProvider instance and initialize with public key -----
  104. CspParameters CspParameters = new CspParameters();
  105. CspParameters.Flags = CspProviderFlags.UseMachineKeyStore;
  106. RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(1024, CspParameters);
  107. RSAParameters RSAparams = new RSAParameters();
  108. RSAparams.Modulus = MODULUS;
  109. RSAparams.Exponent = E;
  110. RSAparams.D = D;
  111. RSAparams.P = P;
  112. RSAparams.Q = Q;
  113. RSAparams.DP = DP;
  114. RSAparams.DQ = DQ;
  115. RSAparams.InverseQ = IQ;
  116. RSA.ImportParameters(RSAparams);
  117. return RSA;
  118. }
  119. catch (Exception ex)
  120. {
  121. return null;
  122. }
  123. finally
  124. {
  125. binr.Close();
  126. }
  127. }
  128. private static int GetIntegerSize(BinaryReader binr)
  129. {
  130. byte bt = 0;
  131. byte lowbyte = 0x00;
  132. byte highbyte = 0x00;
  133. int count = 0;
  134. bt = binr.ReadByte();
  135. if (bt != 0x02) //expect integer
  136. return 0;
  137. bt = binr.ReadByte();
  138. if (bt == 0x81)
  139. count = binr.ReadByte(); // data size in next byte
  140. else
  141. if (bt == 0x82)
  142. {
  143. highbyte = binr.ReadByte(); // data size in next 2 bytes
  144. lowbyte = binr.ReadByte();
  145. byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
  146. count = BitConverter.ToInt32(modint, 0);
  147. }
  148. else
  149. {
  150. count = bt; // we already have the data size
  151. }
  152. while (binr.ReadByte() == 0x00)
  153. { //remove high order zeros in data
  154. count -= 1;
  155. }
  156. binr.BaseStream.Seek(-1, SeekOrigin.Current); //last ReadByte wasn't a removed zero, so back up a byte
  157. return count;
  158. }
  159. }
  160. }