您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

194 行
6.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using System.Xml;
  7. namespace GBNewEnergy.Protocol.NEEncrypts
  8. {
  9. #if NETSTANDARD2_0
  10. public class Default_NERSAEncryptImpl : NERSABase
  11. {
  12. private readonly RSA _privateKeyRsaProvider;
  13. private readonly RSA _publicKeyRsaProvider;
  14. private readonly Encoding _encoding;
  15. public override string HashAlgorithmStr=>throw new NotImplementedException();
  16. public override string PublicKey { get; }
  17. public override string PrivateKey { get; }
  18. public override HashAlgorithmName HashAlgorithmName { get; }
  19. public Default_NERSAEncryptImpl(Encoding encoding, HashAlgorithmName hashAlgorithmName, string publicKey, string privateKey)
  20. {
  21. _privateKeyRsaProvider = CreateRsaProviderFromPrivateKey(privateKey);
  22. _publicKeyRsaProvider = CreateRsaProviderFromPublicKey(publicKey);
  23. HashAlgorithmName = hashAlgorithmName;
  24. _encoding = encoding;
  25. PublicKey = publicKey;
  26. PrivateKey = privateKey;
  27. }
  28. public override byte[] Decrypt(byte[] buffer)
  29. {
  30. return _privateKeyRsaProvider.Decrypt(buffer, RSAEncryptionPadding.Pkcs1);
  31. }
  32. public override byte[] Encrypt(byte[] buffer)
  33. {
  34. return _publicKeyRsaProvider.Encrypt(buffer, RSAEncryptionPadding.Pkcs1);
  35. }
  36. /// <summary>
  37. /// 使用私钥签名
  38. /// </summary>
  39. /// <param name="data">原始数据</param>
  40. /// <returns></returns>
  41. public override string Sign(string data)
  42. {
  43. byte[] dataBytes = _encoding.GetBytes(data);
  44. var signatureBytes = _privateKeyRsaProvider.SignData(dataBytes, HashAlgorithmName, RSASignaturePadding.Pkcs1);
  45. return Convert.ToBase64String(signatureBytes);
  46. }
  47. /// <summary>
  48. /// 使用公钥验证签名
  49. /// </summary>
  50. /// <param name="data">原始数据</param>
  51. /// <param name="sign">签名</param>
  52. /// <returns></returns>
  53. public override bool Verify(string data, string sign)
  54. {
  55. byte[] dataBytes = _encoding.GetBytes(data);
  56. byte[] signBytes = Convert.FromBase64String(sign);
  57. var verify = _publicKeyRsaProvider.VerifyData(dataBytes, signBytes, HashAlgorithmName, RSASignaturePadding.Pkcs1);
  58. return verify;
  59. }
  60. private RSA CreateRsaProviderFromPublicKey(string publicKeyString)
  61. {
  62. var rsa = RSA.Create();
  63. FromXmlStringExtensions(rsa,publicKeyString);
  64. return rsa;
  65. }
  66. private RSA CreateRsaProviderFromPrivateKey(string privateKey)
  67. {
  68. var rsa = RSA.Create();
  69. FromXmlStringExtensions(rsa, privateKey);
  70. return rsa;
  71. }
  72. private int GetIntegerSize(BinaryReader binr)
  73. {
  74. byte bt = 0;
  75. int count = 0;
  76. bt = binr.ReadByte();
  77. if (bt != 0x02)
  78. return 0;
  79. bt = binr.ReadByte();
  80. if (bt == 0x81)
  81. count = binr.ReadByte();
  82. else
  83. if (bt == 0x82)
  84. {
  85. var highbyte = binr.ReadByte();
  86. var lowbyte = binr.ReadByte();
  87. byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
  88. count = BitConverter.ToInt32(modint, 0);
  89. }
  90. else
  91. {
  92. count = bt;
  93. }
  94. while (binr.ReadByte() == 0x00)
  95. {
  96. count -= 1;
  97. }
  98. binr.BaseStream.Seek(-1, SeekOrigin.Current);
  99. return count;
  100. }
  101. private bool CompareBytearrays(byte[] a, byte[] b)
  102. {
  103. if (a.Length != b.Length)
  104. return false;
  105. int i = 0;
  106. foreach (byte c in a)
  107. {
  108. if (c != b[i])
  109. return false;
  110. i++;
  111. }
  112. return true;
  113. }
  114. private static void FromXmlStringExtensions(RSA rsa, string xmlString)
  115. {
  116. var parameters = new RSAParameters();
  117. var xmlDoc = new XmlDocument();
  118. xmlDoc.LoadXml(xmlString);
  119. if (xmlDoc.DocumentElement.Name.Equals("RSAKeyValue"))
  120. {
  121. foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
  122. {
  123. switch (node.Name)
  124. {
  125. case "Modulus":
  126. parameters.Modulus = (string.IsNullOrEmpty(node.InnerText)
  127. ? null
  128. : Convert.FromBase64String(node.InnerText));
  129. break;
  130. case "Exponent":
  131. parameters.Exponent = (string.IsNullOrEmpty(node.InnerText)
  132. ? null
  133. : Convert.FromBase64String(node.InnerText));
  134. break;
  135. case "P":
  136. parameters.P = (string.IsNullOrEmpty(node.InnerText)
  137. ? null
  138. : Convert.FromBase64String(node.InnerText));
  139. break;
  140. case "Q":
  141. parameters.Q = (string.IsNullOrEmpty(node.InnerText)
  142. ? null
  143. : Convert.FromBase64String(node.InnerText));
  144. break;
  145. case "DP":
  146. parameters.DP = (string.IsNullOrEmpty(node.InnerText)
  147. ? null
  148. : Convert.FromBase64String(node.InnerText));
  149. break;
  150. case "DQ":
  151. parameters.DQ = (string.IsNullOrEmpty(node.InnerText)
  152. ? null
  153. : Convert.FromBase64String(node.InnerText));
  154. break;
  155. case "InverseQ":
  156. parameters.InverseQ = (string.IsNullOrEmpty(node.InnerText)
  157. ? null
  158. : Convert.FromBase64String(node.InnerText));
  159. break;
  160. case "D":
  161. parameters.D = (string.IsNullOrEmpty(node.InnerText)
  162. ? null
  163. : Convert.FromBase64String(node.InnerText));
  164. break;
  165. }
  166. }
  167. }
  168. else
  169. {
  170. throw new Exception("Invalid XML RSA key.");
  171. }
  172. rsa.ImportParameters(parameters);
  173. }
  174. }
  175. #endif
  176. }