记:.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();
}

posted @ 2022-06-14 15:25  六月Talk  阅读(132)  评论(0编辑  收藏  举报