山东跨境申报客户端申报助手1.0
作者:互联网
跨境通申报助手 免费客户端 一年
支持全国进出口海关单据推送,申报操作简单 一键申报,回执后放行
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