SHA256WithRSAHelper.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using Org.BouncyCastle.Crypto.Parameters;
  2. using Org.BouncyCastle.Security;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace efunbox_xyyf_windows.util
  10. {
  11. public class SHA256WithRSAHelper
  12. {
  13. private static string RSAPrivateKeyJava2DotNet(string privateKey)
  14. {
  15. RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey));
  16. return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>",
  17. Convert.ToBase64String(privateKeyParam.Modulus.ToByteArrayUnsigned()),
  18. Convert.ToBase64String(privateKeyParam.PublicExponent.ToByteArrayUnsigned()),
  19. Convert.ToBase64String(privateKeyParam.P.ToByteArrayUnsigned()),
  20. Convert.ToBase64String(privateKeyParam.Q.ToByteArrayUnsigned()),
  21. Convert.ToBase64String(privateKeyParam.DP.ToByteArrayUnsigned()),
  22. Convert.ToBase64String(privateKeyParam.DQ.ToByteArrayUnsigned()),
  23. Convert.ToBase64String(privateKeyParam.QInv.ToByteArrayUnsigned()),
  24. Convert.ToBase64String(privateKeyParam.Exponent.ToByteArrayUnsigned()));
  25. }
  26. public static string Sign(string contentForSign, string privateKey)
  27. {
  28. var netKey = RSAPrivateKeyJava2DotNet(privateKey); //转换成适用于.net的私钥
  29. //var rsa = FromXmlString(netKey); //.net core2.2及其以下版本使用,重写FromXmlString(string)方法
  30. var rsa = new RSACryptoServiceProvider();
  31. rsa.FromXmlString(netKey); //.net core3.0直接使用,不需要重写
  32. var rsaClear = new RSACryptoServiceProvider();
  33. var paras = rsa.ExportParameters(true);
  34. rsaClear.ImportParameters(paras); //签名返回
  35. using (var sha256 = new SHA256CryptoServiceProvider())
  36. {
  37. var signData = rsa.SignData(Encoding.Default.GetBytes(contentForSign), sha256);
  38. return Convert.ToBase64String(signData);
  39. }
  40. }
  41. public static string SignSHA1(string contentForSign, string privateKey)
  42. {
  43. var netKey = RSAPrivateKeyJava2DotNet(privateKey); //转换成适用于.net的私钥
  44. //var rsa = FromXmlString(netKey); //.net core2.2及其以下版本使用,重写FromXmlString(string)方法
  45. var rsa = new RSACryptoServiceProvider();
  46. rsa.FromXmlString(netKey); //.net core3.0直接使用,不需要重写
  47. var rsaClear = new RSACryptoServiceProvider();
  48. var paras = rsa.ExportParameters(true);
  49. rsaClear.ImportParameters(paras); //签名返回
  50. using (var sha1 = new SHA1CryptoServiceProvider())
  51. {
  52. var signData = rsa.SignData(Encoding.Default.GetBytes(contentForSign), sha1);
  53. return Convert.ToBase64String(signData);
  54. }
  55. }
  56. private static string RSAPublicKeyJava2DotNet(string publicKey)
  57. {
  58. RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
  59. return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>",
  60. Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()),
  61. Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned()));
  62. }
  63. public static bool VerifySignature(string encryptSource, string compareString, string publicKey)
  64. {
  65. try
  66. {
  67. //.net core2.2及其以下版本使用,重写FromXmlString(string)方法
  68. //using (RSACryptoServiceProvider rsa = FromXmlString(RSAPublicKeyJava2DotNet(publicKey)))
  69. using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
  70. {
  71. rsa.FromXmlString(RSAPublicKeyJava2DotNet(publicKey)); //.net core3.0直接使用,不需要重写
  72. byte[] signature = Convert.FromBase64String(encryptSource);
  73. SHA256Managed sha256 = new SHA256Managed();
  74. RSAPKCS1SignatureDeformatter df = new RSAPKCS1SignatureDeformatter(rsa);
  75. df.SetHashAlgorithm("SHA256");
  76. byte[] compareByte = sha256.ComputeHash(Encoding.Default.GetBytes(compareString));
  77. return df.VerifySignature(compareByte, signature);
  78. }
  79. }
  80. catch (Exception)
  81. {
  82. return false;
  83. }
  84. }
  85. }
  86. }