This is a long time ago wrote a blog, today, he found out to tidy up a blog to publish to the garden
At that time, when the docking union UnionPay did not get out, and then a chance to find a similar code reference for a while finally finished
This code is mainly to achieve the C # service side docking the mobile phone UnionPay Java side of the interface signature
I hope we can help you.
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Security.Cryptography.X509Certificates;6 usingSystem.Security.Cryptography;7 8 namespacemd5withrsatest9 {Ten Public classMd5withrsa One { A Private Static Char[] Bcdlookup = {'0','1','2','3','4','5','6','7','8','9','a','b','C','D','e','F' }; - - /// <summary> the ///returns the signature string for Md5withrsa - /// </summary> - /// <param name= "FileName" >path to the PFX certificate file</param> - /// <param name= "password" >PFX certificate Password</param> + /// <param name= "Strdata" >string to be signed</param> - /// <param name= "encoding" >Character Set, default = Iso-8859-1</param> + /// <returns>returns the signature string for Md5withrsa</returns> A Public Static stringSignData (stringFileName,stringPasswordstringStrdata,stringencoding ="iso-8859-1") at { - X509Certificate2 objx5092; - if(string. Isnullorwhitespace (password)) - { -objx5092 =NewX509Certificate2 (fileName); -}Else in { -objx5092 =Newx509certificate2 (fileName, password); to } +RSACryptoServiceProvider RSA = objx5092. Privatekey asRSACryptoServiceProvider; - byte[] data =encoding.getencoding (Encoding). GetBytes (strdata); the byte[] HashValue = RSA. SignData (data,"MD5");//using Md5withrsa signing for certificates * returnBytestohexstr (HashValue);///Convert Signature results to 16 binary strings $ }Panax Notoginseng /// <summary> - ///Convert Signature results to 16 binary strings the /// </summary> + /// <param name= "BCD" >the byte number of the signature result</param> A /// <returns>16 binary string</returns> the Private Static stringBYTESTOHEXSTR (byte[] BCD) + { -StringBuilder s =NewStringBuilder (BCD. Length *2); $ for(inti =0; I < BCD. Length; i++) $ { -S.append (bcdlookup[(bcd[i) >>4) &0x0f]); -S.append (bcdlookup[bcd[i) &0x0f]); the } - returns.tostring ();Wuyi } the } -}
C # Implementing Md5withrsa signatures