using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace testReadCardForm
{
    public partial class jmj : Form
    {
        public jmj()
        {
            InitializeComponent();
        }

        [DllImport("SSCardDriver.dll", EntryPoint = "iReadCardBas_HSM_Step1")]
        public static extern Int32 iReadCardBas_HSM_Step1(int iType, StringBuilder outInfo);

        [DllImport("SSCardDriver.dll", EntryPoint = "iReadCardBas_HSM_Step2")]
        public static extern Int32 iReadCardBas_HSM_Step2(string keyInfo, StringBuilder outInfo);

        [DllImport("SSCardDriver.dll", EntryPoint = "iGenerateCardKey")]
        public static extern Int32 iGenerateCardKey(string inInfo, StringBuilder keyInfo);

        [DllImport("SSCardDriver.dll", EntryPoint = "iReadCardBas")]
        public static extern Int32 iReadCardBas(int iType, StringBuilder outInfo);

        //iReadCardBas_SAM，读基本信息接口函数
        //返回格式：发卡地区行政区划代码（卡识别码前6位）|社会保障号码|卡号|卡识别码|姓名|卡复位信息（仅取历史字节）|规范版本|发卡日期|卡有效期|终端机编号|终端设备号|
        //返回举例：639900|111111198101011110|X00000019|639900D15600000500BF7C7A48FB4966|张三|00814E43238697159900BF7C7A|1.00|20101001|20201001|410100813475|终端设备号|
        //返回举例：639900|111111198101011110|X00000019|639900D15600000500BF7C7A48FB4966|张三|00814E43238697159900BF7C7A|1.00|20101001|20201001|410100813475|终端设备号|
        private void button1_Click(object sender, EventArgs e)
        {
            int iType = cmbType.SelectedIndex + 1;
            StringBuilder outInfo = new StringBuilder(1024);

            long lRet = iReadCardBas(iType, outInfo);
            if (lRet != 0)
            {
                lbMessage.Text = "读卡失败:" + outInfo.ToString() + ":" + lRet.ToString();
                return;
            }
            txtSSInfo.Text = outInfo.ToString();
        }

//iReadCardBas_HSM_Step1，基于加密机的读基本信息（步骤一），具体参考文件：基于加密机读社会保障卡接口说明
//返回格式：发卡地行政区划代码|卡复位信息|算法标识|卡识别码|内部认证过程因子|内部认证鉴别所需的原始信息|外部认证过程因子|外部认证鉴别所需的原始信息|
        private void button2_Click(object sender, EventArgs e)
        {
            int iType = cmbType.SelectedIndex + 1;
            StringBuilder outInfo = new StringBuilder(1024);

            long lRet = iReadCardBas_HSM_Step1(iType, outInfo);
            if(lRet != 0)
            {
                lbMessage.Text = "读卡失败:" + outInfo.ToString() + ":" + lRet.ToString();
                return;
            }
            txtAuthInfo.Text = outInfo.ToString();
        }

//iReadCardBas_HSM_Step2 “基于加密机的读基本信息（步骤二）”，具体参考文件：基于加密机读社会保障卡接口说明
//返回格式：发卡地区行政区划代码（卡识别码前6位）|社会保障号码|卡号|卡识别码|姓名|卡复位信息（仅取历史字节）|规范版本|发卡日期|卡有效期|终端机编号|终端设备号|

        private void button3_Click(object sender, EventArgs e)
        {
            string keyInfo = txtAuthResult.Text;
            StringBuilder outInfo = new StringBuilder(1024);  
            long lRet = iReadCardBas_HSM_Step2(keyInfo, outInfo);
            if (lRet != 0)
            {
                lbMessage.Text = "读卡失败:" + outInfo.ToString() + ":" + lRet.ToString();
                return;
            }
            txtSSInfo.Text = outInfo.ToString();
        }

        private void jmj_Load(object sender, EventArgs e)
        {
            cmbType.SelectedIndex = 0;
        }
    }
}
