📄 mycryptdlg.cpp
字号:
// MyCryptDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MyCrypt.h"
#include "MyCryptDlg.h"
#include "BinEncrypt.h"
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//
// FUNCTION: BtoH(char *, char *, int)
// PURPOSE: Converts ascii byte to numeric
// PARAMETERS:
// ch - ascii byte to convert
// RETURNS:
// associated numeric value
// COMMENTS:
// Will convert any hex ascii digit to its numeric counterpart.
// Puts in 0xff if not a valid hex digit.
//
unsigned char BtoH(char ch)
{
if (ch >= '0' && ch <= '9') return (ch - '0'); // Handle numerals
if (ch >= 'A' && ch <= 'F') return (ch - 'A' + 0xA); // Handle capitol hex digits
if (ch >= 'a' && ch <= 'f') return (ch - 'a' + 0xA); // Handle small hex digits
return(255);
}
//FUNCTION: AtoH(char *, char *, int)
// PURPOSE: Converts ascii string to network order hex
// PARAMETERS:
// src - pointer to input ascii string
// dest - pointer to output hex
// destlen - size of dest
// COMMENTS:
// 2 ascii bytes make a hex byte so must put 1st ascii byte of pair
// into upper nibble and 2nd ascii byte of pair into lower nibble.
//
void AtoH(char * src, char * dest, int destlen)
{
char * srcptr;
srcptr = src;
while(destlen--)
{
//*dest = BtoH(*srcptr++) << 4; // Put 1st ascii byte in upper nibble.
*dest = BtoH(*srcptr++) << 4;
*dest++ += BtoH(*srcptr++); // Add 2nd ascii byte to above.
}
}
// FUNCTION: HtoB(UCHAR)
// PURPOSE: Converts hex byte to ascii byte
// PARAMETERS:
// ch - Hex byte //
// RETURNS:
// ascii byte
// COMMENTS:
// We actually only convert a nibble since 1 byte hex = 2 bytes ascii.
// So if ch > 0xf we just return 'X'.
//
char HtoB(UCHAR ch)
{
if (ch <= 9)
return ('0' + ch); // handle decimal values
if (ch <= 0xf)
return ('A' + ch - 10); // handle hexidecimal specific values
return('X'); // Someone screwed up
}
//
// FUNCTION: HtoA(char *, char *, int)
// PURPOSE: Converts network ordered hex to ascii string
// PARAMETERS:
// src - pointer to network ordered hex
// dest - pointer to ascii string
// srclen - size of hex number in bytes
// COMMENTS:
// 1 byte hex = 2 bytes ascii so convert high order nibble with HtoB()
// then convert low order nibble. dest buffer better be 2*srclen + 1.
//
void HtoA(char * src, char * dest, int srclen)
{
char * destptr; // temp pointers
UCHAR * srcptr;
srcptr = (UCHAR *)src;
destptr = dest;
while(srclen--)
{
*destptr++ = HtoB((UCHAR)(*srcptr >> 4)); // Convert high order nibble
*destptr++ = HtoB((UCHAR)(*srcptr++ & 0x0F)); // Convert low order nibble
}
*destptr = 0; // Null terminator
}
/////////////////////////////////////////////////////////////////////////////
// CMyCryptDlg dialog
CMyCryptDlg::CMyCryptDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyCryptDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyCryptDlg)
m_edi1 = _T("");
m_edit2 = _T("");
m_edit3 = _T("");
m_strKey = _T("q%F87{gh");
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyCryptDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyCryptDlg)
DDX_Text(pDX, IDC_EDIT1, m_edi1);
DDX_Text(pDX, IDC_EDIT2, m_edit2);
DDX_Text(pDX, IDC_EDIT3, m_edit3);
DDX_Text(pDX, IDC_EDIT4, m_strKey);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyCryptDlg, CDialog)
//{{AFX_MSG_MAP(CMyCryptDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyCryptDlg message handlers
BOOL CMyCryptDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CMyCryptDlg::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();
}
}
HCURSOR CMyCryptDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
CString GetRandomString(int length)
{
int i;
unsigned char ch;
char buffer[32]="#$%$EETv23*(&";
if (length<32)
{
srand( (unsigned)time( NULL ) );
for (i = 0; i < length; i++)
{
//ch = (unsigned char)(rand() & 0xff);
ch = (unsigned char)(rand() & 0xff);
if (ch < 0x61 ) ch= 0x61;
if (ch > 0x80 ) ch= 0x80;
//ch = 0x61;
buffer[i] = ch ;
}
buffer[i]= TEXT('\0');
}
return buffer;
}
bool GetRandomString(int length, char* ret)
{
INT i;
unsigned char ch;
char buffer[6];
srand( (unsigned)time( NULL ) );
for (i = 0; i < length; i++)
{
ch = (unsigned char)(rand() & 0xff);
buffer[i] = ch;
}
buffer[i]= TEXT('\0');
for (int j=0 ;j<=i; j++)
{
*ret = buffer[j];
ret++;
}
for (j=0 ;j<=i; j++)
{
ret--;
}
return true;
}
void CMyCryptDlg::OnButton1()
{
UpdateData(true);
m_edit2.Empty();
m_edit3.Empty();
CByteArray buf;
DWORD dwLength = m_edi1.GetLength();
buf.SetSize( dwLength );
if (!CBinEncrypt::Crypt( m_edi1, m_strKey, buf.GetData(), dwLength ))
MessageBox("Some problems on crypt !");
HtoA( (char*)buf.GetData(), m_edit2.GetBuffer(dwLength*2), dwLength );
m_edit2.ReleaseBuffer();
if (!CBinEncrypt::Decrypt( buf.GetData(), dwLength, m_strKey, m_edit3.GetBuffer(dwLength) ))
MessageBox("Some problems on decrypt !");
m_edit3.ReleaseBuffer();
UpdateData(false);
}
void CMyCryptDlg::OnButton2()
{
UpdateData(true);
DWORD dwLength = m_edit2.GetLength() / 2;
CByteArray buf;
buf.SetSize( dwLength );
m_edit3.Empty();
AtoH( (char*)(LPCTSTR)m_edit2, (char*)buf.GetData(), dwLength );
CBinEncrypt::Decrypt( buf.GetData(), dwLength, m_strKey, m_edit3.GetBuffer(dwLength) );
m_edit3.ReleaseBuffer();
UpdateData(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -