windowsSDK移位加密算法实现

来源:互联网 发布:成都华为软件工厂 编辑:程序博客网 时间:2024/05/27 02:28

昨天老师让我们用MFC写一个凯撒加密,花了一会时间完成后,又花了半天自己写了个移位加密算法,是用SDK写的。

算法部分请教了自己学ACM的同学,这里先谢谢他了。

移位算法描述:

变位加密不隐藏明文的字符,即明文的字母保持相同,但其顺序被打乱重新排列成另一种不同的格式。
1)简单变位加密。预先约定好一组数字表示密钥,将文字依次写在密钥下,再按数字次序重新组织文字实现加密,也有人喜欢将明文逆序输出作为密文。例如
密钥:5 2 4 1 6 3   (密文排列次序)
明文:信息安全技术
密文:技息全信术安

先用c++描述加密解密算法:

#include <stdio.h>      #include <string.h>      #include <iostream>       using namespace std;    char map[1001], ans[1001], fin[1001], temp[1001];      int key[1001];  //map = Proclaim     这里是写SDK的时候把名字变过去了,对照一下//ans = Key//fin = Cryptograph//lenk= Key_Length//lenm= Proclaim_Length//key = Key_Each    int main()      {          cin >> map;          cin >> ans;          int lenk = strlen(ans);          for(int i = 0; i < lenk; i++)              key[i] = ans[i] - '0';          int lenm = strlen(map);                for(int i = lenm; i < 1001; i++)              map[i] = '0';          //memset(fin, '0', sizeof(fin));解密时候加上        for(int i = 0; i < lenm; )  //这里注意一下,最后一个条件是空的!!!!!!        {              int j = 0;              for(j = 0; j < lenk; j++)                  temp[j] = map[i++];              for(j = 0; j < lenk; j++)                  i--;              for(j = 0; j < lenk; j++)                  fin[i++] = temp[key[j]-1];                  //fin[key[j]-1+i] = temp[j];解密算法          }          puts(fin);          return 0;      }  
然后是自己的SDK实现

/*------------------------------------------------------------   变位加密解密算法                    By MiiBotree  ------------------------------------------------------------*/#include <windows.h>#include <string.h>#define IDC_BUTTON1  1#define IDC_BUTTON2  2#define IDC_EDIT1    3#define IDC_EDIT2    4#define IDC_EDIT3    5#define IDC_EDIT4    6 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,                    PSTR szCmdLine, int iCmdShow){     static TCHAR szAppName[] = TEXT ("Caesar") ;     HWND         hwnd ;     MSG          msg ;     WNDCLASS     wndclass ;     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;     wndclass.lpfnWndProc   = WndProc ;     wndclass.cbClsExtra    = 0 ;     wndclass.cbWndExtra    = 0 ;     wndclass.hInstance     = hInstance ;     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;     wndclass.lpszMenuName  = NULL ;     wndclass.lpszClassName = szAppName ;     if (!RegisterClass (&wndclass))     {          MessageBox (NULL, TEXT ("This program requires Windows NT!"),                       szAppName, MB_ICONERROR) ;          return 0 ;     }          hwnd = CreateWindow (szAppName,                                            TEXT ("变位算法By MiiBotree"),   WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU |   WS_THICKFRAME | WS_MINIMIZEBOX,                                  CW_USEDEFAULT,                                        CW_USEDEFAULT,                                        700,                                       550,                                        NULL,                                                 NULL,                                                 hInstance,                                            NULL) ;                               ShowWindow (hwnd, iCmdShow) ;     UpdateWindow (hwnd) ;          while (GetMessage (&msg, NULL, 0, 0))     {          TranslateMessage (&msg) ;          DispatchMessage (&msg) ;     }     return msg.wParam ;}LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){     HDC         hdc ;     PAINTSTRUCT ps ;     RECT        rect ; HWND        hwndButton; HWND        hwndEdit; static int cxChar, cyChar;     switch (message)     {     case WM_CREATE: cxChar = LOWORD (GetDialogBaseUnits ()) ;         cyChar = HIWORD (GetDialogBaseUnits ()) ;               hwndButton = CreateWindow ( TEXT("button"),                                    TEXT("加密"),                                   WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,                                   cxChar + 420, cyChar * (1 + 2 * 1)+100,                                   10 * cxChar, 7 * cyChar / 4,                                   hwnd, (HMENU) IDC_BUTTON1,                                   ((LPCREATESTRUCT) lParam)->hInstance, NULL) ;               hwndButton = CreateWindow ( TEXT("button"),                                    TEXT("解密"),                                   WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,                                   cxChar + 420 , cyChar * (1 + 2 * 1)+200,                                   10 * cxChar, 7 * cyChar / 4,                                   hwnd, (HMENU) IDC_BUTTON2,                                   ((LPCREATESTRUCT) lParam)->hInstance, NULL) ;   hwndEdit  = CreateWindow (TEXT("edit"),NULL,WS_CHILD | WS_VISIBLE | WS_BORDER|ES_LEFT  | ES_MULTILINE  |ES_AUTOVSCROLL,cxChar+200  , cyChar * (1 + 2 * 1)+40,                                    20 * cxChar, (7 * cyChar / 4)*2,    hwnd, (HMENU)IDC_EDIT1, ((LPCREATESTRUCT) lParam)->hInstance, NULL) ;   hwndEdit  = CreateWindow (TEXT("edit"),NULL,WS_CHILD | WS_VISIBLE | WS_BORDER|ES_LEFT  | ES_MULTILINE  |ES_AUTOVSCROLL,cxChar+200 , cyChar * (1 + 2 * 1)+140,                                    20 * cxChar, (7 * cyChar / 4)*2,    hwnd, (HMENU)IDC_EDIT2, ((LPCREATESTRUCT) lParam)->hInstance, NULL) ;   hwndEdit  = CreateWindow (TEXT("edit"),NULL,WS_CHILD | WS_VISIBLE | WS_BORDER|ES_LEFT  | ES_MULTILINE |ES_AUTOVSCROLL,cxChar+200 , cyChar * (1 + 2 * 1)+240,                                    20 * cxChar, (7 * cyChar / 4)*2,    hwnd, (HMENU)IDC_EDIT3, ((LPCREATESTRUCT) lParam)->hInstance, NULL) ;   hwndEdit  = CreateWindow (TEXT("edit"),NULL,WS_CHILD | WS_VISIBLE | WS_BORDER|ES_LEFT  | ES_MULTILINE |ES_AUTOVSCROLL,cxChar+200 , cyChar * (1 + 2 * 1)+340,                                    20 * cxChar, (7 * cyChar / 4)*2,    hwnd, (HMENU)IDC_EDIT4, ((LPCREATESTRUCT) lParam)->hInstance, NULL) ;            return 0 ;               case WM_PAINT:  LOGFONT a;  a.lfHeight=25;  a.lfWidth=10;  a.lfEscapement=0;  a.lfWeight=700;  a.lfItalic=FALSE;  a.lfUnderline=FALSE;      a.lfStrikeOut=FALSE;  a.lfCharSet =GB2312_CHARSET;      hdc = BeginPaint (hwnd, &ps);           GetClientRect (hwnd, &rect);  SelectObject(hdc,CreateFontIndirect(&a));  SetTextColor(hdc,(255,255,255));  TextOut(hdc, 100, 100, L"明文", wcslen(L"明文"));  TextOut(hdc, 100, 200, L"密钥", wcslen(L"密钥"));  TextOut(hdc, 100, 300, L"密文", wcslen(L"密文") );  TextOut(hdc, 100, 400, L"明文解", wcslen(L"明文解") );  TextOut(hdc, 500, 450, L"MiiBotree制作", wcslen(L"MiiBotree制作") );          ReleaseDC(hwnd, hdc);          EndPaint (hwnd, &ps);          return 0; case WM_COMMAND: if (LOWORD (wParam == IDC_BUTTON1)) {TCHAR Proclaim[256];//明文TCHAR Cryptograph[256];//密文TCHAR Key[256];//密钥TCHAR temp[256];int Key_Length = 0,    Proclaim_Length;      //密钥的长度, 明文的长度int Key_Each[256];GetDlgItemText(hwnd, IDC_EDIT1, Proclaim, sizeof(Proclaim));GetDlgItemText(hwnd, IDC_EDIT2, Key,      sizeof(Key));Key_Length = wcslen(Key);Proclaim_Length = wcslen(Proclaim);if (Key_Length > Proclaim_Length)//判断输入是否有错误MessageBox(hwnd,TEXT("明文或者密钥输入有误,请重新输入"), TEXT("出错啦"), MB_OK);if (Key_Length == 0)MessageBox(hwnd,TEXT("明文或者密钥输入有误,请重新输入"), TEXT("出错啦"), MB_OK);if (Proclaim_Length % Key_Length != 0)//明文补全{for (int i = Proclaim_Length; i <= (Proclaim_Length / Key_Length + 1)*Key_Length-1; i++)Proclaim[i] = '0'; Proclaim[(Proclaim_Length / Key_Length + 1)*Key_Length] = NULL;}//明文补全结束for (int i = 0; i < Key_Length; i++)Key_Each[i] = Key[i] - '0'; for(int i = 0; i < Proclaim_Length;)  {  int j = 0;  for(j = 0; j < Key_Length; j++)  temp[j] = Proclaim[i++];  for(j = 0; j < Key_Length; j++)  i--;  for(j = 0; j < Key_Length; j++)  Cryptograph[i++] = temp[Key_Each[j]-1];  }  if (Proclaim_Length % Key_Length != 0)Cryptograph[(Proclaim_Length / Key_Length + 1)*Key_Length] = NULL;elseCryptograph[Proclaim_Length]=NULL;SetDlgItemText(hwnd,IDC_EDIT3,Cryptograph);}if (LOWORD (wParam == 2)){TCHAR Cryptograph[256];//密文TCHAR Key[256];//密钥TCHAR ReProclaim[256];//还原明文TCHAR temp[256];int Key_Length,    Cryptograph_Length;      //密钥的长度, 密文的长度 int Key_Each[256];GetDlgItemText(hwnd, IDC_EDIT3,Cryptograph, sizeof(Cryptograph));GetDlgItemText(hwnd, IDC_EDIT2, Key,      sizeof(Key));Key_Length = wcslen(Key);Cryptograph_Length = wcslen(Cryptograph);for (int i = 0; i < Key_Length; i++)Key_Each[i] = Key[i] - '0';memset(ReProclaim, '0', sizeof(ReProclaim)); for(int i = 0; i < Cryptograph_Length; )  {  int j = 0;  for(j = 0; j < Key_Length; j++)  temp[j] = Cryptograph[i++];  for(j = 0; j < Key_Length; j++)  i--;  for(j = 0; j < Key_Length; j++)      ReProclaim[Key_Each[j]-1+i] = temp[j]; for(j = 0; j < Key_Length; j++)  i++;  }  ReProclaim[Cryptograph_Length]=NULL;SetDlgItemText(hwnd,IDC_EDIT4,ReProclaim);} return 0;      case WM_DESTROY:          PostQuitMessage (0) ;          return 0 ;     }     return DefWindowProc (hwnd, message, wParam, lParam) ;}

最后发张效果图