⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cryptappdlg.cpp

📁 TI公司的USB键盘例子程序,包括驱动,固件,应用程序等.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                            PSP_DEVICE_INTERFACE_DATA  DeviceInfoData,
                            char *devName)
{
    PSP_DEVICE_INTERFACE_DETAIL_DATA     functionClassDeviceData = NULL;
    ULONG  predictedLength = 0;
    ULONG  requiredLength = 0;
    HANDLE hOut = INVALID_HANDLE_VALUE;

    //
    // allocate a function class device data structure to receive the
    // goods about this particular device.
    //
    SetupDiGetDeviceInterfaceDetail (
            HardwareDeviceInfo,
            DeviceInfoData,
            NULL, // probing so no output buffer yet
            0, // probing so output buffer length of zero
            &requiredLength,
            NULL); // not interested in the specific dev-node


    predictedLength = requiredLength;
    // sizeof (SP_FNCLASS_DEVICE_DATA) + 512;

    functionClassDeviceData = (PSP_DEVICE_INTERFACE_DETAIL_DATA )malloc (predictedLength);
    functionClassDeviceData->cbSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA);

    //
    // Retrieve the information from Plug and Play.
    //
    if (! SetupDiGetDeviceInterfaceDetail (
               HardwareDeviceInfo,
               DeviceInfoData,
               functionClassDeviceData,
               predictedLength,
               &requiredLength,
               NULL)) 
    {
        free( functionClassDeviceData );
        return INVALID_HANDLE_VALUE;
    }

        strcpy( devName,functionClassDeviceData->DevicePath) ;

    hOut = CreateFile (
                  functionClassDeviceData->DevicePath,
                  GENERIC_READ | GENERIC_WRITE,
                  FILE_SHARE_READ | FILE_SHARE_WRITE,
                  NULL, // no SECURITY_ATTRIBUTES structure
                  OPEN_EXISTING, // No special create flags
                  0, // No special attributes
                  NULL); // No template file

    free( functionClassDeviceData );

    return hOut;
}





void CCryptAppDlg::SetButtons()
{
    if(!m_bDevOpen)
    {
        // disable everyting
        EnableItem(IDC_ENCRYPT, FALSE);
        EnableItem(IDC_DECRYPT, FALSE);
        EnableItem(IDC_SEND_DATA, FALSE);

        SetDlgItemText(IDC_APP_INFO, "Failed to open USB device");
        return;
    }

    // get type of funciton - encrypting or decrypting
    m_dwCryptFunction = GetCryptFunction();

    EnableItem(IDC_ENCRYPT, TRUE);
    EnableItem(IDC_DECRYPT, TRUE);

    if(m_dwCryptFunction == CRYPT_FUNCTION_ENCRYPTION)
    {
        SetDlgItemText(IDC_APP_INFO, "You are currently encrypting");
        CheckRadioButton(IDC_ENCRYPT, IDC_DECRYPT, IDC_ENCRYPT);
    }
    else
    {
        SetDlgItemText(IDC_APP_INFO, "You are currently decrypting");
        CheckRadioButton(IDC_ENCRYPT, IDC_DECRYPT, IDC_DECRYPT);
    }
}


DWORD CCryptAppDlg::GetCryptFunction()
{
  DWORD dwFuncType = 0;
  DWORD dwBytesReturned;


  ::DeviceIoControl((HANDLE)m_cUsbDevice.m_hFile, IOCTL_CRYPT_GET_FUNCTION, &dwFuncType, 
                    sizeof(DWORD), &dwFuncType, sizeof(dwFuncType),
                     &dwBytesReturned, NULL);

    return dwFuncType;
}


void CCryptAppDlg::EnableItem(int nDlgItem, BOOL bEnable)
{
  CWnd *pWnd;

    pWnd = GetDlgItem(nDlgItem);

    if(pWnd != NULL)
    {
        pWnd->EnableWindow(bEnable);
    }
}


void CCryptAppDlg::OnEncrypt() 
{
  BOOL bRetOK;
  DWORD dwBytesReturned;
  DWORD dwFuncType = CRYPT_FUNCTION_ENCRYPTION;

    bRetOK = DeviceIoControl((HANDLE)m_cUsbDevice.m_hFile, IOCTL_CRYPT_SET_FUNCTION, 
                              &dwFuncType, sizeof(DWORD), &dwFuncType, sizeof(dwFuncType),
                              &dwBytesReturned, NULL);

    if(bRetOK)
    {
        SetButtons();
    }
    else
    {
        AfxMessageBox("Failed to set encryption mode.");
    }
}

void CCryptAppDlg::OnDecrypt() 
{
  BOOL bRetOK;
  DWORD dwBytesReturned;
  DWORD dwFuncType = CRYPT_FUNCTION_DECRYPTION;


    bRetOK = DeviceIoControl((HANDLE)m_cUsbDevice.m_hFile, IOCTL_CRYPT_SET_FUNCTION, 
                             &dwFuncType, sizeof(DWORD), &dwFuncType, sizeof(dwFuncType),
                             &dwBytesReturned, NULL);

    if(bRetOK)
    {
        SetButtons();
    }
    else
    {
        AfxMessageBox("Failed to set decryption mode.");
    }
}

void CCryptAppDlg::OnSendData() 
{
  BYTE InputSampleData[20];
  BYTE OutputResults[20];

    // send simple 20 bytes of data
    memset(InputSampleData, 0x41, sizeof(InputSampleData));

    // write data
    m_cUsbDevice.Write(InputSampleData, sizeof(InputSampleData));

    // read back
    m_cUsbDevice.Read(OutputResults, sizeof(OutputResults));


    // if we're decrypting then get the number of bytes
    // decrypted
    if(m_dwCryptFunction == CRYPT_FUNCTION_DECRYPTION)
    {
        GetDecryptBytes();
    }
}



bool CCryptAppDlg::ConnectToWMI()
{
  IWbemLocator *pIWbemLocator = NULL;
  HRESULT hResult;
  BSTR NameSpace;
  CString csNameSpace;
  CString csPropNameDir, csProperty;
  

    hResult = CoCreateInstance(
                           CLSID_WbemLocator,
                           NULL,
                           CLSCTX_INPROC_SERVER,
                           IID_IWbemLocator,
                           (LPVOID *)&pIWbemLocator );

    if(!SUCCEEDED(hResult))
        return false;

    csNameSpace = "root\\WMI";
        //
        // Using the locator, connect to COM in the given namespace.
        //
    NameSpace = csNameSpace.AllocSysString();

    hResult = pIWbemLocator->ConnectServer(NameSpace, 
                        NULL,   // NULL means current account, for simplicity.
                        NULL,   // NULL means current password, for simplicity.
                        0L,     // locale
                        0L,     // securityFlags
                        NULL,   // authority (domain for NTLM)
                        NULL,   // context
                        &m_pIWbemServices ); // Returned IWbemServices.




    if(hResult == WBEM_S_NO_ERROR) 
    {
           //
           // Switch the security level to IMPERSONATE so that provider(s)
           // will grant access to system-level objects, and so that
           // CALL authorization will be used.
           //

           hResult = CoSetProxyBlanket(
                            (IUnknown *)m_pIWbemServices, // proxy
                            RPC_C_AUTHN_WINNT,        // authentication service
                            RPC_C_AUTHZ_NONE,         // authorization service
                            NULL,                     // server principle name
                            RPC_C_AUTHN_LEVEL_CALL,   // authentication level
                            RPC_C_IMP_LEVEL_IMPERSONATE, // impersonation level
                            NULL,                     // identity of the client
                            0 );                      // capability flags

    }


    return SUCCEEDED(hResult) ? true : false;
}



void CCryptAppDlg::GetDecryptBytes()
{
  HRESULT hResult;
  VARIANTARG vtProgressBytesVal;
  IEnumWbemClassObject *pEnumWbeObj;
  IWbemClassObject *pIWbemClassObj;
  CString csPropNameDir;
  ULONG ulNumReturned;
  CIMTYPE CimType = 0;
  LONG Flavor = 0;


    if(m_pIWbemServices == NULL)
        return;

    VariantInit(&vtProgressBytesVal);

        // now get pointer to enumerator, to enumerate
    // through classes
    csPropNameDir = "USBCryptInformation";  //"MSNdis";  //


    hResult = m_pIWbemServices->CreateInstanceEnum(csPropNameDir.AllocSysString(),
                                            WBEM_FLAG_DIRECT_READ,
                                            NULL, &pEnumWbeObj);

    if(!SUCCEEDED(hResult))
        return;

    // we'll just enum the first instance
    ulNumReturned = 0;
    hResult = pEnumWbeObj->Next(2000, 1, &pIWbemClassObj,
                                &ulNumReturned);

    if(!SUCCEEDED(hResult) || ulNumReturned == 0)
        return;



    hResult = pIWbemClassObj->Get(L"DecryptByteCount",  0, &vtProgressBytesVal,
                                    &CimType, &Flavor);

    if(SUCCEEDED(hResult))
    {
        SetDlgItemInt(IDC_PROGRESS, vtProgressBytesVal.ulVal, FALSE);
        //AfxMessageBox("Got decrypt byte count!!");
    }

    // release references
    pIWbemClassObj->Release();
    pEnumWbeObj->Release();


#ifdef _DEBUG
    if(!SUCCEEDED(hResult))
        AfxMessageBox("ERROR, failed to get decryption byte progress");
#endif
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -