ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

山东跨境申报客户端申报助手1.0

2021-12-11 08:01:06  阅读:175  来源: 互联网

标签:Exception 1.0 string result new byte throw 申报 客户端


跨境通申报助手 免费客户端 一年

 

 支持全国进出口海关单据推送,申报操作简单 一键申报,回执后放行

    public bool IsICOrUKeyOk(out string centiNoOut)
        {
            centiNoOut = "";
            bool result;
            try
            {
                byte[] array = new byte[100];
                int num = array.Length;
                byte[] array2 = new byte[10000];
                int num2 = array2.Length;
                int cardID = ICHepler32.GetCardID(array, out num);
                if (cardID == 0)
                {
                    int certNo = ICHepler32.GetCertNo(array2, out num2);
                    if (certNo == 0)
                    {
                        byte[] array3 = new byte[num2];
                        Array.Copy(array2, array3, num2);
                        centiNoOut = Encoding.UTF8.GetString(array3);
                        return true;
                    }
                    if (certNo == -1)
                    {
                        throw new Exception("卡初始化出错");
                    }
                    if (certNo == -2)
                    {
                        throw new Exception("读取证书号失败");
                    }
                }
                else
                {
                    if (cardID == -1)
                    {
                        throw new Exception("卡初始化出错");
                    }
                    if (cardID == -2)
                    {
                        throw new Exception("读取卡号失败");
                    }
                }
                result = false;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return result;
        }

 

public string SignInfo(string msgTemp, string centiNo)
        {
            if (string.IsNullOrEmpty(this.Password))
            {
                throw new Exception("Ukey卡密码不能为空");
            }
            string text = this.GetRidOfDeclaretion(msgTemp);
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(text);
            if (Regex.IsMatch(xmlDocument.InnerXml, "<ceb:.*/>"))
            {
                throw new Exception("必须是闭合节点");
            }
            string text2 = this.EncryptToSHA1(xmlDocument.InnerXml);
            text = string.Format("<ds:SignedInfo xmlns:ceb=\"http://www.chinaport.gov.cn/ceb\" xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><ds:CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\"></ds:CanonicalizationMethod><ds:SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"></ds:SignatureMethod><ds:Reference URI=\"\"><ds:Transforms><ds:Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\"></ds:Transform></ds:Transforms><ds:DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"></ds:DigestMethod><ds:DigestValue>{0}</ds:DigestValue></ds:Reference></ds:SignedInfo>", text2);
            string text3 = this.OnlySignXml(text);
            if (text3 == "")
            {
                throw new Exception("加签失败");
            }
            return this.RebuildXmlMsg(xmlDocument.InnerXml, text2, text3, centiNo, "");
        }

   private string OnlySignXml(string digestMsg)
        {
            string result;
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(digestMsg);
                IntPtr intPtr = Marshal.AllocHGlobal(bytes.Length);
                Marshal.Copy(bytes, 0, intPtr, bytes.Length);
                byte[] array = new byte[200];
                IntPtr intPtr2 = Marshal.AllocHGlobal(array.Length);
                Marshal.Copy(array, 0, intPtr2, array.Length);
                int num = array.Length;
                string password = this.Password;
                int num2 = ICHepler.Sign(intPtr, bytes.Length, intPtr2, ref num, password);
                array = new byte[num];
                Marshal.Copy(intPtr2, array, 0, num);
                Marshal.FreeHGlobal(intPtr);
                Marshal.FreeHGlobal(intPtr2);
                if (num2 == 0)
                {
                    result = Encoding.UTF8.GetString(array);
                }
                else
                {
                    if (num2 == -1)
                    {
                        throw new Exception("卡初始化出错");
                    }
                    if (num2 == -2)
                    {
                        throw new Exception("卡口令不正确");
                    }
                    if (num2 == -3)
                    {
                        throw new Exception("签名失败");
                    }
                    if (num2 == -4)
                    {
                        throw new Exception("编码失败");
                    }
                    result = "";
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return result;
        }

 private string EncryptToSHA1(string strMsgXml)
        {
            string result;
            try
            {
                SHA1CryptoServiceProvider sha1CryptoServiceProvider = new SHA1CryptoServiceProvider();
                byte[] bytes = Encoding.UTF8.GetBytes(strMsgXml);
                byte[] inArray = sha1CryptoServiceProvider.ComputeHash(bytes);
                sha1CryptoServiceProvider.Clear();
                ((IDisposable)sha1CryptoServiceProvider).Dispose();
                result = Convert.ToBase64String(inArray);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return result;
        }

       
        private string RebuildXmlMsg(string strMsgXml, string digestMsg, string signResult, string centiNo, string signInfo)
        {
            string result;
            try
            {
                string str = string.Format("<ds:Signature xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\"><ds:SignedInfo><ds:CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\"/><ds:SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"/><ds:Reference URI=\"\"><ds:Transforms><ds:Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\"/></ds:Transforms><ds:DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"/><ds:DigestValue>{0}</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>{1}</ds:SignatureValue><ds:KeyInfo><ds:KeyName>{2}</ds:KeyName><ds:X509Data><ds:X509Certificate>{3}</ds:X509Certificate></ds:X509Data></ds:KeyInfo></ds:Signature>", new object[]
                {
                    digestMsg,
                    signResult,
                    centiNo,
                    signInfo
                });
                string text = strMsgXml.Substring(strMsgXml.LastIndexOf('<'));
                strMsgXml = strMsgXml.Replace(text, str + text);
                result = strMsgXml;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return result;
        }

 

private static string ConvertXmlDocumentTostring(XmlDocument xmlDocument)
        {
            MemoryStream memoryStream = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(memoryStream, null)
            {
                Formatting = Formatting.Indented//缩进
            };
            xmlDocument.Save(writer);
            StreamReader streamReader = new StreamReader(memoryStream);
            memoryStream.Position = 0;
            string xmlString = streamReader.ReadToEnd();
            streamReader.Close();
            memoryStream.Close();
            return xmlString;
        }
        private static XmlDocument GetXmlDocument(string xmlString)
        {
            XmlDocument document = new XmlDocument();
            document.LoadXml(xmlString);
            return document;
        }

下载地址:http://sdkjt.kjtcn.net/Downloadlist/downloadKJTHelp

加我QQ :183840232,手机:15557148372,微信号:15557148372

标签:Exception,1.0,string,result,new,byte,throw,申报,客户端
来源: https://www.cnblogs.com/kuangood2018/p/15674528.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有