private void button1_Click(object sender, EventArgs e) { UInt32 ret = 0; UInt32 d2cSize = 0; IntPtr device_handle = IntPtr.Zero; IntPtr d2cHandlePtr = IntPtr.Zero; 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) { textBox1.Text = "打开控制锁失败.0x" + ret.ToString("X8"); goto Final; } byte[] psd = System.Text.ASCIIEncoding.Default.GetBytes(pin); //验证pin码 ret = Libd2c.master_pin_verify(device_handle, 0, psd, (UInt32)psd.Length); if (ret != 0) { textBox1.Text = "校验PIN码失败0x{0:X8}0x" + ret.ToString("X8"); goto Final; } UInt32 CERT_SIZE = 4028; byte[] root_ca_cert = new byte[CERT_SIZE]; //获得ca证书 ret = Libd2c.master_get_ca_cert_ex(device_handle, Libd2c.CA_TYPE.PKI_CA_TYPE_ROOT, 1, root_ca_cert, CERT_SIZE, ref CERT_SIZE); if (ret != SSErrCode.SS_OK) { textBox1.Text = "获取控制锁证书失败。0x" + ret.ToString("X8"); goto Final; } //生成一个d2c句柄 ret = Libd2c.d2c_file_new(device_handle, ref d2cHandlePtr, Libd2c.SIGN_TYPE.SIGN_TYPE_SEED, root_ca_cert, CERT_SIZE); if (ret != SSErrCode.SS_OK) { textBox1.Text = "向d2c中添加内容失败。0x" + ret.ToString("X8"); goto Final; } //将Jason添加到d2c句柄 string strLicJson = JsonConvert.SerializeObject(fileObject); ret = Libd2c.d2c_add_pkg(d2cHandlePtr, strLicJson, "reset dongle"); if (ret != SenseShield.SSErrCode.SS_OK) { textBox1.Text = "向d2c中添加内容失败。0x" + ret.ToString("X8"); goto Final; } ret = Libd2c.d2c_get(d2cHandlePtr, null, 0, ref d2cSize); if (ret != SenseShield.SSErrCode.SS_ERROR_INSUFFICIENT_BUFFER) { textBox1.Text = "缓冲区不足。0x" + ret.ToString("X8"); goto Final; } Final: byte[] d2c_buf = new byte[d2cSize]; string maked_lic_str = string.Empty; ret = Libd2c.d2c_get(d2cHandlePtr, d2c_buf, d2cSize, ref d2cSize); if (ret == SenseShield.SSErrCode.SS_OK) { maked_lic_str = Encoding.Default.GetString(d2c_buf); SenseShield.Program.WriteD2CToFile(textBox2.Text, "清空锁", maked_lic_str); textBox1.Text = "写出重置加密锁d2c文件成功"; } }
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 = "写出文件成功。"; } }
//生成写文件字符串 绑定的许可可以先填0吧 public static UInt32 make_file(string lic_op, string file_name, byte[] file_buffer, UInt32 bind_lic, ref string maked_lic_str) { 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; UInt32 CERT_SIZE = 2048; byte[] root_ca_cert = null; //打开控制锁 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); } //将字节转化16进制字符串 string str_buff = null; foreach (byte b in file_buffer) { if (b <= 15) { str_buff += "0"; } str_buff += Convert.ToString(b, 16); } JObject fileObject = new JObject(); fileObject["op"] = lic_op; fileObject["filename"] = file_name; fileObject["filetype"] = "evx"; fileObject["filebuffer"] = str_buff; fileObject["fileoffset"] = 0; //fileObject["bind_lic"] = bind_lic; string strLicJson = JsonConvert.SerializeObject(fileObject); string strLicDesc = strLicJson; //或得控制锁得CA证书 root_ca_cert = new byte[CERT_SIZE]; ret = Libd2c.master_get_ca_cert_ex(device_handle, Libd2c.CA_TYPE.PKI_CA_TYPE_ROOT, 1, root_ca_cert, CERT_SIZE, ref CERT_SIZE); if (ret != SSErrCode.SS_OK) { WriteLineRed("获取控制锁证书失败"); } //生成一个d2c句柄 ret = Libd2c.d2c_file_new(device_handle, ref d2cHandlePtr, Libd2c.SIGN_TYPE.SIGN_TYPE_SEED, root_ca_cert, CERT_SIZE); //往d2c中添加许可 //ret = Libd2c.d2c_add_lic(d2cHandlePtr, strLicJson, strLicDesc, null); ret = Libd2c.d2c_add_pkg(d2cHandlePtr, strLicJson, "seed_file_hello_sample"); WriteLineGreen(strLicJson); if (ret != SenseShield.SSErrCode.SS_OK) { Console.WriteLine("向d2c中添加内容失败。0x{0:08X}", ret); return(ret); } 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); }
//生成清空锁的d2c public static UInt32 make_clear_d2c(string lic_op, double start_time, double end_time, ref string maked_lic_str) { UInt32 ret = 0; IntPtr device_handle = IntPtr.Zero; IntPtr d2cHandlePtr = IntPtr.Zero; UInt32 d2cSize = 0; byte[] d2c_buf = null; UInt32 CERT_SIZE = 4096; byte[] root_ca_cert = new byte[CERT_SIZE]; //打开控制锁 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); } /* 重置锁: * { * "op": "reset" 操作类型:重置锁 * "not_before": UTC 时间(有效开始时间) * "not_after": UTC 时间(有效终止时间),升级包在起止时间范围内(包含起止的那一秒)可重复使用。 * } */ //生成jasion JObject fileObject = new JObject(); fileObject["op"] = "reset"; fileObject["not_before"] = 1607665408; //Unix时间戳 fileObject["not_after"] = 1607666008; //Unix时间戳 //licJsonObj["start_time"] = "=" + String.Format("{0:D}", start_time); //Unix时间戳 //licJsonObj["end_time"] = "=" + String.Format("{0:D}", end_time); //Unix时间戳 string strLicJson = JsonConvert.SerializeObject(fileObject); string strLicDesc = strLicJson; //获取控制锁证书 ret = Libd2c.master_get_ca_cert_ex(device_handle, Libd2c.CA_TYPE.PKI_CA_TYPE_ROOT, 1, root_ca_cert, CERT_SIZE, ref CERT_SIZE); if (ret != SenseShield.SSErrCode.SS_OK) { Console.WriteLine("缓冲区不足。0x{0:08X}", ret); return(ret); } //生成一个d2c句柄 ret = Libd2c.d2c_file_new(device_handle, ref d2cHandlePtr, Libd2c.SIGN_TYPE.SIGN_TYPE_SEED, root_ca_cert, CERT_SIZE); if (ret != SenseShield.SSErrCode.SS_OK) { WriteLineRed("生成d2c句柄失败"); return(ret); } //将jason 添加到d2c句柄 ret = Libd2c.d2c_add_pkg(d2cHandlePtr, strLicJson, "reset dongle"); WriteLineGreen(strLicJson); if (ret != SenseShield.SSErrCode.SS_OK) { Console.WriteLine("向d2c中添加内容失败。0x{0:08X}", ret); return(ret); } 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); }
// 生成许可字符串 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); }