/// <summary> /// 验证签名 /// </summary> /// <param name="strSignData">已签名的数据</param> /// <param name="strSource">原数据</param> /// <param name="strPublicKeyPath">公钥存放路径</param> /// <returns>true/false</returns> public bool UnsignMD5WithRSA(string strSignData, string strSource, string strPublicKeyPath) { XmlDocument doc = new XmlDocument(); // 装入指定的XML文档 doc.Load(strPublicKeyPath); XmlNodeList node = doc.GetElementsByTagName("RSAKeyValue"); //参数值进行base64编码 XmlNode selectSingleNode = node[0].SelectSingleNode("Modulus"); if (selectSingleNode != null) selectSingleNode.InnerText = Convert.ToBase64String(GetBytes(selectSingleNode.InnerText.Trim())); XmlNode singleNode = node[0].SelectSingleNode("Exponent"); if (singleNode != null) singleNode.InnerText = Convert.ToBase64String(GetBytes(singleNode.InnerText.Trim())); string strKeyPublic = doc.InnerXml; bool bReturn = false; RSACryption RC = new RSACryption(); string m_strHashbyteDeformatter = ""; RC.GetHash(strSource, ref m_strHashbyteDeformatter); if (RC.SignatureDeformatter(strKeyPublic, m_strHashbyteDeformatter, strSignData) == false) { bReturn = false; } else { bReturn = true; } return bReturn; }
/// <summary> /// 签名 /// </summary> /// <param name="strSource">需签名的字符串</param> /// <param name="strPrivateKeyPath">私钥存放路径</param> /// <returns>签名结果</returns> public string SignMD5WithRSA(string strSource, string strPrivateKeyPath) { XmlDocument doc = new XmlDocument(); // 装入指定的XML文档 doc.Load(strPrivateKeyPath); XmlNodeList node = doc.GetElementsByTagName("RSAKeyValue"); //参数值进行base64编码 XmlNode selectSingleNode = node[0].SelectSingleNode("Modulus"); if (selectSingleNode != null) selectSingleNode.InnerText = Convert.ToBase64String(GetBytes(selectSingleNode.InnerText.Trim())); XmlNode singleNode = node[0].SelectSingleNode("Exponent"); if (singleNode != null) singleNode.InnerText = Convert.ToBase64String(GetBytes(singleNode.InnerText.Trim())); XmlNode xmlNode = node[0].SelectSingleNode("P"); if (xmlNode != null) xmlNode.InnerText = Convert.ToBase64String(GetBytes(xmlNode.InnerText.Trim())); XmlNode selectSingleNode1 = node[0].SelectSingleNode("Q"); if (selectSingleNode1 != null) selectSingleNode1.InnerText = Convert.ToBase64String(GetBytes(selectSingleNode1.InnerText.Trim())); XmlNode singleNode1 = node[0].SelectSingleNode("DP"); if (singleNode1 != null) singleNode1.InnerText = Convert.ToBase64String(GetBytes(singleNode1.InnerText.Trim())); XmlNode xmlNode1 = node[0].SelectSingleNode("DQ"); if (xmlNode1 != null) xmlNode1.InnerText = Convert.ToBase64String(GetBytes(xmlNode1.InnerText.Trim())); XmlNode node1 = node[0].SelectSingleNode("InverseQ"); if (node1 != null) node1.InnerText = Convert.ToBase64String(GetBytes(node1.InnerText.Trim())); XmlNode selectSingleNode2 = node[0].SelectSingleNode("D"); if (selectSingleNode2 != null) selectSingleNode2.InnerText = Convert.ToBase64String(GetBytes(selectSingleNode2.InnerText.Trim())); string strKeyPrivate = doc.InnerXml; RSACryption rSACryption = new RSACryption(); String HashData = ""; rSACryption.GetHash(strSource, ref HashData); //签名结果 string strEncryptedSignatureData = ""; rSACryption.SignatureFormatter(strKeyPrivate, HashData, ref strEncryptedSignatureData); return strEncryptedSignatureData; }