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

📄 sevrdlg.cpp

📁 机顶盒加解密工作示例代码
💻 CPP
字号:
// sevrDlg.cpp : implementation file
//

#include "stdafx.h"
#include "sevr.h"
#include "sevrDlg.h"
#include "Des.h"
#include<stdlib.h>
#include<math.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()

/////////////////////////////////////////////////////////////////////////////
// CSevrDlg dialog

CSevrDlg::CSevrDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSevrDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSevrDlg)
	m_yuanshishuju = _T("");
	m_jiamishuju = _T("");
	m_miyao = keyarray[0];
	m_jiamimiyao = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSevrDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSevrDlg)
	DDX_Text(pDX, IDC_YUANSHISHUJU, m_yuanshishuju);
	DDX_Text(pDX, IDC_JIAMISHUJU, m_jiamishuju);
	DDX_Text(pDX, IDC_MIYAO, m_miyao);
	DDX_Text(pDX, IDC_JIAMIMIYAO, m_jiamimiyao);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSevrDlg, CDialog)
	//{{AFX_MSG_MAP(CSevrDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSevrDlg message handlers

BOOL CSevrDlg::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
	
	// TODO: Add extra initialization here
	SetTimer(1,90000000,NULL);//
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSevrDlg::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 CSevrDlg::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 CSevrDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSevrDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
     UpdateData(true);//获取数据
	 CDes des;
	 int ch=0,i=0,j=0;
	 for(i=0;i<8;i++)
	{
		ch=m_miyao.GetAt(i);
		for(j=0;j<8;j++)
		{
			des.yushimiyaoerjinzhi[8*(i+1)-j]=ch%2;
			ch/=2;
		}
	}//for
	for(i=0;i<8;i++)
	{
		ch=m_yuanshishuju.GetAt(i);
		for(j=0;j<8;j++)
		{
			des.yuanshishujuerjinzhi[8*(i+1)-j]=ch%2;
			ch/=2;
		}
	}//for
	 des.subkey();
	 m_jiamishuju=des.jiami();
	 UpdateData(false);//显示加密后的数据
}

void CSevrDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	 UpdateData(true);//获取密钥
     //用RSA加密密钥,这里为了方便,我取p=11,q=23,e=3,d=147,n=253
	 int i=0,j=0;
	int n=253,a=1,e=3,b=0;
	int table1[65]={0};
	 for(i=0;i<8;i++)   
	 { 
		 //加密
		 b=m_miyao.GetAt(i);//
		  n=253;
		  a=1;
		  e=3;
         while(e!=1)
		 {
			 if(1==e%2)
			 {
				 a*=b;
				 b=(b*b)%n;
				 e/=2;
			 }
			 else
			 { b=(b*b)%n;
			   e/=2;
			 }
		 }
		 a=(a*b)%n;
		 for(j=0;j<=7;j++)
		 {
			table1[8*(i+1)-j]=a%2;
			a/=2;
		 }
       
	 }//加密密钥
	 m_jiamimiyao="";
	 for(i=1;i<=64;i++)
	 {
		 if(0==table1[i])
			 m_jiamimiyao+='0';
		 else
			 m_jiamimiyao+='1';
	 }
	 UpdateData(false);//显示加密后的密钥
}

void CSevrDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	//将加密后的数据与加密后的密钥传输出去
	CString str=(m_jiamimiyao+m_jiamishuju);
	CSocket serv;
	serv.Create(5000);
	serv.Listen();
	CSocket socksend;
	serv.Accept(socksend);
	socksend.Send((const char *)str,str.GetLength());
    socksend.Close();
}

void CSevrDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	m_miyao=keyarray[int(rand()%5)];
		UpdateData(false);
	CDialog::OnTimer(nIDEvent);
}

//DEL void CSevrDlg::OnButton4() 
//DEL {
//DEL 	// TODO: Add your control notification handler code here
//DEL 	UpdateData(true);
//DEL 	CDes des;
//DEL  	CString str1="";
//DEL 	CString str2="";
//DEL 	int a=0;
//DEL 	int i=0,j=0,ch=0;
//DEL 		int temp[9]={0};
//DEL 	for(i=0;i<64;i++)
//DEL 	{
//DEL       if('0'==m_jiamishuju.GetAt(i))
//DEL 		des.yuanshishujuerjinzhi[i+1]=0;
//DEL 	  else
//DEL        des.yuanshishujuerjinzhi[i+1]=1;
//DEL 	}
//DEL 	 for(i=0;i<8;i++)
//DEL 	{
//DEL 		ch=m_miyao.GetAt(i);
//DEL 		for(j=0;j<8;j++)
//DEL 		{
//DEL 			des.yushimiyaoerjinzhi[8*(i+1)-j]=ch%2;
//DEL 			ch/=2;
//DEL 		}
//DEL 	}//for
//DEL 	des.subkey();
//DEL 	des.changekey();
//DEL 	str1=des.jiami();
//DEL //MessageBox("Hello");
//DEL 	for(i=0;i<8;i++)
//DEL 	{    a=0;
//DEL 		for(j=0;j<8;j++)
//DEL           a+=(str1.GetAt(8*(i+1)-j-1)-'0')*(int)pow(2,j);
//DEL 		str2+=(char)a;
//DEL 	}
//DEL 	MessageBox(str2);
//DEL }

⌨️ 快捷键说明

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