예제 #1
0
        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);
            }
        }
예제 #2
0
        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);
        }