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

📄 autotestview.cpp

📁 自动测试csp的程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// AutotestView.cpp : implementation of the CAutotestView class
//

#include "stdafx.h"
#include "Autotest.h"

#include "AutotestDoc.h"
#include "AutotestView.h"



/////////////////////////////////////////////////////
//for testcsp




#undef UNICODE					// ## Not Yet
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <windows.h>
#include <wincrypt.h>

#define UTILITY_BUF_SIZE	1000

#define PARAMETER1 10
#define PARAMETER2 9
#define PARAMETER3 8
#define PARAMETER4 7
#define PARAMETER5 6
#define PARAMETER6 5
#define PARAMETER7 4
#define PARAMETER8 3
#define PARAMETER9 2
#define PARAMETER10 1

///////////////////////////////////////////////////////
//for install
 
//////////////////////////////////////////////////////////////////////////

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAutotestView

IMPLEMENT_DYNCREATE(CAutotestView, CFormView)

BEGIN_MESSAGE_MAP(CAutotestView, CFormView)
	//{{AFX_MSG_MAP(CAutotestView)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAutotestView construction/destruction

CAutotestView::CAutotestView()
	: CFormView(CAutotestView::IDD)
{
	//{{AFX_DATA_INIT(CAutotestView)
	m_Str = _T("");
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CAutotestView::~CAutotestView()
{
}

void CAutotestView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAutotestView)
	DDX_Text(pDX, IDC_EDIT1, m_Str);
	//}}AFX_DATA_MAP
}

BOOL CAutotestView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CAutotestView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

}

/////////////////////////////////////////////////////////////////////////////
// CAutotestView printing

BOOL CAutotestView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CAutotestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CAutotestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CAutotestView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CAutotestView diagnostics

#ifdef _DEBUG
void CAutotestView::AssertValid() const
{
	CFormView::AssertValid();
}

void CAutotestView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CAutotestDoc* CAutotestView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAutotestDoc)));
	return (CAutotestDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CAutotestView message handlers

void CAutotestView::OnButton1() 
{


	ShellExecute(0,"open","cspsign.exe","s csp.dll csp.sig",NULL,SW_HIDE);
	 m_Str=m_Str+"Create sign file ok ! \r\n";
	
	 UpdateData(FALSE);

}

void CAutotestView::OnButton2() 

{


	

    #define CSP_PROV	"CSP Provider"
  
    CHAR szprovider[] = "SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider\\CSP Provider";

    CHAR szdef[]      = "SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider\\A Test Provider";

    CHAR szcsp[]      = "SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider\\CSP Provider";

    CHAR sztype[]     = "SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider Types\\Type 900";

    CHAR szImagePath[] = "csp.dll";

    DWORD     	    dwIgn;
    HKEY      	    hKey;
    DWORD           err;
    DWORD           dwValue;
    HANDLE          hFileSig;
    DWORD     	    NumBytesRead;
    DWORD           lpdwFileSizeHigh;
    LPVOID          lpvAddress;    
    DWORD           NumBytes;

    //
    if ((hFileSig = CreateFile("csp.sig",
                               GENERIC_READ, 0, NULL,
			       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
			       0)) != INVALID_HANDLE_VALUE)
    {
        if ((NumBytes = GetFileSize((HANDLE) hFileSig, &lpdwFileSizeHigh)) ==
                                    0xffffffff)
        {
            m_Str.Format("Install failed: Getting size of file cspsign: %x\n",
                    GetLastError());
            CloseHandle(hFileSig);
			UpdateData(FALSE);
			return;

        }

        if ((lpvAddress = VirtualAlloc(NULL, NumBytes, MEM_RESERVE |
		                                       MEM_COMMIT,
                                       PAGE_READWRITE)) == NULL)
        {
            CloseHandle(hFileSig);
            m_Str.Format("Install failed: Alloc to read uisign: %x\n",
                    GetLastError());
			UpdateData(FALSE);
			return;

        }

        if (!ReadFile((HANDLE) hFileSig, lpvAddress, NumBytes,
		      &NumBytesRead, 0))
        {

            CloseHandle(hFileSig);
            m_Str.Format("Install failed: Reading uisign: %x\n",
                    GetLastError());
            VirtualFree(lpvAddress, 0, MEM_RELEASE);
			UpdateData(FALSE);
			return;

        }

        CloseHandle(hFileSig);

        if (NumBytesRead != NumBytes)
        {
            m_Str.Format("Install failed: Bytes read doesn't match file size\n");
			UpdateData(FALSE);
			return;

        }

	//
	// Create or open in local machine for provider:
	//
        if ((err = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
                                  (const char *) szprovider,
                                  0L, "", REG_OPTION_NON_VOLATILE,
                                  KEY_ALL_ACCESS, NULL, &hKey,
                                  &dwIgn)) != ERROR_SUCCESS)
        {
            m_Str.Format("Install failed: RegCreateKeyEx\n");
			UpdateData(FALSE);
			return;
        }

	//
	// Set Image path to: scp.dll
	//
        if ((err = RegSetValueEx(hKey, "Image Path", 0L, REG_SZ, 
			(CONST BYTE*) szImagePath,strlen(szImagePath)+1)) != ERROR_SUCCESS)
        {
            m_Str.Format("Install failed: Setting Image Path value\n");
			UpdateData(FALSE);
			return;


        }

	//
	// Set Type to: Type 900
	//
        dwValue = 0x384;
        if ((err = RegSetValueEx(hKey, "Type", 0L, REG_DWORD,
                                 (CONST BYTE*) &dwValue,
                                 sizeof(DWORD))) != ERROR_SUCCESS)
        {
            m_Str.Format("Install failed: Setting Type value: %x\n", err);
			UpdateData(FALSE);
			return;


        }

	//
	// Place signature
	//
        if ((err = RegSetValueEx(hKey, "Signature", 0L, REG_BINARY, 
                                 (CONST BYTE*) lpvAddress,
                                 NumBytes)) != ERROR_SUCCESS)
        {
            m_Str.Format("Install failed: Setting Signature value for cspsign: %x\n", err);
			UpdateData(FALSE);
			return;


        }

        RegCloseKey(hKey);
        VirtualFree(lpvAddress, 0, MEM_RELEASE);

	//
	// Create or open in local machine for provider type:
	// Type 900
	//
        if ((err = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
                                  (const char *) sztype,
                                  0L, "", REG_OPTION_NON_VOLATILE,
                                  KEY_ALL_ACCESS, NULL, &hKey,
                                  &dwIgn)) != ERROR_SUCCESS)
        {
            m_Str.Format("Install failed: Registry entry existed: %x\n", err);
			UpdateData(FALSE);
			return;

        }

        if ((err = RegSetValueEx(hKey, "Name", 0L, REG_SZ, (CONST BYTE*) CSP_PROV,
                                 strlen(CSP_PROV)+1)) != ERROR_SUCCESS)
        {
            m_Str.Format("Install failed: Setting Default type: %x\n", err);
			UpdateData(FALSE);
			return;


        }

	m_Str.Format("Installed: %s\r\n", szImagePath);

    }
	else{
	

		m_Str=m_Str+("sig file not found!\r\n");
	}

	m_Str=m_Str+"Regedit ok !\r\n";
	
	UpdateData(FALSE);
	
}

void CAutotestView::OnButton3() 
{
   m_Str=m_Str+"Test begin ...... !\r\n";
   
   UpdateData(FALSE);



    HCRYPTPROV     hProv;
    HCRYPTKEY      hKey;
    HCRYPTKEY      hKey2;
    HCRYPTPROV     hHash;
    HCRYPTKEY      hPub;
    HCRYPTKEY      hUser;
    CHAR           pszMyName[64];

    m_Str=m_Str+" 01 Calling CryptAcquireContext - ";
	if (RCRYPT_FAILED(CryptAcquireContext(&hProv, pszMyName,
                          "CSP Provider", 900, 0)))
	{
        m_Str.Format("CryptAcquireConext returned error %x \r\n", GetLastError());
        m_Str=m_Str+"FAILED\r\n";
		return;
		
	}
	else
	    m_Str=m_Str+"SUCCEED\r\n";

    m_Str=m_Str+" 02 Calling CryptGenKey - ";
	if (RCRYPT_FAILED(CryptGenKey(hProv,
				      (int) PARAMETER2,
				      PARAMETER3,
				      &hKey)))
	{
		if (GetLastError() == ERROR_INVALID_PARAMETER)
	    {
		    m_Str=m_Str+"Access violation\r\n";
        }
        else
        {
		    m_Str=m_Str+"Test Failed\n";
        }
	}
	else
	{
        m_Str=m_Str+"SUCCEED\r\n";
    }

    m_Str=m_Str+" 03 Calling CryptDestroyKey - ";
	if (RCRYPT_FAILED(CryptDestroyKey(hKey)))
	{
		if (GetLastError() == ERROR_INVALID_PARAMETER)
	    {
		    m_Str=m_Str+"Access violation\r\n";
        }
        else
        {
		    m_Str=m_Str+"Test Failed\n";
        }
	}
	else
	{
        m_Str=m_Str+"SUCCEED\r\n";
    }

//
//	Create key for other calls to functions
//
    m_Str=m_Str+" 04 Calling CryptGenKey - ";
	if (RCRYPT_FAILED(CryptGenKey(hProv,
				      (int) PARAMETER2,
				      PARAMETER3,
				      &hKey)))
	{
		if (GetLastError() == ERROR_INVALID_PARAMETER)
	    {
	        m_Str=m_Str+"Access violation\r\n";
        }
        else
        {
		    m_Str=m_Str+"Test Failed\n";
        }
	}
	else
	{
        m_Str=m_Str+"SUCCEED\r\n";
    }

    m_Str=m_Str+" 05 Calling CryptSetKeyParam - ";
	if (RCRYPT_FAILED(CryptSetKeyParam(hKey,

⌨️ 快捷键说明

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