public string GetSignedXml(string xmlDocument, X509Certificate2 cert) { XmlDocument doc = new XmlDocument(); doc.LoadXml(xmlDocument); SignedXml signedXml = new SignedXml(doc) { SigningKey = cert.PrivateKey }; System.Security.Cryptography.Xml.Reference reference = new System.Security.Cryptography.Xml.Reference { Uri = "" }; XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform(); reference.AddTransform(env); signedXml.AddReference(reference); signedXml.ComputeSignature(); XmlElement xmlDigitalSignature = signedXml.GetXml(); doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, true)); var response = xmlSerializer.Deserialize <SSOLibrary.Signature>(xmlDigitalSignature.OuterXml); signableObject.Signature = response; var signedRequest = this.xmlSerializer.Serialize(signableObject.GetType(), signableObject); return(signedRequest); }
/// <summary> /// 把签名上下文转为json对象 /// </summary> /// <returns>返回json对象</returns> public JObject ToJson() { JObject json = new JObject(); json["type"] = Signable.GetType().FullName; using (MemoryStream ms = new MemoryStream()) using (BinaryWriter writer = new BinaryWriter(ms, Encoding.UTF8)) { Signable.SerializeUnsigned(writer); writer.Flush(); json["hex"] = ms.ToArray().ToHexString(); } JArray scripts = new JArray(); for (int i = 0; i < signatures.Length; i++) { if (signatures[i] == null) { scripts.Add(null); } else { scripts.Add(new JObject()); scripts[i]["redeem_script"] = redeemScripts[i].ToHexString(); JArray sigs = new JArray(); foreach (var pair in signatures[i]) { JObject signature = new JObject(); signature["pubkey"] = pair.Key.EncodePoint(true).ToHexString(); signature["signature"] = pair.Value.ToHexString(); sigs.Add(signature); } scripts[i]["signatures"] = sigs; scripts[i]["completed"] = completed[i]; } } json["scripts"] = scripts; return(json); }
/// <summary> /// 把签名上下文转为json对象 /// </summary> /// <returns>返回json对象</returns> public JObject ToJson() { JObject json = new JObject(); json["type"] = Signable.GetType().FullName; using (MemoryStream ms = new MemoryStream()) using (BinaryWriter writer = new BinaryWriter(ms, Encoding.UTF8)) { Signable.SerializeUnsigned(writer); writer.Flush(); json["hex"] = ms.ToArray().ToHexString(); } JArray scripts = new JArray(); for (int i = 0; i < redeemScripts.Length; i++) { if (redeemScripts[i] == null) { scripts.Add(null); } else { scripts.Add(new JObject()); scripts[i]["redeem_script"] = redeemScripts[i].ToHexString(); JArray ps = new JArray(); foreach (byte[] parameter in parameters[i]) { ps.Add(parameter?.ToHexString()); } scripts[i]["parameters"] = ps; scripts[i]["temp"] = temp[i]; } } json["scripts"] = scripts; return(json); }