记:.net中海关终端节点报文加签与推送
keyInfoName 海关cer文件名,
certificate 海关cer内容
DxpMsg dxpMsg = new DxpMsg();
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add("ceb", "http://www.chinaport.gov.cn/ceb");
namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
namespaces.Add("ds", "http://www.w3.org/2000/09/xmldsig#");
TransInfoType transInfoType = new TransInfoType();
Data data = new Data();
transInfoType.MsgType = "CEB311Message";
transInfoType.SenderId = ceb311Message.BaseTransfer.dxpId;
xml = XmlHelper.SerializeEncoding(ceb311Message, namespaces);
var signatureXml = SignHelper.GetSignatureSignatureValue(keyInfoName, certificate, xml );
xml = xml.Insert(xml.LastIndexOf("</ceb:CEB311Message>"), signatureXml);
data.Value = StringHelper.StringToBase64Byte(xml);
transInfoType.CopMsgId = Guid.NewGuid().ToString();
transInfoType.ReceiverIds = new string[] { "DXPEDCCEB0000002" };
transInfoType.CreatTime = DateTime.Now;
dxpMsg.TransInfo = transInfoType;
dxpMsg.Data = data;
XmlSerializerNamespaces dxpMsgnamespaces = new XmlSerializerNamespaces();
dxpMsgnamespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
string dxpMsg = XmlHelper.SerializeEncoding(dxpMsg, dxpMsgnamespaces);
public static string GetSignatureNotKeyInfo()
{
return GetSignedInfoContentPlus(string.Empty);
}
public static string GetSignatureSignatureValue(string keyInfoName, string certificate, string dataString)
{
string canonicalizeXmlData = Canonicalize(dataString);
SHA1 sha = new SHA1CryptoServiceProvider();
string digest = Convert.ToBase64String(sha.ComputeHash(Encoding.UTF8.GetBytes(canonicalizeXmlData)));
string signedInfostring = Canonicalize(GetSignedInfoContentPlus(digest));
State state = WebSocketSign(signedInfostring);
JObject jObject = JObject.Parse(state.Data);
JArray jArray = jObject["Data"].ToObject<JArray>();
string signatureValue = jArray[0].ToString();
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add("ceb", "http://www.chinaport.gov.cn/ceb");
namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
namespaces.Add("ds", "http://www.w3.org/2000/09/xmldsig#");
KeyInfoType keyInfo = new KeyInfoType();
keyInfo.ItemsElementName = new ItemsChoiceType2[] { ItemsChoiceType2.KeyName, ItemsChoiceType2.X509Data };
X509DataType x509DataType = new X509DataType();
x509DataType.ItemsElementName = new ItemsChoiceType[] { ItemsChoiceType.X509Certificate };
x509DataType.Items = new object[] { Convert.FromBase64String(certificate) };
keyInfo.Items = new object[] { keyInfoName, x509DataType };
string keyInfoxml = XmlHelper.SerializeEncoding(keyInfo, namespaces);
keyInfoxml = keyInfoxml.Substring(keyInfoxml.LastIndexOf("<ds:KeyName>"));
keyInfoxml = "<ds:KeyInfo>" + keyInfoxml;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("<ds:Signature xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\">");
stringBuilder.Append(signedInfostring);
stringBuilder.Append($"<ds:SignatureValue>{signatureValue}</ds:SignatureValue>");
stringBuilder.Append(keyInfoxml);
stringBuilder.Append("</ds:Signature>");
return stringBuilder.ToString();
}
private static string GetSignedInfoContentPlus(string digest)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("<ds:SignedInfo xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\" xmlns:ceb=\"http://www.chinaport.gov.cn/ceb\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
stringBuilder.Append("<ds:CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\"/>");
stringBuilder.Append("<ds:SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"/>");
stringBuilder.Append("<ds:Reference URI=\"\">");
stringBuilder.Append("<ds:Transforms><ds:Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\"/></ds:Transforms>");
stringBuilder.Append("<ds:DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>");
stringBuilder.Append("<ds:DigestValue>");
stringBuilder.Append(digest);
stringBuilder.Append("</ds:DigestValue></ds:Reference></ds:SignedInfo>");
return stringBuilder.ToString();
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构