private void button1_Click(object sender, EventArgs e) { UInt32 ret = 0; UInt32 d2cSize = 0; IntPtr device_handle = IntPtr.Zero; IntPtr d2cHandlePtr = IntPtr.Zero; string license_str = ""; byte[] cert3 = SenseShield.Program.get_lockp7b_from_db(textBox2.Text);//get_lockp7b_from_db("9733c801000702079da70011000b0013"); //获得license_str********************************************************************************************* //打开控制锁 ret = Libd2c.master_open(ref device_handle); if (ret != 0) { textBox6.Text = "打开控制锁失败.0x" + ret.ToString("X8") + "请检查控制锁是否插入或者被占用"; goto Final; } byte[] psd = System.Text.ASCIIEncoding.Default.GetBytes(pin); ret = Libd2c.master_pin_verify(device_handle, 0, psd, (UInt32)psd.Length); if (ret != 0) { textBox6.Text = "校验PIN码失败0x{0:X8}0x" + ret.ToString("X8"); goto Final; } //指定锁号签发许可 byte[] accountIdBytes = SenseShield.Libd2cCommon.HexStrToBytes(textBox2.Text);//HexStrToBytes(textBox2.Text); UInt32 accountIdSize = (UInt32)(textBox2.Text.Length / 2); //生成d2cHandlePtr ret = Libd2c.d2c_lic_new(device_handle, ref d2cHandlePtr, Libd2c.ACCOUNT_TYPE.NONE, accountIdBytes, accountIdSize, cert3, (uint)cert3.Length); if (ret != 0) { textBox6.Text = "创建d2c失败。0x" + ret.ToString("X8"); goto Final; } string strLicJson = JsonConvert.SerializeObject(licJsonObj); string strLicDesc = strLicJson; ret = Libd2c.d2c_add_lic(d2cHandlePtr, strLicJson, strLicDesc, null); if (ret != SSErrCode.SS_OK) { textBox6.Text = "向d2c中添加内容失败。0x" + ret.ToString("X8") + "请检查许可操作、锁号等参数是否正确"; goto Final; } ret = Libd2c.d2c_get(d2cHandlePtr, null, 0, ref d2cSize); if (ret != 4) //SS_ERROR_INSUFFICIENT_BUFFER = 4 缓冲区不足 { textBox6.Text = "缓冲区不足。0x" + ret.ToString("X8"); goto Final; } Final: byte [] d2c_buf = new byte[d2cSize]; ret = Libd2c.d2c_get(d2cHandlePtr, d2c_buf, d2cSize, ref d2cSize); if (0 == ret) { license_str = Encoding.Default.GetString(d2c_buf); //写出文件***************************************************************************************************** SenseShield.Program.WriteD2CToFile(comboBox2.Text, textBox1.Text, license_str); textBox6.Text = "写出文件成功。"; } }
// 生成许可字符串 public static UInt32 make_license(UInt32 license_id, UInt64 start_time, UInt64 end_time, string lock_type, string data_area_raw, string lock_sn, byte[] lock_cert, ref string maked_lic_str, string lic_op, string data_area) { UInt32 ret = 0; IntPtr device_handle = IntPtr.Zero; IntPtr d2cHandlePtr = IntPtr.Zero; // Libd2c.ACCOUNT_TYPE accountType = Libd2c.ACCOUNT_TYPE.NONE; string accountID = string.Empty; string licGUID = string.Empty; UInt32 d2cSize = 0; byte[] d2c_buf = null; IntPtr errMsgPtr = IntPtr.Zero; string errMsg = string.Empty; //打开控制锁 ret = Libd2c.master_open(ref device_handle); if (ret != 0) { Console.WriteLine("打开控制锁失败。0x{0:X}", ret); return(ret); } //验证控制所pin byte[] psd = System.Text.ASCIIEncoding.Default.GetBytes(pin); ret = Libd2c.master_pin_verify(device_handle, 0, psd, (UInt32)psd.Length); if (ret != 0) { Console.WriteLine("校验PIN码失败。0x{0:08X}", ret); return(ret); } //指定锁号签发许可 accountID = lock_sn; byte[] accountIdBytes = Libd2cCommon.HexStrToBytes(accountID); UInt32 accountIdSize = (UInt32)(accountID.Length / 2); //创建d2c ret = Libd2c.d2c_lic_new(device_handle, ref d2cHandlePtr, Libd2c.ACCOUNT_TYPE.NONE, accountIdBytes, accountIdSize, lock_cert, (uint)lock_cert.Length); if (ret != SenseShield.SSErrCode.SS_OK) { Console.WriteLine("创建d2c失败。0x{0:08X}", ret); } JObject licJsonObj = make_license_json(license_id, start_time, end_time, lock_type, data_area_raw, lic_op, data_area); string strLicJson = JsonConvert.SerializeObject(licJsonObj); string strLicDesc = strLicJson; //往d2c中添加许可 ret = Libd2c.d2c_add_lic(d2cHandlePtr, strLicJson, strLicDesc, null); if (ret != SenseShield.SSErrCode.SS_OK) { Console.WriteLine("向d2c中添加内容失败。0x{0:08X}", ret); return(ret); } WriteLineGreen(strLicJson); ret = Libd2c.d2c_get(d2cHandlePtr, null, 0, ref d2cSize); if (ret != SenseShield.SSErrCode.SS_ERROR_INSUFFICIENT_BUFFER) { Console.WriteLine("缓冲区不足。0x{0:08X}", ret); return(ret); } d2c_buf = new byte[d2cSize]; ret = Libd2c.d2c_get(d2cHandlePtr, d2c_buf, d2cSize, ref d2cSize); if (ret == SenseShield.SSErrCode.SS_OK) { maked_lic_str = Encoding.Default.GetString(d2c_buf); } return(ret); }