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

📄 pwkeyencryptdlg.cpp

📁 HDCP Key加密程序
💻 CPP
字号:
// PWKeyEncryptDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PWKeyEncrypt.h"
#include "PWKeyEncryptDlg.h"
#include "Tokens.h"
#include "Settings.h"
#include "direct.h"
#include "errno.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPWKeyEncryptDlg dialog

CPWKeyEncryptDlg::CPWKeyEncryptDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPWKeyEncryptDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPWKeyEncryptDlg)
	m_bEncrypt = TRUE;
	m_strFileFormat = _T("HDCP_Key_%07d");
	m_strIniFile = _T("PWKeyEncrypt.ini");
	m_strKeyFile = _T("");
	m_nNumKeys = 10;
	m_nStartKey = 0;
	m_strOutputDir = _T("");
	m_strSample = _T("");
	m_strKeyInfo = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPWKeyEncryptDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPWKeyEncryptDlg)
	DDX_Control(pDX, IDOK, m_butEncrypt);
	DDX_Control(pDX, IDC_PROGRESS, m_Progress);
	DDX_Check(pDX, IDC_CHECK_ENCRYPT, m_bEncrypt);
	DDX_Text(pDX, IDC_EDIT_FILEFORMAT, m_strFileFormat);
	DDX_Text(pDX, IDC_EDIT_INIFILE, m_strIniFile);
	DDX_Text(pDX, IDC_EDIT_KEYFILE, m_strKeyFile);
	DDX_Text(pDX, IDC_EDIT_NUMKEYS, m_nNumKeys);
	DDX_Text(pDX, IDC_EDIT_STARTKEY, m_nStartKey);
	DDX_Text(pDX, IDC_EDIT_OUTPUTDIR, m_strOutputDir);
	DDX_Text(pDX, IDC_STATIC_SAMPLE, m_strSample);
	DDX_Text(pDX, IDC_STATIC_KEYINFO, m_strKeyInfo);
	//}}AFX_DATA_MAP

	BOOL bEnable = (m_nStartKey + m_nNumKeys <= m_nKeysAvailable);
	bEnable &= m_bFormatValid;

	m_butEncrypt.EnableWindow(bEnable);

}

BEGIN_MESSAGE_MAP(CPWKeyEncryptDlg, CDialog)
	//{{AFX_MSG_MAP(CPWKeyEncryptDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_EN_CHANGE(IDC_EDIT_FILEFORMAT, OnChangeEditFileformat)
	ON_EN_CHANGE(IDC_EDIT_KEYFILE, OnChangeEditKeyfile)
	ON_EN_CHANGE(IDC_EDIT_NUMKEYS, OnChangeEditNumkeys)
	ON_EN_CHANGE(IDC_EDIT_STARTKEY, OnChangeEditStartkey)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPWKeyEncryptDlg message handlers

static char szFilterIni[] = "PWKeyEncrypt Ini Files (*.ini)|*.ini|All Files |*.*||";
static char szFilterKey[] = "HDCP Key Files (*.bin)|*.bin|All Files |*.*||";

BOOL CPWKeyEncryptDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	
	UpdateFileNameSample();
	UpdateKeyFileInfo();
	UpdateData(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPWKeyEncryptDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CPWKeyEncryptDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CPWKeyEncryptDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

BOOL CPWKeyEncryptDlg::LoadIniFile()
{
	CSettings IniFile;
	if (IniFile.PrepareKeys(m_strIniFile, "Paramters") > 0)
	{
		IniFile.GetValue("start_key", m_nStartKey);
		IniFile.GetValue("number_of_keys", m_nNumKeys);
		IniFile.GetValue("key_file", m_strKeyFile);
		IniFile.GetValue("output_directory", m_strOutputDir);
		IniFile.GetValue("file_format", m_strFileFormat);
	}

	return TRUE;
}

void CPWKeyEncryptDlg::OnChangeEditFileformat() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	UpdateFileNameSample();
	UpdateData(FALSE);
}

void CPWKeyEncryptDlg::UpdateFileNameSample()
{
	BOOL bFormat = TRUE;

	//
	// Ensure that the format string contains a valid
	// format specifier for the integer argument.
	//
	int nPos = m_strFileFormat.Find("%", 0);
	int nLen = m_strFileFormat.GetLength();

	if (nPos < 0)
	{
		//
		// No format specifier found.
		//
		bFormat = FALSE;
	}
	else
	{
		//
		// Found a '%' at nPos. Check that each following
		// character up to a 'd', 'x', or 'X' is a numeric
		// character.
		//
		BOOL bCont = TRUE;
		bFormat = FALSE;

		nPos++;
		while (nPos < nLen)
		{
			//
			// First see if this is the format character.
			//
			char ch = m_strFileFormat[nPos];
			if (ch == 'd' || ch == 'x' || ch == 'X')
			{
				bFormat = TRUE;
				break;
			}

			//
			// Next, make sure it is a digit..
			//
			if (!isdigit(m_strFileFormat[nPos]))
			{
				break;
			}

			nPos++;
		}

	}

	if (bFormat)
	{
		CString File1;
		CString File2;

		File1.Format(m_strFileFormat+".hky", m_nStartKey);
		File2.Format(m_strFileFormat+".hky", (m_nStartKey+m_nNumKeys-1));

 		m_strSample.Format("Files: %s  --->  %s", File1, File2);
		m_bFormatValid = TRUE;
	}
	else
	{
		m_strSample = "Invalid Format String";
		m_bFormatValid = FALSE;
	}
}

void CPWKeyEncryptDlg::OnOK() 
{
	UpdateData(TRUE);
	EncryptKeys();
}

BOOL CreatePath(CString strPath)
{
	CTokens Tok(strPath, "\\/");
	CString strTmp;
	int nCreated = 0;
	for (int i=0; i < Tok.HowMany()-1; i++)
	{
		strTmp += Tok[i] + "\\";
		OutputDebugString(strTmp);
		int err = _mkdir(strTmp);
		if (err == -1 && errno == ENOENT)
		{
			CString strMsg;
			strMsg.Format("Could not create directory: %s", strTmp);
			AfxMessageBox(strMsg, MB_OK);
			return FALSE;
		}
		nCreated += (err=0)?1:0;
	}

	if (nCreated > 0)
	{
		CString strMsg;
		strMsg = "The following path has been created:\n\n" + strTmp;
		AfxMessageBox(strMsg);
	}

	return TRUE;
}

void CPWKeyEncryptDlg::EncryptKeys()
{
	//
	// First, make sure that everything is OK to proceed.
	//

	//
	// Check to ensure that the key file exists
	//
	m_KeyFile.SetKeyFileName(m_strKeyFile);

	if (!m_KeyFile.Exists())
	{
		CString strMsg;
		strMsg.Format("The specified key file does not exist, cannot continue.\n\n%s", m_strKeyFile);

		DisplayMessage(strMsg, MB_OK|MB_ICONEXCLAMATION);
		return;
	}
	
	//
	// Check to see if there are enough keys available to create
	// all the requested keysets.
	//
	unsigned long nKeys = 0;
	if (!m_KeyFile.GetNumberOfKeys(nKeys))
	{
		CString strMsg;
		strMsg.Format("Could not determine number of key available in key file!\n\n%s", m_strKeyFile);

		DisplayMessage(strMsg, MB_OK|MB_ICONEXCLAMATION); 
		return;
	}

	if (nKeys < (unsigned long)(m_nStartKey + m_nNumKeys))
	{
		CString strMsg;
		strMsg  = "Not enough keys sets available in the key file to generate\n";
		strMsg += "the requested number of key sets.";
		DisplayMessage(strMsg, MB_OK|MB_ICONEXCLAMATION);
		return;
	}

	int nKey = m_nStartKey;
	int nNumKeys = (m_nStartKey+m_nNumKeys);

	//SECFileSystem fs;
	CString strFileName;
	CFile tmp;

	if (m_hWnd)
	{
		m_Progress.ShowWindow(SW_SHOW);
		m_Progress.SetRange(0, (int)nNumKeys);
		m_Progress.SetStep(1);
		m_Progress.SetPos(0);
	}

	//
	// Get each key in sequence...
	//
	for (nKey = m_nStartKey; nKey < (m_nStartKey+m_nNumKeys); nKey++)
	{
		CKeySet keyset;

		m_KeyFile.LoadKeyData(nKey, &keyset);
		keyset.Encrypt();

		strFileName.Format(m_strFileFormat+".hky", nKey);

		//
		// Make sure we have a trailing slash.
		//
		if (!m_strOutputDir.IsEmpty())
		{
			m_strOutputDir.TrimRight("\\");
			m_strOutputDir += "\\";
		}

		//
		// Make sure the entire output directory path
		// exists.
		//
		CreatePath(m_strOutputDir);
		
		CString strOutputFile = m_strOutputDir+strFileName;

		if (!tmp.Open(strOutputFile, CFile::modeCreate|CFile::modeWrite))
		{
			//
			// Could not open output key file.  Abort.
			//
			break;
		}

		tmp.Write(keyset.EncryptedData, VECTOR_SIZE+KEYSET_SIZE);
		tmp.Close();

		if (m_hWnd)
		{
			m_Progress.StepIt();
		}
	}

	if (m_hWnd)
	{
		m_Progress.ShowWindow(SW_HIDE);
	}

	CString strMsg;
	strMsg.Format("Key Encrytpion is Complete!\n\n%d Keys Encrytped", nNumKeys);
	DisplayMessage(strMsg, MB_OK|MB_ICONINFORMATION);

	//
	// Now that we are done, increment the start key...
	//
	m_nStartKey += m_nNumKeys;

	if (m_hWnd)
	{
		UpdateFileNameSample();
		UpdateData(FALSE);
	}

	//
	// Save the new paraters to the ini file.
	//
	SaveIniFile();

	//
	// Don't call the base class or the application will exit...
	//
	//CDialog::OnOK();
}

void CPWKeyEncryptDlg::OnChangeEditKeyfile() 
{
	//
	// A New key file was selected. Update the keyinfo.
	//
	UpdateData(TRUE);
	UpdateKeyFileInfo();
	UpdateData(FALSE);
}

void CPWKeyEncryptDlg::UpdateKeyFileInfo()
{
	CFileStatus fs;
	CFile::GetStatus(m_strKeyFile, fs);
	if (fs.m_attribute & 0x1A)
	{
		m_strKeyInfo = "No keyfile yet...";
		return;

	}

	m_nKeysAvailable = 0;
	m_KeyFile.SetKeyFileName(m_strKeyFile);

	if (!m_KeyFile.Exists())
	{
		m_strKeyInfo = "Specified key file does not exists.";
	}
	else
	{
		unsigned long nKeys;
		if (!m_KeyFile.GetNumberOfKeys(nKeys))
		{
			m_strKeyInfo = "Cannot determine number of keys. Invalid Key File?";
		}
		else
		{
			m_strKeyInfo.Format("This file has %d keysets available.", nKeys);
			m_nKeysAvailable = nKeys;
		}
	}
}

void CPWKeyEncryptDlg::OnChangeEditNumkeys() 
{
	UpdateData(TRUE);
}

void CPWKeyEncryptDlg::OnChangeEditStartkey() 
{
	UpdateData(TRUE);
}

void CPWKeyEncryptDlg::SaveIniFile()
{
	CSettings IniFile;

	IniFile.ResetKeys(m_strIniFile, "Parameters");
	IniFile.SetValue("start_key", m_nStartKey);
	IniFile.SetValue("number_of_keys", m_nNumKeys);
	IniFile.SetValue("key_file", m_strKeyFile);
	IniFile.SetValue("output_directory", m_strOutputDir);
	IniFile.SetValue("file_format", m_strFileFormat);

	IniFile.SaveIniFile(m_strIniFile);
}

int CPWKeyEncryptDlg::DisplayMessage(CString &strMsg, UINT nType)
{
	if (m_hWnd)
	{
		return AfxMessageBox(strMsg, nType);
	}
	else
	{
		fprintf(stderr, strMsg);
		return 0;
	}

	OutputDebugString(strMsg);
}

⌨️ 快捷键说明

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