RsaHelper.cs 6.1 KB

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