初始化项目
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
namespace ConsoleDemo
|
||||
{
|
||||
public class EncryptDes
|
||||
{
|
||||
|
||||
/**
|
||||
* aStrString 加密内容
|
||||
* aStrKey 加密秘钥
|
||||
*/
|
||||
public static String Encrypt3Des(String aStrString, String aStrKey, CipherMode mode = CipherMode.ECB, String iv = "12345678")
|
||||
{
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
var des = new TripleDESCryptoServiceProvider
|
||||
{
|
||||
Key = Encoding.UTF8.GetBytes(aStrKey),
|
||||
Mode = mode
|
||||
};
|
||||
if (mode == CipherMode.CBC)
|
||||
{
|
||||
des.IV = Encoding.UTF8.GetBytes(iv);
|
||||
}
|
||||
var desEncrypt = des.CreateEncryptor();
|
||||
byte[] buffer = Encoding.UTF8.GetBytes(aStrString);
|
||||
return Convert.ToBase64String(desEncrypt.TransformFinalBlock(buffer, 0, buffer.Length));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
public static string Decrypt3Des(string aStrString, string aStrKey, CipherMode mode = CipherMode.ECB, string iv = "12345678")
|
||||
{
|
||||
try
|
||||
{
|
||||
var des = new TripleDESCryptoServiceProvider
|
||||
{
|
||||
Key = Encoding.UTF8.GetBytes(aStrKey),
|
||||
Mode = mode,
|
||||
Padding = PaddingMode.PKCS7
|
||||
};
|
||||
if (mode == CipherMode.CBC)
|
||||
{
|
||||
des.IV = Encoding.UTF8.GetBytes(iv);
|
||||
}
|
||||
var desDecrypt = des.CreateDecryptor();
|
||||
var result = "";
|
||||
byte[] buffer = Convert.FromBase64String(aStrString);
|
||||
result = Encoding.UTF8.GetString(desDecrypt.TransformFinalBlock(buffer, 0, buffer.Length));
|
||||
return result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
namespace ConsoleDemo
|
||||
{
|
||||
public class PostJson
|
||||
{
|
||||
/**
|
||||
* Json的请求头 post请求地址
|
||||
*/
|
||||
public static string Post4Json(string url,string buildRequest)
|
||||
{
|
||||
string result = "";
|
||||
HttpWebRequest request =(HttpWebRequest) WebRequest.Create(url);
|
||||
request.Method = "POST";
|
||||
request.Timeout = 5000;
|
||||
request.ContentType = "application/json";
|
||||
byte[] byte4builde = Encoding.UTF8.GetBytes(buildRequest);
|
||||
request.ContentLength = byte4builde.Length;
|
||||
using (Stream reqStream=request.GetRequestStream())
|
||||
{
|
||||
reqStream.Write(byte4builde,0,byte4builde.Length);
|
||||
reqStream.Close();
|
||||
}
|
||||
|
||||
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
|
||||
Stream stream = response.GetResponseStream();
|
||||
//获得响应内容
|
||||
using (StreamReader reader=new StreamReader(stream,Encoding.UTF8))
|
||||
{
|
||||
result = reader.ReadToEnd();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
using ConsoleDemo;
|
||||
|
||||
Console.WriteLine("Hello, World!");
|
||||
@@ -0,0 +1,123 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
namespace ConsoleDemo
|
||||
{
|
||||
public class PublicData
|
||||
{
|
||||
/**
|
||||
* 公共报文组装
|
||||
*/
|
||||
public static string publicparam(string content,string platformCode ,
|
||||
string platformAlias,string privateKey,string password)
|
||||
{
|
||||
StringBuilder sign =new StringBuilder();
|
||||
string contentstr=EncryptDes.Encrypt3Des(content, password);
|
||||
sign.Append("content="+contentstr+"&");
|
||||
sign.Append("format=JSON&");
|
||||
sign.Append("platformCode="+platformCode+"&");
|
||||
string time = DateTime.Now.ToString("yyyyMMddHHmmss");
|
||||
string serianlNo = platformAlias + time + GenerateCheckCode(8);
|
||||
sign.Append("serialNo="+serianlNo+"&");
|
||||
sign.Append("signType=RSA&");
|
||||
string timestamp=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
sign.Append("timestamp="+timestamp+"&");
|
||||
sign.Append("version=1.0");
|
||||
string rsasign = RSA.sign(sign.ToString(), privateKey);
|
||||
Hashtable publictable=new Hashtable();
|
||||
publictable.Add("sign",rsasign);
|
||||
publictable.Add("format","JSON");
|
||||
publictable.Add("platformCode",platformCode);
|
||||
publictable.Add("serialNo",serianlNo);
|
||||
publictable.Add("signType","RSA");
|
||||
publictable.Add("timestamp",timestamp);
|
||||
publictable.Add("version","1.0");
|
||||
publictable.Add("content",contentstr);
|
||||
return ToJson.Table2Json(publictable);
|
||||
}
|
||||
|
||||
public static string disposeResponse(string str,string ptpublickey, string deskey)
|
||||
{
|
||||
|
||||
Hashtable strtable=new Hashtable();
|
||||
StringBuilder content=new StringBuilder();
|
||||
Hashtable dispose=new Hashtable();
|
||||
|
||||
string sign = "";
|
||||
string serialNo = "";
|
||||
str=str.Replace("{","").Replace("}","").Replace("\"","").Replace(",","&").Replace(":","&");
|
||||
string[] arraystr = str.Split('&');
|
||||
|
||||
for (int i = 0; i < arraystr.Length-1; i+=2)
|
||||
{
|
||||
if (arraystr[i]=="sign")
|
||||
{
|
||||
sign = arraystr[i + 1];
|
||||
}
|
||||
if (arraystr[i]=="serialNo")
|
||||
{
|
||||
serialNo=arraystr[i + 1];
|
||||
}
|
||||
strtable.Add(arraystr[i],arraystr[i+1]);
|
||||
}
|
||||
strtable.Remove("serialNo");
|
||||
strtable.Remove("sign");
|
||||
ArrayList ke = new ArrayList(strtable.Keys);
|
||||
ke.Sort();
|
||||
foreach (string tableEntry in ke)
|
||||
{
|
||||
content.Append(string.Format("{0}={1}&", tableEntry, strtable[tableEntry]));
|
||||
}
|
||||
content.Append("serialNo=" + serialNo);
|
||||
bool res=RSA.verify(content.ToString(), sign, ptpublickey, "UTF-8");
|
||||
Console.WriteLine(res);
|
||||
if (res)
|
||||
{
|
||||
for (int i = 0; i < arraystr.Length - 1; i += 2)
|
||||
{
|
||||
if (arraystr[i] == "content")
|
||||
{
|
||||
|
||||
arraystr[i+1]=EncryptDes.Decrypt3Des(arraystr[i+1],deskey);
|
||||
}
|
||||
dispose.Add(arraystr[i],arraystr[i+1]);
|
||||
|
||||
}
|
||||
return ToJson.Table2Json(dispose);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
return "验签失败";
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 随机数生成
|
||||
*/
|
||||
private static string GenerateCheckCode(int codeCount)
|
||||
{
|
||||
int rep = 0;
|
||||
string str = string.Empty;
|
||||
long num2 = DateTime.Now.Ticks + rep;
|
||||
rep++;
|
||||
Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
|
||||
for (int i = 0; i < codeCount; i++)
|
||||
{
|
||||
char ch;
|
||||
int num = random.Next();
|
||||
if ((num % 2) == 0)
|
||||
{
|
||||
ch = (char)(0x30 + ((ushort)(num % 10)));
|
||||
}
|
||||
else
|
||||
{
|
||||
ch = (char)(0x41 + ((ushort)(num % 0x1a)));
|
||||
}
|
||||
str = str + ch.ToString();
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,502 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace ConsoleDemo
|
||||
{
|
||||
internal class RSA
|
||||
|
||||
{
|
||||
|
||||
/**
|
||||
* content 签名前的报文
|
||||
* privateKey 私钥
|
||||
* input_charset 编码格式 (以下默认UTF-8)
|
||||
*/
|
||||
public static string sign(string content, string privateKey)
|
||||
{
|
||||
byte[] Data = Encoding.GetEncoding("UTF-8").GetBytes(content);
|
||||
RSACryptoServiceProvider rsa = DecodePemPrivateKey(privateKey);
|
||||
SHA1 sh = new SHA1CryptoServiceProvider();
|
||||
byte[] signData = rsa.SignData(Data, sh);
|
||||
return Convert.ToBase64String(signData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验签
|
||||
/// </summary>
|
||||
/// <param name="content">待验签字符串</param>
|
||||
/// <param name="signedString">签名</param>
|
||||
/// <param name="publicKey">公钥</param>
|
||||
/// <param name="input_charset">编码格式</param>
|
||||
/// <returns>true(通过),false(不通过)</returns>
|
||||
public static bool verify(string content, string signedString, string publicKey, string input_charset)
|
||||
{
|
||||
bool result ;
|
||||
byte[] Data = Encoding.GetEncoding(input_charset).GetBytes(content);
|
||||
byte[] data = Convert.FromBase64String(signedString);
|
||||
RSAParameters paraPub = ConvertFromPublicKey(publicKey);
|
||||
RSACryptoServiceProvider rsaPub = new RSACryptoServiceProvider();
|
||||
rsaPub.ImportParameters(paraPub);
|
||||
SHA1 sh = new SHA1CryptoServiceProvider();
|
||||
result = rsaPub.VerifyData(Data, sh, data);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加密
|
||||
/// </summary>
|
||||
/// <param name="resData">需要加密的字符串</param>
|
||||
/// <param name="publicKey">公钥</param>
|
||||
/// <param name="input_charset">编码格式</param>
|
||||
/// <returns>明文</returns>
|
||||
public static string encryptData(string resData, string publicKey, string input_charset)
|
||||
{
|
||||
byte[] DataToEncrypt = Encoding.ASCII.GetBytes(resData);
|
||||
string result = encrypt(DataToEncrypt, publicKey, input_charset);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 解密
|
||||
/// </summary>
|
||||
/// <param name="resData">加密字符串</param>
|
||||
/// <param name="privateKey">私钥</param>
|
||||
/// <param name="input_charset">编码格式</param>
|
||||
/// <returns>明文</returns>
|
||||
public static string decryptData(string resData, string privateKey, string input_charset)
|
||||
{
|
||||
byte[] DataToDecrypt = Convert.FromBase64String(resData);
|
||||
string result = "";
|
||||
for (int j = 0; j < DataToDecrypt.Length / 128; j++)
|
||||
{
|
||||
byte[] buf = new byte[128];
|
||||
for (int i = 0; i < 128; i++)
|
||||
{
|
||||
|
||||
buf[i] = DataToDecrypt[i + 128 * j];
|
||||
}
|
||||
result += decrypt(buf, privateKey, input_charset);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#region 内部方法
|
||||
|
||||
private static string encrypt(byte[] data, string publicKey, string input_charset)
|
||||
{
|
||||
RSACryptoServiceProvider rsa = DecodePemPublicKey(publicKey);
|
||||
SHA1 sh = new SHA1CryptoServiceProvider();
|
||||
byte[] result = rsa.Encrypt(data, false);
|
||||
|
||||
return Convert.ToBase64String(result);
|
||||
}
|
||||
|
||||
private static string decrypt(byte[] data, string privateKey, string input_charset)
|
||||
{
|
||||
string result = "";
|
||||
RSACryptoServiceProvider rsa = DecodePemPrivateKey(privateKey);
|
||||
SHA1 sh = new SHA1CryptoServiceProvider();
|
||||
byte[] source = rsa.Decrypt(data, false);
|
||||
char[] asciiChars = new char[Encoding.GetEncoding(input_charset).GetCharCount(source, 0, source.Length)];
|
||||
Encoding.GetEncoding(input_charset).GetChars(source, 0, source.Length, asciiChars, 0);
|
||||
result = new string(asciiChars);
|
||||
//result = ASCIIEncoding.ASCII.GetString(source);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static RSACryptoServiceProvider DecodePemPublicKey(String pemstr)
|
||||
{
|
||||
byte[] pkcs8publickkey;
|
||||
pkcs8publickkey = Convert.FromBase64String(pemstr);
|
||||
if (pkcs8publickkey != null)
|
||||
{
|
||||
RSACryptoServiceProvider rsa = DecodeRSAPublicKey(pkcs8publickkey);
|
||||
return rsa;
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
private static RSACryptoServiceProvider DecodePemPrivateKey(String pemstr)
|
||||
{
|
||||
byte[] pkcs8privatekey;
|
||||
pkcs8privatekey = Convert.FromBase64String(pemstr);
|
||||
if (pkcs8privatekey != null)
|
||||
{
|
||||
RSACryptoServiceProvider rsa = DecodePrivateKeyInfo(pkcs8privatekey);
|
||||
return rsa;
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
private static RSACryptoServiceProvider DecodePrivateKeyInfo(byte[] pkcs8)
|
||||
{
|
||||
byte[] SeqOID = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };
|
||||
byte[] seq = new byte[15];
|
||||
|
||||
MemoryStream mem = new MemoryStream(pkcs8);
|
||||
int lenstream = (int)mem.Length;
|
||||
BinaryReader binr = new BinaryReader(mem); //wrap Memory Stream with BinaryReader for easy reading
|
||||
byte bt = 0;
|
||||
ushort twobytes = 0;
|
||||
|
||||
try
|
||||
{
|
||||
twobytes = binr.ReadUInt16();
|
||||
if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
|
||||
binr.ReadByte(); //advance 1 byte
|
||||
else if (twobytes == 0x8230)
|
||||
binr.ReadInt16(); //advance 2 bytes
|
||||
else
|
||||
return null;
|
||||
|
||||
bt = binr.ReadByte();
|
||||
if (bt != 0x02)
|
||||
return null;
|
||||
|
||||
twobytes = binr.ReadUInt16();
|
||||
|
||||
if (twobytes != 0x0001)
|
||||
return null;
|
||||
|
||||
seq = binr.ReadBytes(15); //read the Sequence OID
|
||||
if (!CompareBytearrays(seq, SeqOID)) //make sure Sequence for OID is correct
|
||||
return null;
|
||||
|
||||
bt = binr.ReadByte();
|
||||
if (bt != 0x04) //expect an Octet string
|
||||
return null;
|
||||
|
||||
bt = binr.ReadByte(); //read next byte, or next 2 bytes is 0x81 or 0x82; otherwise bt is the byte count
|
||||
if (bt == 0x81)
|
||||
binr.ReadByte();
|
||||
else
|
||||
if (bt == 0x82)
|
||||
binr.ReadUInt16();
|
||||
//------ at this stage, the remaining sequence should be the RSA private key
|
||||
|
||||
byte[] rsaprivkey = binr.ReadBytes((int)(lenstream - mem.Position));
|
||||
RSACryptoServiceProvider rsacsp = DecodeRSAPrivateKey(rsaprivkey);
|
||||
return rsacsp;
|
||||
}
|
||||
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
finally { binr.Close(); }
|
||||
|
||||
}
|
||||
|
||||
private static bool CompareBytearrays(byte[] a, byte[] b)
|
||||
{
|
||||
if (a.Length != b.Length)
|
||||
return false;
|
||||
int i = 0;
|
||||
foreach (byte c in a)
|
||||
{
|
||||
if (c != b[i])
|
||||
return false;
|
||||
i++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static RSACryptoServiceProvider DecodeRSAPublicKey(byte[] publickey)
|
||||
{
|
||||
// encoded OID sequence for PKCS #1 rsaEncryption szOID_RSA_RSA = "1.2.840.113549.1.1.1"
|
||||
byte[] SeqOID = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };
|
||||
byte[] seq = new byte[15];
|
||||
// --------- Set up stream to read the asn.1 encoded SubjectPublicKeyInfo blob ------
|
||||
MemoryStream mem = new MemoryStream(publickey);
|
||||
BinaryReader binr = new BinaryReader(mem); //wrap Memory Stream with BinaryReader for easy reading
|
||||
byte bt = 0;
|
||||
ushort twobytes = 0;
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
twobytes = binr.ReadUInt16();
|
||||
if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
|
||||
binr.ReadByte(); //advance 1 byte
|
||||
else if (twobytes == 0x8230)
|
||||
binr.ReadInt16(); //advance 2 bytes
|
||||
else
|
||||
return null;
|
||||
|
||||
seq = binr.ReadBytes(15); //read the Sequence OID
|
||||
if (!CompareBytearrays(seq, SeqOID)) //make sure Sequence for OID is correct
|
||||
return null;
|
||||
|
||||
twobytes = binr.ReadUInt16();
|
||||
if (twobytes == 0x8103) //data read as little endian order (actual data order for Bit String is 03 81)
|
||||
binr.ReadByte(); //advance 1 byte
|
||||
else if (twobytes == 0x8203)
|
||||
binr.ReadInt16(); //advance 2 bytes
|
||||
else
|
||||
return null;
|
||||
|
||||
bt = binr.ReadByte();
|
||||
if (bt != 0x00) //expect null byte next
|
||||
return null;
|
||||
|
||||
twobytes = binr.ReadUInt16();
|
||||
if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
|
||||
binr.ReadByte(); //advance 1 byte
|
||||
else if (twobytes == 0x8230)
|
||||
binr.ReadInt16(); //advance 2 bytes
|
||||
else
|
||||
return null;
|
||||
|
||||
twobytes = binr.ReadUInt16();
|
||||
byte lowbyte = 0x00;
|
||||
byte highbyte = 0x00;
|
||||
|
||||
if (twobytes == 0x8102) //data read as little endian order (actual data order for Integer is 02 81)
|
||||
lowbyte = binr.ReadByte(); // read next bytes which is bytes in modulus
|
||||
else if (twobytes == 0x8202)
|
||||
{
|
||||
highbyte = binr.ReadByte(); //advance 2 bytes
|
||||
lowbyte = binr.ReadByte();
|
||||
}
|
||||
else
|
||||
return null;
|
||||
byte[] modint = { lowbyte, highbyte, 0x00, 0x00 }; //reverse byte order since asn.1 key uses big endian order
|
||||
int modsize = BitConverter.ToInt32(modint, 0);
|
||||
|
||||
byte firstbyte = binr.ReadByte();
|
||||
binr.BaseStream.Seek(-1, SeekOrigin.Current);
|
||||
|
||||
if (firstbyte == 0x00)
|
||||
{ //if first byte (highest order) of modulus is zero, don't include it
|
||||
binr.ReadByte(); //skip this null byte
|
||||
modsize -= 1; //reduce modulus buffer size by 1
|
||||
}
|
||||
|
||||
byte[] modulus = binr.ReadBytes(modsize); //read the modulus bytes
|
||||
|
||||
if (binr.ReadByte() != 0x02) //expect an Integer for the exponent data
|
||||
return null;
|
||||
int expbytes = (int)binr.ReadByte(); // should only need one byte for actual exponent data (for all useful values)
|
||||
byte[] exponent = binr.ReadBytes(expbytes);
|
||||
|
||||
// ------- create RSACryptoServiceProvider instance and initialize with public key -----
|
||||
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
|
||||
RSAParameters RSAKeyInfo = new RSAParameters();
|
||||
RSAKeyInfo.Modulus = modulus;
|
||||
RSAKeyInfo.Exponent = exponent;
|
||||
RSA.ImportParameters(RSAKeyInfo);
|
||||
return RSA;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
finally { binr.Close(); }
|
||||
|
||||
}
|
||||
|
||||
private static RSACryptoServiceProvider DecodeRSAPrivateKey(byte[] privkey)
|
||||
{
|
||||
byte[] MODULUS, E, D, P, Q, DP, DQ, IQ;
|
||||
|
||||
// --------- Set up stream to decode the asn.1 encoded RSA private key ------
|
||||
MemoryStream mem = new MemoryStream(privkey);
|
||||
BinaryReader binr = new BinaryReader(mem); //wrap Memory Stream with BinaryReader for easy reading
|
||||
byte bt = 0;
|
||||
ushort twobytes = 0;
|
||||
int elems = 0;
|
||||
try
|
||||
{
|
||||
twobytes = binr.ReadUInt16();
|
||||
if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
|
||||
binr.ReadByte(); //advance 1 byte
|
||||
else if (twobytes == 0x8230)
|
||||
binr.ReadInt16(); //advance 2 bytes
|
||||
else
|
||||
return null;
|
||||
|
||||
twobytes = binr.ReadUInt16();
|
||||
if (twobytes != 0x0102) //version number
|
||||
return null;
|
||||
bt = binr.ReadByte();
|
||||
if (bt != 0x00)
|
||||
return null;
|
||||
|
||||
|
||||
//------ all private key components are Integer sequences ----
|
||||
elems = GetIntegerSize(binr);
|
||||
MODULUS = binr.ReadBytes(elems);
|
||||
|
||||
elems = GetIntegerSize(binr);
|
||||
E = binr.ReadBytes(elems);
|
||||
|
||||
elems = GetIntegerSize(binr);
|
||||
D = binr.ReadBytes(elems);
|
||||
|
||||
elems = GetIntegerSize(binr);
|
||||
P = binr.ReadBytes(elems);
|
||||
|
||||
elems = GetIntegerSize(binr);
|
||||
Q = binr.ReadBytes(elems);
|
||||
|
||||
elems = GetIntegerSize(binr);
|
||||
DP = binr.ReadBytes(elems);
|
||||
|
||||
elems = GetIntegerSize(binr);
|
||||
DQ = binr.ReadBytes(elems);
|
||||
|
||||
elems = GetIntegerSize(binr);
|
||||
IQ = binr.ReadBytes(elems);
|
||||
|
||||
// ------- create RSACryptoServiceProvider instance and initialize with public key -----
|
||||
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
|
||||
RSAParameters RSAparams = new RSAParameters();
|
||||
RSAparams.Modulus = MODULUS;
|
||||
RSAparams.Exponent = E;
|
||||
RSAparams.D = D;
|
||||
RSAparams.P = P;
|
||||
RSAparams.Q = Q;
|
||||
RSAparams.DP = DP;
|
||||
RSAparams.DQ = DQ;
|
||||
RSAparams.InverseQ = IQ;
|
||||
RSA.ImportParameters(RSAparams);
|
||||
return RSA;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
finally { binr.Close(); }
|
||||
}
|
||||
|
||||
private static int GetIntegerSize(BinaryReader binr)
|
||||
{
|
||||
byte bt = 0;
|
||||
byte lowbyte = 0x00;
|
||||
byte highbyte = 0x00;
|
||||
int count = 0;
|
||||
bt = binr.ReadByte();
|
||||
if (bt != 0x02) //expect integer
|
||||
return 0;
|
||||
bt = binr.ReadByte();
|
||||
|
||||
if (bt == 0x81)
|
||||
count = binr.ReadByte(); // data size in next byte
|
||||
else
|
||||
if (bt == 0x82)
|
||||
{
|
||||
highbyte = binr.ReadByte(); // data size in next 2 bytes
|
||||
lowbyte = binr.ReadByte();
|
||||
byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
|
||||
count = BitConverter.ToInt32(modint, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
count = bt; // we already have the data size
|
||||
}
|
||||
|
||||
|
||||
|
||||
while (binr.ReadByte() == 0x00)
|
||||
{ //remove high order zeros in data
|
||||
count -= 1;
|
||||
}
|
||||
binr.BaseStream.Seek(-1, SeekOrigin.Current); //last ReadByte wasn't a removed zero, so back up a byte
|
||||
return count;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 解析.net 生成的Pem
|
||||
private static RSAParameters ConvertFromPublicKey(string pemFileConent)
|
||||
{
|
||||
|
||||
byte[] keyData = Convert.FromBase64String(pemFileConent);
|
||||
if (keyData.Length < 162)
|
||||
{
|
||||
throw new ArgumentException("pem file content is incorrect.");
|
||||
}
|
||||
byte[] pemModulus = new byte[128];
|
||||
byte[] pemPublicExponent = new byte[3];
|
||||
Array.Copy(keyData, 29, pemModulus, 0, 128);
|
||||
Array.Copy(keyData, 159, pemPublicExponent, 0, 3);
|
||||
RSAParameters para = new RSAParameters();
|
||||
para.Modulus = pemModulus;
|
||||
para.Exponent = pemPublicExponent;
|
||||
return para;
|
||||
}
|
||||
|
||||
private static RSAParameters ConvertFromPrivateKey(string pemFileConent)
|
||||
{
|
||||
byte[] keyData = Convert.FromBase64String(pemFileConent);
|
||||
if (keyData.Length < 609)
|
||||
{
|
||||
throw new ArgumentException("pem file content is incorrect.");
|
||||
}
|
||||
|
||||
int index = 11;
|
||||
byte[] pemModulus = new byte[128];
|
||||
Array.Copy(keyData, index, pemModulus, 0, 128);
|
||||
|
||||
index += 128;
|
||||
index += 2;//141
|
||||
byte[] pemPublicExponent = new byte[3];
|
||||
Array.Copy(keyData, index, pemPublicExponent, 0, 3);
|
||||
|
||||
index += 3;
|
||||
index += 4;//148
|
||||
byte[] pemPrivateExponent = new byte[128];
|
||||
Array.Copy(keyData, index, pemPrivateExponent, 0, 128);
|
||||
|
||||
index += 128;
|
||||
index += ((int)keyData[index + 1] == 64 ? 2 : 3);//279
|
||||
byte[] pemPrime1 = new byte[64];
|
||||
Array.Copy(keyData, index, pemPrime1, 0, 64);
|
||||
|
||||
index += 64;
|
||||
index += ((int)keyData[index + 1] == 64 ? 2 : 3);//346
|
||||
byte[] pemPrime2 = new byte[64];
|
||||
Array.Copy(keyData, index, pemPrime2, 0, 64);
|
||||
|
||||
index += 64;
|
||||
index += ((int)keyData[index + 1] == 64 ? 2 : 3);//412/413
|
||||
byte[] pemExponent1 = new byte[64];
|
||||
Array.Copy(keyData, index, pemExponent1, 0, 64);
|
||||
|
||||
index += 64;
|
||||
index += ((int)keyData[index + 1] == 64 ? 2 : 3);//479/480
|
||||
byte[] pemExponent2 = new byte[64];
|
||||
Array.Copy(keyData, index, pemExponent2, 0, 64);
|
||||
|
||||
index += 64;
|
||||
index += ((int)keyData[index + 1] == 64 ? 2 : 3);//545/546
|
||||
byte[] pemCoefficient = new byte[64];
|
||||
Array.Copy(keyData, index, pemCoefficient, 0, 64);
|
||||
|
||||
RSAParameters para = new RSAParameters();
|
||||
para.Modulus = pemModulus;
|
||||
para.Exponent = pemPublicExponent;
|
||||
para.D = pemPrivateExponent;
|
||||
para.P = pemPrime1;
|
||||
para.Q = pemPrime2;
|
||||
para.DP = pemExponent1;
|
||||
para.DQ = pemExponent2;
|
||||
para.InverseQ = pemCoefficient;
|
||||
return para;
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,277 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Text;
|
||||
|
||||
|
||||
namespace ConsoleDemo
|
||||
{
|
||||
|
||||
public class StarDemo
|
||||
{
|
||||
//私钥(与发给票通的公钥为一对)
|
||||
private static String privateKey =
|
||||
"MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIVLAoolDaE7m5oMB1ZrILHkMXMF6qmC8I/FCejz4hwBcj59H3rbtcycBEmExOJTGwexFkNgRakhqM+3uP3VybWu1GBYNmqVzggWKKzThul9VPE3+OTMlxeG4H63RsCO1//J0MoUavXMMkL3txkZBO5EtTqek182eePOV8fC3ZxpAgMBAAECgYBp4Gg3BTGrZaa2mWFmspd41lK1E/kPBrRA7vltMfPj3P47RrYvp7/js/Xv0+d0AyFQXcjaYelTbCokPMJT1nJumb2A/Cqy3yGKX3Z6QibvByBlCKK29lZkw8WVRGFIzCIXhGKdqukXf8RyqfhInqHpZ9AoY2W60bbSP6EXj/rhNQJBAL76SmpQOrnCI8Xu75di0eXBN/bE9tKsf7AgMkpFRhaU8VLbvd27U9vRWqtu67RY3sOeRMh38JZBwAIS8tp5hgcCQQCyrOS6vfXIUxKoWyvGyMyhqoLsiAdnxBKHh8tMINo0ioCbU+jc2dgPDipL0ym5nhvg5fCXZC2rvkKUltLEqq4PAkAqBf9b932EpKCkjFgyUq9nRCYhaeP6JbUPN3Z5e1bZ3zpfBjV4ViE0zJOMB6NcEvYpy2jNR/8rwRoUGsFPq8//AkAklw18RJyJuqFugsUzPznQvad0IuNJV7jnsmJqo6ur6NUvef6NA7ugUalNv9+imINjChO8HRLRQfRGk6B0D/P3AkBt54UBMtFefOLXgUdilwLdCUSw4KpbuBPw+cyWlMjcXCkj4rHoeksekyBH1GrBJkLqDMRqtVQUubuFwSzBAtlc";
|
||||
|
||||
//票通公钥(票通提供)
|
||||
private static String ptPublicKey =
|
||||
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJkx3HelhEm/U7jOCor29oHsIjCMSTyKbX5rpoAY8KDIs9mmr5Y9r+jvNJH8pK3u5gNnvleT6rQgJQW1mk0zHuPO00vy62tSA53fkSjtM+n0oC1Fkm4DRFd5qJgoP7uFQHR5OEffMjy2qIuxChY4Au0kq+6RruEgIttb7wUxy8TwIDAQAB";
|
||||
|
||||
//3DES秘钥(票通提供)
|
||||
private static String password = "lsBnINDxtct8HZB7KCMyhWSJ";
|
||||
|
||||
//请更换请求平台简称(票通提供)
|
||||
private static String platform_alias = "DEMK";
|
||||
|
||||
//请更换请求平台编码(票通提供)
|
||||
private static String platform_code = "11111111";
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
|
||||
Console.Write(PublicData.disposeResponse(Blue(), ptPublicKey, password));//蓝票接口
|
||||
|
||||
// testqueryInvoice();//查询接口
|
||||
// testGetInvoiceRepertoryInfo();//库存接口
|
||||
// testAuthWeChatCards();//插入微信卡包接口
|
||||
// testTitleInfo();//查询抬头接口
|
||||
// testGetPTBoxStatus();//查询设备状态
|
||||
// testGetQrCodeByItems();//获取开票二维码和提取码
|
||||
// testdeleteInvoiceQrCode();//作为二维码
|
||||
// testInvoiceRed();//冲红接口
|
||||
// testRegister();// 注册接口
|
||||
//string content = "{\"code\":\"0000\",\"msg\":\"处理成功\",\"sign\":\"ZjAqLXwvEEgz2+jzP/+vUWGuvhBr4N4Gg/pLLOt90sMP160SC1RrkOy6b5p1CCx3y4QYRkbqq2NmkYXpAJX5BdkoXFYUO1hF4ufUvYPmIjQvKT9JMnXt1RV0EdNLliiEowJPjjXDSlTZdthIsTXdVirCkGohzLt3b/2YU9moAM8=\",\"serialNo\":\"CTXP20181206100927n5ObjiJM\",\"content\":\"q55jwSlpLhWV7cnEgNTvm+bswSXLiOPDbw8HvqR7SKhQDWJ/x1qlcJHAOB2lYHmQmefePoaVJ4abG7O9aJwIssDZsit2a2pqNeiCWqVmKhceLAsD/IV4DAlHmwZZhb9tqqco+HDHmZlqJy9pQv478OW0UDx/X0kTbIy4au5pZvJdODh4t31o5I2HrGm1HNcykyKMDpr5D1Mx2mYsjHm95OKBAzLPKMo+o1JrotnyjlS08CbbF6CF5OPZPB8tu88g0xl1u7/3kkjgc0KEmE+bQTEF6RoLqtQ9XRdfHf+tjzLpUcfS7j/nzPcHJnU3d1PGU0NsR+QNyHvI2cfo8HLlmnL5V7GDX+iSMNKMJ8vq7lWwcHvxZjyrHRzSpmxsQJXkQN4hungnNjiNGWzJZ8FssSLLkHw3VlQVJ8mz9sugsCn3Gr/muwUG46W7AsxUqM0Oo1JrotnyjlT6yPhLDxoIzumCiet4Hf02Gxfox417aZ6Jw+BGXo/B9KtKAIlQfkV8Zen4leGaPYo+6G+NPE2a7E+g3FRb571HMiwddiHpNVYzpc/pGTxna1JDIOODExKTJPCrI47HGZGbG7O9aJwIssDZsit2a2pqTWa8x1ePRf8eLAsD/IV4DAlHmwZZhb9tqqco+HDHmZlqJy9pQv478OW0UDx/X0kTbIy4au5pZvJdODh4t31o5JaXBuJBVtYBkyKMDpr5D1Mx2mYsjHm95F0rfY1FyxjYo1JrotnyjlS08CbbF6CF5MlEbxEcfrXqIRR3QbB604fgc0KEmE+bQTEF6RoLqtQ9XRdfHf+tjzILJodvesFM/gbYzWVOUKLKkw3+uglxfg0H9K+suDCPJFRGRT6xxFCnTglsJo/q9b0bF+jHjXtpnu9+bNNgN22dfgeGCjXTZ52gwQVlALiNUkaR5cdbKH8gRWCS9TRcrE1pnHLSywkCgr2LCkRS/wEd1863dwA7HMJuY8TDqXWlYC/kkUF84Oo8kyKMDpr5D1NA31vurQ5/BHbrS0QR43dycGeIzhqufLnEBxK01e2CYnK3sqwBwwBa6qhdLFlh9DTb7pjjR5w00A/i74mi2g8Fq91vU5nj5kkoL7fsH3ChjxJwiAQA8RwGPsTnkCcQSnzqj8s+uTYMPFzmWuWd2UYP\"}";
|
||||
|
||||
//PublicData.disposeResponse(content,ptPublicKey,password);
|
||||
}
|
||||
/**
|
||||
* 注册接口
|
||||
*/
|
||||
public static string testRegister()
|
||||
{
|
||||
|
||||
String url = "http://fpkj.testnw.vpiaotong.cn/tp/openapi/register.pt";
|
||||
Hashtable map = new Hashtable();
|
||||
map.Add("taxpayerNum", platform_alias + "0003242300000"); //销方纳税人识别号
|
||||
map.Add("enterpriseName", "测试C#"); //销方企业名称
|
||||
map.Add("legalPersonName", "AA"); //法人名称
|
||||
map.Add("contactsName", "AA"); //联系人名称
|
||||
map.Add("contactsEmail", "1121@qq.com"); //联系人邮箱
|
||||
map.Add("contactsPhone", "15111111133"); //联系人手机号
|
||||
map.Add("regionCode", "11"); //地区编码
|
||||
map.Add("cityName", "海淀区"); //市(区)名
|
||||
map.Add("enterpriseAddress", "地址"); //详细地址
|
||||
// TODO 请修改为正确的图片Base64传
|
||||
map.Add("taxRegistrationCertificate", "sdddddddddddddddddddd"); //证件图片base64
|
||||
string content = ToJson.Table2Json(map);
|
||||
String builderrequest =
|
||||
PublicData.publicparam(content, platform_code, platform_alias, privateKey, password);
|
||||
string response = PostJson.Post4Json(url, builderrequest);
|
||||
|
||||
Console.Write("最终返回:" + response);
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 开具蓝票
|
||||
*/
|
||||
public static string Blue()
|
||||
{
|
||||
|
||||
string url = "http://fpkj.testnw.vpiaotong.cn/tp/openapi/invoiceBlue.pt";
|
||||
|
||||
ArrayList itemList = new ArrayList();
|
||||
Hashtable OuterMessage = new Hashtable();
|
||||
OuterMessage.Add("taxpayerNum", "500102201007206608");
|
||||
// TODO 请更换请求流水号前缀
|
||||
OuterMessage.Add("invoiceReqSerialNo", "SCPTT538117842484711");
|
||||
OuterMessage.Add("buyerName", "购买购买方名称购买方名称购买方名称");
|
||||
OuterMessage.Add("buyerAddress", "购买方地址");
|
||||
OuterMessage.Add("buyerTel", "1234-56789104");
|
||||
OuterMessage.Add("sellerBankAccount", "123456789");
|
||||
OuterMessage.Add("sellerAddress", "深圳市福田区沙头街道天安社区深南大道车是多少大所大所大多所大所大cdtuiolj");
|
||||
OuterMessage.Add("sellerTel", "17603327743");
|
||||
OuterMessage.Add("takerEmail", "767034475@qq.com");
|
||||
OuterMessage.Add("drawerName", "");
|
||||
OuterMessage.Add("casherName", "收款人Dd");
|
||||
OuterMessage.Add("reviewerName", "复核人Bb");
|
||||
OuterMessage.Add("takerName", "");
|
||||
OuterMessage.Add("definedData", "测试数据1,测试数据2");
|
||||
Hashtable InnerMessageOne = new Hashtable();
|
||||
InnerMessageOne.Add("taxClassificationCode", "1010101020000000000"); //税收分类编码(可以按照Excel文档填写)
|
||||
InnerMessageOne.Add("quantity", "1.00"); //数量
|
||||
InnerMessageOne.Add("goodsName", "货物名称"); //货物名称
|
||||
InnerMessageOne.Add("unitPrice", "5.64"); //单价
|
||||
InnerMessageOne.Add("invoiceAmount", "5.64"); //金额
|
||||
InnerMessageOne.Add("taxRateValue", "0.16"); //税率
|
||||
InnerMessageOne.Add("includeTaxFlag", "0"); //含税标识
|
||||
Hashtable InnerMessageTwo = new Hashtable();
|
||||
InnerMessageTwo.Add("taxClassificationCode", "1010101020000000000"); //税收分类编码(可以按照Excel文档填写)
|
||||
InnerMessageTwo.Add("quantity", "1.00"); //数量
|
||||
InnerMessageTwo.Add("goodsName", "货物名称"); //货物名称
|
||||
InnerMessageTwo.Add("unitPrice", "5.64"); //单价
|
||||
InnerMessageTwo.Add("invoiceAmount", "5.64"); //金额
|
||||
InnerMessageTwo.Add("taxRateValue", "0.16"); //税率
|
||||
InnerMessageTwo.Add("includeTaxFlag", "0"); //含税标识
|
||||
itemList.Add(InnerMessageOne);
|
||||
itemList.Add(InnerMessageTwo);
|
||||
|
||||
OuterMessage.Add("itemList", itemList);
|
||||
string content = ToJson.Table2Json(OuterMessage);
|
||||
String builderrequest =
|
||||
PublicData.publicparam(content, platform_code, platform_alias, privateKey, password);
|
||||
string response = PostJson.Post4Json(url, builderrequest);
|
||||
|
||||
Console.WriteLine("最终返回:" + response);
|
||||
return response;
|
||||
|
||||
}
|
||||
/**
|
||||
* 红票开具接口
|
||||
*/
|
||||
public static void testInvoiceRed()
|
||||
{
|
||||
String url = "http://fpkj.testnw.vpiaotong.cn/tp/openapi/invoiceRed.pt";
|
||||
Hashtable map = new Hashtable();
|
||||
map.Add("taxpayerNum", "110101201702071"); //销方税号(请于要冲红的蓝票税号一致)
|
||||
// TODO 请更换请求流水号前缀
|
||||
map.Add("invoiceReqSerialNo", platform_alias + "5678902275418903"); //发票流水号 (唯一, 与蓝票发票流水号不一致)
|
||||
map.Add("invoiceCode", "150003529999"); //冲红发票的发票代码
|
||||
map.Add("invoiceNo", "61033842"); //冲红发票的发票号码
|
||||
map.Add("redReason", "冲红"); //冲红原因
|
||||
map.Add("amount", "-65.70"); //冲红金额 (要与原发票的总金额一致)
|
||||
string content = ToJson.Table2Json(map);
|
||||
String builderrequest =
|
||||
PublicData.publicparam(content, platform_code, platform_alias, privateKey, password);
|
||||
string response = PostJson.Post4Json(url, builderrequest);
|
||||
|
||||
Console.Write("最终返回:" + response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 开票二维码接口
|
||||
*/
|
||||
public static void testGetQrCodeByItems() {
|
||||
String url = "http://fpkj.testnw.vpiaotong.cn/tp/openapi/getQrCodeByItems.pt";
|
||||
Hashtable map = new Hashtable();
|
||||
map.Add("taxpayerNum", "110101201705230001"); //销方纳税人识别号
|
||||
map.Add("enterpriseName", "测试"); //销方企业名称
|
||||
map.Add("tradeNo", platform_alias + "10002001");//订单号(唯一)
|
||||
map.Add("tradeTime", "2017-06-26 09:15:54"); //交易时间
|
||||
map.Add("invoiceAmount", "100"); //发票金额(含税)
|
||||
map.Add("casherName", "收款人A"); //收款人姓名(校验规则: 中文/字母大小写/及其两者组合)
|
||||
map.Add("reviewerName", "审核人A"); //审核人姓名(校验规则: 中文/字母大小写/及其两者组合)
|
||||
map.Add("drawerName", "开票人A"); //开票人姓名(校验规则: 中文/字母大小写/及其两者组合)
|
||||
map.Add("allowInvoiceCount", "1"); //允许开票张数(非必填 默认值:1)
|
||||
// map.put("smsFlag", "false"); //是否发送短信 (非必填 默认值:false 测试环境不发送短信)
|
||||
// map.put("expireTime", ""); //有效时间 (非必填 默认值:永久有效 填写格式 yyyy-MM-dd HH:mm:ss)
|
||||
// map.put("email","XXXXX@XX.com"); //二维码发送邮箱地址(非必填)
|
||||
//其他参数见接口文档
|
||||
ArrayList list = new ArrayList();
|
||||
Hashtable listMapOne = new Hashtable();
|
||||
listMapOne.Add("itemName", "小麦"); //开票项目名
|
||||
listMapOne.Add("taxRateValue", "0.16"); //税率
|
||||
listMapOne.Add("taxClassificationCode", "1010101020000000000");//税收分类编码
|
||||
listMapOne.Add("quantity", "1"); //数量
|
||||
listMapOne.Add("unitPrice", "50"); //单价
|
||||
listMapOne.Add("invoiceItemAmount", "50"); //金额
|
||||
Hashtable listMapTwo = new Hashtable();
|
||||
listMapTwo.Add("itemName", "大米");
|
||||
listMapTwo.Add("taxRateValue", "0.16");
|
||||
listMapTwo.Add("taxClassificationCode", "1010101020000000000");
|
||||
listMapTwo.Add("quantity", "1");
|
||||
listMapTwo.Add("unitPrice", "50");
|
||||
listMapTwo.Add("invoiceItemAmount", "50");
|
||||
list.Add(listMapOne);
|
||||
list.Add(listMapTwo);
|
||||
map.Add("itemList", list);
|
||||
string content = ToJson.Table2Json(map);
|
||||
String builderrequest=PublicData.publicparam(content,platform_code,platform_alias,privateKey,password);
|
||||
string response= PostJson.Post4Json(url, builderrequest);
|
||||
|
||||
Console.Write("最终返回:"+response);
|
||||
|
||||
}
|
||||
/**
|
||||
* 作废二维码接口
|
||||
*/
|
||||
|
||||
public static void testdeleteInvoiceQrCode()
|
||||
{
|
||||
String url = "http://fpkj.testnw.vpiaotong.cn/tp/openapi/deleteInvoiceQrCode.pt";
|
||||
string content = "[{\"taxpayerNum\":\"110101201705230001\",\"enterpriseName\":\"测试\",\"tradeNo\":\"DEMO10002001\",\"tradeTime\":\"2017-06-26 09:15:54\",\"invoiceAmount\":\"100\"}]";
|
||||
String builderrequest=PublicData.publicparam(content,platform_code,platform_alias,privateKey,password);
|
||||
string response= PostJson.Post4Json(url, builderrequest);
|
||||
Console.Write("最终返回:"+response);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 查询发票
|
||||
*/
|
||||
public static void testqueryInvoice()
|
||||
{
|
||||
String url = "http://fpkj.testnw.vpiaotong.cn/tp/openapi/queryInvoice.pt";
|
||||
// String url = "http://fpkj.testnw.vpiaotong.cn/tp/openapi/queryInvoiceInfo.pt"; //查询发票票面全面信息地址
|
||||
String content =
|
||||
"{ \"taxpayerNum\": \"110101201702071\", \"invoiceReqSerialNo\": \"DEMO6678997514279636\"}";
|
||||
String builderrequest =
|
||||
PublicData.publicparam(content, platform_code, platform_alias, privateKey, password);
|
||||
string response = PostJson.Post4Json(url, builderrequest);
|
||||
Console.Write("最终返回:" + response);
|
||||
}
|
||||
|
||||
/*
|
||||
* 获取库存接口
|
||||
*/
|
||||
public static void testGetInvoiceRepertoryInfo()
|
||||
{
|
||||
String url = "http://fpkj.testnw.vpiaotong.cn/tp/openapi/getInvoiceRepertoryInfo.pt";
|
||||
String content = "{\"taxpayerNum\":\"110101201702071\",\"enterpriseName\":\"电子票测试新1\"}";
|
||||
String builderrequest =
|
||||
PublicData.publicparam(content, platform_code, platform_alias, privateKey, password);
|
||||
string response = PostJson.Post4Json(url, builderrequest);
|
||||
Console.Write("最终返回:" + response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入微信卡包接口
|
||||
*/
|
||||
public static void testAuthWeChatCards()
|
||||
{
|
||||
String url = "http://fpkj.testnw.vpiaotong.cn/tp/openapi/authWeChatCards.pt";
|
||||
String content = "{\"taxpayerNum\":\"110101201705230001\",\"invoiceReqSerialNo\":\"GAGA0000000000000009\"}";
|
||||
String builderrequest =
|
||||
PublicData.publicparam(content, platform_code, platform_alias, privateKey, password);
|
||||
string response = PostJson.Post4Json(url, builderrequest);
|
||||
Console.Write("最终返回:" + response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询发票抬头
|
||||
*/
|
||||
public static void testTitleInfo()
|
||||
{
|
||||
String url = "http://fpkj.testnw.vpiaotong.cn/tp/openapi/getInvoiceTitleInfo.pt";
|
||||
String content = "{\"enterpriseName\":\"测试\"}";
|
||||
String builderrequest =
|
||||
PublicData.publicparam(content, platform_code, platform_alias, privateKey, password);
|
||||
string response = PostJson.Post4Json(url, builderrequest);
|
||||
Console.Write("最终返回:" + response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询票通宝状态接口
|
||||
*/
|
||||
public static void testGetPTBoxStatus()
|
||||
{
|
||||
String url = "http://fpkj.testnw.vpiaotong.cn/tp/openapi/getPTBoxStatus.pt";
|
||||
String content = "{\"taxpayerNum\":\"110101201702071\",\"enterpriseName\":\"电子票测试新1\"}";
|
||||
String builderrequest =
|
||||
PublicData.publicparam(content, platform_code, platform_alias, privateKey, password);
|
||||
string response = PostJson.Post4Json(url, builderrequest);
|
||||
Console.Write("最终返回:" + response);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
namespace ConsoleDemo
|
||||
{
|
||||
internal class ToJson
|
||||
{
|
||||
public static String Table2Json(Hashtable table)
|
||||
{
|
||||
|
||||
StringBuilder jsonstr =new StringBuilder();
|
||||
jsonstr.Append("{");
|
||||
foreach (DictionaryEntry tableEntry in table)
|
||||
{
|
||||
if (tableEntry.Key=="itemList")
|
||||
{
|
||||
String liststr=list2json((ArrayList)tableEntry.Value);
|
||||
|
||||
jsonstr.Append(string.Format("\"{0}\":{1},", tableEntry.Key,liststr));
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
jsonstr.Append(string.Format("\"{0}\":\"{1}\",", tableEntry.Key, tableEntry.Value));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
jsonstr.Append("}");
|
||||
jsonstr.Remove(jsonstr.Length - 2, 1);
|
||||
return jsonstr.ToString();
|
||||
// Console.Write(jsonstr.ToString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private static String list2json(ArrayList list)
|
||||
{
|
||||
StringBuilder jsonstr =new StringBuilder();
|
||||
jsonstr.Append("[");
|
||||
foreach (Hashtable valuelist in list)
|
||||
{
|
||||
jsonstr.Append(Table2Json(valuelist)) ;
|
||||
jsonstr.Append(",");
|
||||
}
|
||||
jsonstr.Remove(jsonstr.Length - 1, 1);
|
||||
jsonstr.Append("]");
|
||||
|
||||
return jsonstr.ToString();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user