private bool CreateUSBApp(UKeyInfo ukeyInfo) { uint iRet = 1; iRet = sm2Enroll.CreateUSBKEYApplication(1, ukeyInfo.AppName, ukeyInfo.AdminPin, ukeyInfo.AdminPinCount, ukeyInfo.UserPin, ukeyInfo.UserPinCount); if (iRet == 0) { return(true); } return(false); }
private void button3_Click(object sender, EventArgs e) { try { UKeyInfo uKeyInfo_hh = GetUkeyInfoHH(); DoFormat(uKeyInfo_hh); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private UKeyInfo GetUkeyInfoHH() { UKeyInfo ukeyInfoHT = new UKeyInfo(); ukeyInfoHT.AdminPin = "88888888"; ukeyInfoHT.AdminPinCount = 10; ukeyInfoHT.AppName = "HBCAAPPLICATION_RSA"; ukeyInfoHT.CertType = "SM2"; ukeyInfoHT.CreateFlag = 1; ukeyInfoHT.UKeyName = "HH"; ukeyInfoHT.UKeyType = "华虹GM3000"; ukeyInfoHT.UserPin = "11111111"; ukeyInfoHT.UserPinCount = 10; return(ukeyInfoHT); }
private void button5_Click(object sender, EventArgs e) { try { DoListenSKF doListen = new DoListenSKF(DoListeningSKF); System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(doListen)); t.IsBackground = true; log.Log("格式化开始!"); UKeyInfo ukeyInfoLM = GetUkeyInfoFT(); t.Start((object)ukeyInfoLM); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private UKeyInfo GetUkeyInfoFT() { UKeyInfo ukeyInfoHT = new UKeyInfo(); ukeyInfoHT.AdminPin = "88888888"; ukeyInfoHT.AdminPinCount = 10; ukeyInfoHT.AppName = "HBCAAPPLICATION_RSA"; ukeyInfoHT.CertType = "SM2"; ukeyInfoHT.CreateFlag = 1; ukeyInfoHT.UKeyName = "FT"; ukeyInfoHT.UKeyType = "FTGM3000"; ukeyInfoHT.UserPin = "11111111"; ukeyInfoHT.UserPinCount = 10; ukeyInfoHT.SKFDllName = "HBCA_ePass3000GM.dll"; ukeyInfoHT.AuthAlg = 0x00000401; return(ukeyInfoHT); }
private UKeyInfo GetUkeyInfoLM() { UKeyInfo ukeyInfoHT = new UKeyInfo(); ukeyInfoHT.AdminPin = "88888888"; ukeyInfoHT.AdminPinCount = 10; ukeyInfoHT.AppName = "HBCAAPPLICATION_RSA"; ukeyInfoHT.CertType = "SM2"; ukeyInfoHT.CreateFlag = 1; ukeyInfoHT.UKeyName = "LM"; ukeyInfoHT.UKeyType = "LMGM3000"; ukeyInfoHT.UserPin = "11111111"; ukeyInfoHT.UserPinCount = 10; ukeyInfoHT.SKFDllName = "gm3000_skf_hubca.dll"; ukeyInfoHT.AuthAlg = 0x00000401; return(ukeyInfoHT); }
private void DoFormat(UKeyInfo ukeyInfoHT) { try { uint iRet = sm2Enroll.SetUSBKeyType(ukeyInfoHT.AppName, UKeyInfo.SM2, ukeyInfoHT.UKeyType); if (iRet == 0) { GetForm g = new GetForm(GetForm1); System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(g)); t.Start(); iRet = sm2Enroll.FormatUSBKey(); //System.Threading.Thread.Sleep(2000); if (iRet == 0) { log.Log("格式化成功!"); System.Threading.Thread t1 = new System.Threading.Thread(new System.Threading.ThreadStart(g)); t1.Start(); bool bSucc = CreateUSBApp(ukeyInfoHT); if (bSucc) { log.Log("创建应用成功!"); } else { log.Log("创建应用出错,错误代码:" + sm2Enroll.GetErrorCode().ToString() + ",错误信息:" + sm2Enroll.GetErrorMsg() + ",请用UKEY对应的格式化工具格式化。"); } } else { log.Log("格式化出错,错误代码:" + sm2Enroll.GetErrorCode().ToString() + ",错误信息:" + sm2Enroll.GetErrorMsg() + ",请用UKEY对应的格式化工具格式化。"); } } else { log.Log("加载IDAEnroll出错,错误代码:" + sm2Enroll.GetErrorCode().ToString() + ",错误信息:" + sm2Enroll.GetErrorMsg()); } } catch (Exception ex) { throw; } }
private void DoListening() { StringBuilder sb = new StringBuilder("", 50); UInt32 length = 50; UInt32 eventType = 1; int iRet = -1; iRet = SKF_WaitForDevEvent(sb, ref length, ref eventType); while (true) { if (eventType == 1) { UKeyInfo ukeyInfoHT = GetUkeyInfoHT(); DoFormat(ukeyInfoHT); } Application.DoEvents(); iRet = SKF_WaitForDevEvent(sb, ref length, ref eventType); } }
private void DoListeningSKF(object uKeyInfo) { UKeyInfo ukeyConfig = null; if (uKeyInfo is UKeyInfo) { ukeyConfig = (UKeyInfo)uKeyInfo; } else { log.Log("线程参数错误"); return; } SKFObject skfObject = new SKFObject(ukeyConfig.SKFDllName); int iRet = -1; iRet = skfObject.newInstance(); if (iRet != 0) { log.Log("初始化国密库失败,错误代码:" + iRet.ToString()); return; } StringBuilder sb = new StringBuilder("", 50); UInt32 length = 1024; UInt32 eventType = 1; iRet = skfObject.skf_waitForDevEvent(sb, ref length, ref eventType); while (true) { if (eventType == 1) { DoFormatSKF(ukeyConfig, skfObject); } Application.DoEvents(); iRet = skfObject.skf_waitForDevEvent(sb, ref length, ref eventType); System.Threading.Thread.Sleep(500); } }
private void DoListening_HH() { StringBuilder sb = new StringBuilder("", 50); UInt32 length = 1024; UInt32 eventType = 1; int iRet = -1; iRet = SKF_WaitForDevEvent_HH(sb, ref length, ref eventType); while (true) { if (eventType == 1) { UKeyInfo uKeyInfo_hh = GetUkeyInfoHH(); DoFormat(uKeyInfo_hh); } else if (eventType == 2) { UKeyInfo uKeyInfo_hh = GetUkeyInfoHH(); DoFormat(uKeyInfo_hh); } Application.DoEvents(); iRet = SKF_WaitForDevEvent_HH(sb, ref length, ref eventType); } }
private int DoFormatSKF(UKeyInfo ukeyInfo, SKFObject skfObject) { try { int iRet = -1; uint length1 = 0; iRet = skfObject.skf_enumDev(true, null, ref length1); if (iRet != 0) { log.Log("枚举设备失败,错误代码:" + iRet.ToString()); return(iRet); } StringBuilder sb = new StringBuilder(); iRet = -1; iRet = skfObject.skf_enumDev(true, sb, ref length1); if (iRet != 0) { log.Log("枚举设备失败,错误代码:" + iRet.ToString()); return(iRet); } IntPtr devHandle = IntPtr.Zero; iRet = -1; iRet = skfObject.skf_connectDev(sb, ref devHandle); if (iRet != 0) { log.Log("连接设备失败,错误代码:" + iRet.ToString()); return(iRet); } byte[] randomData = new byte[32]; iRet = -1; iRet = skfObject.skf_genRandom(devHandle, randomData, 8); if (iRet != 0) { log.Log("产生随机数失败,错误代码:" + iRet.ToString()); return(iRet); } iRet = -1; byte[] data = System.Text.Encoding.ASCII.GetBytes("1234567812345678"); IntPtr hKeyHandle = IntPtr.Zero; iRet = skfObject.skf_setSymmKey(devHandle, data, SGD_SM4_ECB, ref hKeyHandle); if (iRet != 0) { log.Log("获取加密句柄失败,错误代码:" + iRet.ToString()); return(iRet); } BLOCKCIPHERPARAM encParam = new BLOCKCIPHERPARAM(); encParam.IV = new byte[32]; iRet = -1; iRet = skfObject.skf_encryptInit(hKeyHandle, encParam); if (iRet != 0) { log.Log("设置加密参数失败,错误代码:" + iRet.ToString()); return(iRet); } UInt32 encDataLen = 256; byte[] encryptResult = new byte[encDataLen]; iRet = -1; iRet = skfObject.skf_encrypt(hKeyHandle, randomData, (UInt32)16, encryptResult, ref encDataLen); if (iRet != 0) { log.Log("加密失败1,错误代码:" + iRet.ToString()); return(iRet); } iRet = -1; iRet = skfObject.skf_devAuth(devHandle, encryptResult, encDataLen); if (iRet != 0) { log.Log("设备认证失败,错误代码:" + iRet.ToString()); return(iRet); } StringBuilder appName = new StringBuilder(256); UInt32 dataLen = 0; iRet = -1; iRet = skfObject.skf_enumApplication(devHandle, null, ref dataLen); if (iRet != 0) { log.Log("枚举应用失败1,错误代码:" + iRet.ToString()); return(iRet); } iRet = -1; iRet = skfObject.skf_enumApplication(devHandle, appName, ref dataLen); if (iRet != 0) { log.Log("枚举应用失败2,错误代码:" + iRet.ToString()); return(iRet); } if (appName.ToString().Trim().Length != 0) { iRet = -1; iRet = skfObject.skf_deleteApplication(devHandle, appName); if (iRet != 0) { log.Log("删除应用失败,错误代码:" + iRet.ToString()); return(iRet); } } log.Log("格式化成功!"); iRet = -1; IntPtr hApplication = IntPtr.Zero; iRet = skfObject.skf_createApplication(devHandle, ukeyInfo.AppName, ukeyInfo.AdminPin, ukeyInfo.AdminPinCount, ukeyInfo.UserPin, ukeyInfo.UserPinCount, SECURE_USER_ACCOUNT, ref hApplication); if (iRet != 0) { log.Log("创建应用失败,错误代码:" + iRet.ToString()); return(iRet); } log.Log("创建应用成功!"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } return(0); }