C # implement the MD5WITHRSA signature,
This is a blog I wrote a long time ago. Today, I found it out and posted it to the blog Park.
At that time, I had not come up with the connection with UnionPay for a long time. Later, I had a chance to find a similar code for reference and finally got it ready.
This code mainly implements the signature of the java interface connecting the C # server to the mobile UnionPay mobile phone.
Hope to help you
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. security. cryptography. x509Certificates; 6 using System. security. cryptography; 7 8 namespace MD5WithRSATest 9 {10 public class MD5WithRSA11 {12 private static char [] bcdLookup = {'0', '1', '2', '3 ', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D ', 'E', 'F '}; 13 14 /// <summary> 15 /// return the MD5WithRSA signature string 16 /// </summary> 17 /// <param name = "fileName"> pfx Certificate file path </param> 18 /// <param name = "password"> pfx certificate password </param> 19 /// <param name = "strdata"> string to be signed </param> 20 // <param name = "encoding"> character set, the default is ISO-8859-1 </param> 21 // <returns> returns the MD5WithRSA signature string </returns> 22 public static string SignData (string fileName, string password, string strdata, string encoding = "ISO-8859-1") 23 {24 X509Certificate2 objx5092; 25 if (string. isNullOrWhiteSpace (password) 26 {27 objx5092 = new X509Certificate2 (fileName); 28} else29 {30 objx5092 = new X509Certificate2 (fileName, password ); 31} 32 RSACryptoServiceProvider rsa = objx5092.PrivateKey as RSACryptoServiceProvider; 33 byte [] data = Encoding. getEncoding (encoding ). getBytes (strdata); 34 byte [] hashvalue = rsa. signData (data, "MD5"); // use the MD5withRSA signature 35 return bytesToHexStr (hashvalue) for the certificate ); /// convert the signature result to a hexadecimal string 36} 37 // <summary> 38 // convert the signature result to a hexadecimal string 39 /// </ summary> 40 // <param name = "bcd"> byte Number of the signature result </param> 41 // <returns> hexadecimal string </returns> 42 private static string bytesToHexStr (byte [] bcd) 43 {44 StringBuilder s = new StringBuilder (bcd. length * 2); 45 for (int I = 0; I <bcd. length; I ++) 46 {47 s. append (bcdLookup [(bcd [I]> 4) & 0x0f]); 48 s. append (bcdLookup [bcd [I] & 0x0f]); 49} 50 return s. toString (); 51} 52} 53}