Des算法C++实现

VS2013新建Win32项目,选择空项目,添加头文件和源文件,为了美观程序使用duilib界面库,有些地方需要配置一下。

参考教程:http://www.cnblogs.com/Alberl/p/3342030.html

 

Des算法是加密64位明文的。本例将输入的8位字符通过ASCII码转化为64位二进制数。若超过8个字符或不足8个字符,就填充空格至8的位数个字符,每8个加密。


DES.h:

#pragma once
#include 
#include 
using namespace DuiLib;

#ifdef _DEBUG
#   ifdef _UNICODE
#       pragma comment(lib, "DuiLib_ud.lib")
#   else
#       pragma comment(lib, "DuiLib_d.lib")
#   endif
#else
#   ifdef _UNICODE
#       pragma comment(lib, "DuiLib_u.lib")
#   else
#       pragma comment(lib, "DuiLib.lib")
#   endif
#endif

#include 
#include

const int IP_Table[65] = { 0,
58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7
};

const int Ebox[49] = { 0,
32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1
};
//PC1选位表(密钥生成置换表1)
const int PC_1[57] = { 0,
57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4
};
//PC2选位表(密钥生成置换表2)
const int PC_2[49] = { 0,
14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32
};
//左移位数表 
const int LeftMove[17] = {
	0, 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
};

const int P[33]={0,
16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25
};

int SBox[8][4][16] = {
	// S1 
	{ { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 },
	{ 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 },
	{ 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0 },
	{ 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 } },
	//S2
	{ { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10 },
	{ 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5 },
	{ 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15 },
	{ 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 } },
	//S3
	{ { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8 },
	{ 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1 },
	{ 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7 },
	{ 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 } },
	//S4
	{ { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 },
	{ 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9 },
	{ 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4 },
	{ 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 } },
	//S5
	{ { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 },
	{ 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6 },
	{ 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14 },
	{ 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 } },
	//S6
	{ { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11 },
	{ 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8 },
	{ 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6 },
	{ 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 } },
	//S7
	{ { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1 },
	{ 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6 },
	{ 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2 },
	{ 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 } },
	//S8
	{ { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7 },
	{ 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2 },
	{ 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8 },
	{ 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 } }
};
const int IP_1[65] = { 0,
40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25
};

/*void output(CDuiString str, int a[], int n)//没有界面时测试用
{
	cout << str << ":";
	for (int i = 1; i <= n; i++)
	{
		cout << a[i];
	}
	cout << endl;
}*/
void  char_to_bit(char c, int temp[])//一个字符(1字节)转化为二进制(8位)
{
	for (int i = 0; i<8; i++){
		int a = ((c >> i) & 1);
		temp[7 - i] = a;
	}
}

//字符串CDuiString转二进制int[](56位)
void str_to_bit(CDuiString str, int strbit[]){
	int len = str.GetLength();
	int i, j;
	int temp[8];
	for (i = 0; i

在main.cpp里实现界面控件的消息响应

#pragma once
#include "DES.h"
//#include 
//using namespace DuiLib;

#ifdef _DEBUG
#   ifdef _UNICODE
#       pragma comment(lib, "DuiLib_ud.lib")
#   else
#       pragma comment(lib, "DuiLib_d.lib")
#   endif
#else
#   ifdef _UNICODE
#       pragma comment(lib, "DuiLib_u.lib")
#   else
#       pragma comment(lib, "DuiLib.lib")
#   endif
#endif

class CDuiFrameWnd : public CWindowWnd, public INotifyUI
{
protected:
	CPaintManagerUI m_PaintManager;

public:
	CDuiFrameWnd() { };
	UINT GetClassStyle() const { return UI_CLASSSTYLE_DIALOG; };
	void OnFinalMessage(HWND hWnd) { delete this; };
	 
	void Init()
	{
		m_pCloseBtn = static_cast(m_PaintManager.FindControl(_T("close")));
		m_pMinBtn = static_cast(m_PaintManager.FindControl(_T("min")));
		m_pjiamiBtn = static_cast(m_PaintManager.FindControl(_T("jiami")));
		m_pjiemiBtn = static_cast(m_PaintManager.FindControl(_T("jiemi")));
		m_pkey = static_cast(m_PaintManager.FindControl(_T("key")));
		m_pmingwen = static_cast(m_PaintManager.FindControl(_T("mingwen")));
		m_pmiwen = static_cast(m_PaintManager.FindControl(_T("miwen")));
		m_pmingwen->SetFont(0);
	}
	//WindowClassName  
	LPCTSTR GetWindowClassName()const{ return _T("DUIWND"); }
	//响应控件消息  
	void Notify(TNotifyUI& msg)
	{
		if (msg.sType == _T("click")) {
			if (msg.pSender == m_pCloseBtn)
			{
				PostQuitMessage(0);
				return;
			}
			else if (msg.pSender == m_pMinBtn)
			{
				SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);
				return;
			}
			else if (msg.pSender==m_pjiamiBtn)
			{
				//if (m_pmingwen)
				if ((m_pmingwen->GetText()).IsEmpty())
				{
					MessageBox(NULL, L"输入8位明文(如:perfect!)", NULL, 0);
					return;
				}
				if ((m_pkey->GetText()).IsEmpty())
				{
					MessageBox(NULL, L"输入7位密钥(如:zjut666)", NULL, 0);
					return;
				}
				else
				{
					
					if ((m_pkey->GetText()).GetLength() < 7)
					{
						MessageBox(NULL, L"输入7位密钥(如:zjut666)", NULL, 0);
						return;
					}
					CDuiString strlong = m_pmingwen->GetText();
					CDuiString key = m_pkey->GetText();
					int strbit[65], leftbit[33], rightbit[33];
					int leftkey[29], rightkey[29], key48[50];//保存左右28位密钥
					CDuiString str;
					m_pmiwen->SetText(L"");
					int duan = ((m_pmingwen->GetText().GetLength()-1) / 8) + 1;
					for (int l = 0; l < duan; l++)
					{
						str = L"00000000";
						for (int k = 0; k < 8; k++)
						{
							if (l * 8 + k == m_pmingwen->GetText().GetLength())
							{
								str.SetAt( k,_T(' '));
							}
							else
							{
								str.SetAt(k, m_pmingwen->GetText().GetAt(l * 8 + k));
							}

						}
						str_to_bit(str, strbit);//64位明文
						ip_change(strbit);
						divisionkey(key, leftkey, rightkey);
						divisionbit(strbit, leftbit, rightbit);
						for (int number = 1; number <= 16; number++)
						{
							keymovebit(key48, leftkey, rightkey, number);//strkey 1-48位位Kn字密钥,正确
							stepbit(leftbit, rightbit, key48);
						}
						addtomi(rightbit, leftbit, strbit);//左右ip逆变换
						CDuiString mi=L"0000000000000000000000000000000000000000000000000000000000000000";
						for (int i = 1; i < 65; i++)
						{
							if (strbit[i] == 1)
								mi.SetAt(i-1, _T('1'));
							else
								mi.SetAt(i-1, _T('0'));
						}
						m_pmiwen->AppendText(mi);
					}
				}
			}
			else if (msg.pSender == m_pjiemiBtn)
			{
				if ((m_pmingwen->GetText()).IsEmpty())
				{
					MessageBox(NULL, L"输入64位密文", NULL, 0);
					return;
				}
				if ((m_pkey->GetText()).IsEmpty())
				{
					MessageBox(NULL, L"输入7位密钥(如:zjut666)", NULL, 0);
					return;
				}
				else
				{

					if ((m_pkey->GetText()).GetLength() < 7)
					{
						MessageBox(NULL, L"输入7位密钥(如:zjut666)", NULL, 0);
						return;
					}
					m_pmiwen->SetText(L"");
					CDuiString strlong = m_pmingwen->GetText();
					CDuiString key = m_pkey->GetText();
					int strbit[65], leftbit[33], rightbit[33];
					int leftkey[29], rightkey[29], key48[50];//保存左右28位密钥
					int key_t[16][49];
					CDuiString str;
					int duan = (m_pmingwen->GetText().GetLength()  / 64);
					for (int t = 0; t < duan; t++)
					{
						str = L"0000000000000000000000000000000000000000000000000000000000000000";
						for (int j = 0; j < 64; j++)
						{
							
							str.SetAt(j, m_pmingwen->GetText().GetAt(t * 64 + j));
						}
						for (int i = 1; i < 65; i++)
						{
							if (str.GetAt(i - 1) == _T('1'))
								strbit[i] = 1;
							else
								strbit[i] = 0;
						}
						ip_change(strbit);
						divisionbit(strbit, leftbit, rightbit);
						divisionkey(key, leftkey, rightkey);
						for (int number = 1; number <= 16; number++)
						{
							keymovebit(key48, leftkey, rightkey, number);//strkey 1-48位位Kn字密钥
							copyint(key_t[number - 1], key48);
						}
						for (int m = 15; m >= 0; m--)
						{
							stepbit(leftbit, rightbit, key_t[m]);
						}
						addtomi(rightbit, leftbit, strbit);
						CDuiString mi = L"00000000";
						char a[100];
						TCHAR p[100];
						int res = 0, mid = 1;
						for (int i = 1; i <= 64; i++)
						{
							if (i % 8 == 0)
							{
								res += strbit[i];

								a[i / 8 - 1] = (char)res;
								res = 0;
							}
							else{
								res += strbit[i] * pow(2, 8 - i % 8);
							}

						}
						MultiByteToWideChar(CP_ACP, 0, a, -1, p, 100);
						for (int j = 0; j<8; j++)
						{
							if (p[j]>0 && p[j] < 255)
							{
								mi.SetAt(j, p[j]);
								
							}
							else
							{
								mi.SetAt(j, _T(' '));
							}
							
						}
						m_pmiwen->AppendText(mi);



					}
					
					

				}
			}
		}	
	}
	//响应窗口消息  
	LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
		styleValue &= ~WS_CAPTION;
		::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);

		m_PaintManager.Init(m_hWnd);
		CDialogBuilder builder;
		CControlUI* pRoot = builder.Create(_T("duilib.xml"), (UINT)0, NULL, &m_PaintManager);
		ASSERT(pRoot && "Failed to parse XML");
		m_PaintManager.AttachDialog(pRoot);
		m_PaintManager.AddNotifier(this);

		Init();
		return 0;
	}

	LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		::PostQuitMessage(0L);
		bHandled = FALSE;
		return 0;
	}

	LRESULT OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		if (::IsIconic(*this)) bHandled = FALSE;
		return (wParam == 0) ? TRUE : FALSE;
	}

	LRESULT OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		return 0;
	}

	LRESULT OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		return 0;
	}

	LRESULT OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		POINT pt; pt.x = GET_X_LPARAM(lParam); pt.y = GET_Y_LPARAM(lParam);
		::ScreenToClient(*this, &pt);

		RECT rcClient;
		::GetClientRect(*this, &rcClient);

		RECT rcCaption = m_PaintManager.GetCaptionRect();
		if (pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right \
			&& pt.y >= rcCaption.top && pt.y < rcCaption.bottom) {
			CControlUI* pControl = static_cast(m_PaintManager.FindControl(pt));
			if (pControl && _tcscmp(pControl->GetClass(), DUI_CTR_BUTTON) != 0)
				return HTCAPTION;
		}

		return HTCLIENT;
	}

	LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		SIZE szRoundCorner = m_PaintManager.GetRoundCorner();
		if (!::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0)) {
			CDuiRect rcWnd;
			::GetWindowRect(*this, &rcWnd);
			rcWnd.Offset(-rcWnd.left, -rcWnd.top);
			rcWnd.right++; rcWnd.bottom++;
			HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom, szRoundCorner.cx, szRoundCorner.cy);
			::SetWindowRgn(*this, hRgn, TRUE);
			::DeleteObject(hRgn);
		}

		bHandled = FALSE;
		return 0;
	}

	LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
	{
		LRESULT lRes = 0;
		BOOL bHandled = TRUE;
		switch (uMsg) {
		case WM_CREATE:        lRes = OnCreate(uMsg, wParam, lParam, bHandled); break;
		case WM_DESTROY:       lRes = OnDestroy(uMsg, wParam, lParam, bHandled); break;
		case WM_NCACTIVATE:    lRes = OnNcActivate(uMsg, wParam, lParam, bHandled); break;
		case WM_NCCALCSIZE:    lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled); break;
		case WM_NCPAINT:       lRes = OnNcPaint(uMsg, wParam, lParam, bHandled); break;
		case WM_NCHITTEST:     lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled); break;
		case WM_SIZE:          lRes = OnSize(uMsg, wParam, lParam, bHandled); break;
		default:
			bHandled = FALSE;
		}
		if (bHandled) return lRes;
		if (m_PaintManager.MessageHandler(uMsg, wParam, lParam, lRes)) return lRes;
		return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
	}


private:
	CButtonUI* m_pCloseBtn;
	CButtonUI* m_pMinBtn;
	CButtonUI* m_pjiamiBtn;
	CButtonUI* m_pjiemiBtn;
	CRichEditUI* m_pmingwen;
	CRichEditUI* m_pmiwen;
	CEditUI* m_pkey;
};


int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	CPaintManagerUI::SetInstance(hInstance);
	CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath() );

	HRESULT Hr = ::CoInitialize(NULL);
	if (FAILED(Hr)) return 0;

	CDuiFrameWnd* pFrame = new CDuiFrameWnd();
	if (pFrame == NULL) return 0;
	pFrame->Create(NULL, NULL, UI_WNDSTYLE_DIALOG, 0);
	pFrame->CenterWindow();
	pFrame->ShowWindow(true);
	CPaintManagerUI::MessageLoop();

	::CoUninitialize();
	return 0;
}

duilib.xml的内容:



 
	
			
		

刚刚学习Duilib库,不是很懂,本文作笔记。





你可能感兴趣的:(visual,studio,win32,界面,库)