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

📄 mapcode.cpp

📁 加密解密算法 32位哈希加密 这是一个包含测试环境的工程,运行可以测试结果.
💻 CPP
字号:
// MapCode.cpp : implementation file
//

#include "stdafx.h"
#include "PrjMapCode.h"
#include "MapCode.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMapCode dialog


CMapCode::CMapCode(CWnd* pParent /*=NULL*/)
	: CDialog(CMapCode::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMapCode)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CMapCode::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMapCode)
	DDX_Control(pDX, IDC_EDIT3, m_EDIT3);
	DDX_Control(pDX, IDC_EDIT2, m_EDIT2);
	DDX_Control(pDX, IDC_EDIT1, m_EDIT1);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMapCode, CDialog)
	//{{AFX_MSG_MAP(CMapCode)
	ON_BN_CLICKED(IDOK, OnEncode)
	ON_BN_CLICKED(IDC_BUTTON1, OnDeCode)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMapCode message handlers

void CMapCode::OnEncode() 
{
	// TODO: Add your control notification handler code here
	CString strSrc;
	m_EDIT1.GetWindowText(strSrc);
	strSrc.TrimLeft();
	strSrc.TrimRight();
	int count = strSrc.GetLength();
	char *pSrc = (char *)malloc(count);
	for(int i = 0; i < count; i++)
	{
		pSrc[i] = strSrc[i];
	}
	int sum = ((count/5)+1)*8 +1;
	if (count%5 == 0)
		sum = (count/5)*8 +1;;
	char *edst = (char *)malloc(sum);;
	Encode(pSrc,edst,count);
	edst[sum-1] = '\0';
	CString result = edst;
	m_EDIT2.SetWindowText(result);

}


void CMapCode::OnDeCode() 
{
	// TODO: Add your control notification handler code here
	CString strSrc;
	m_EDIT2.GetWindowText(strSrc);
	strSrc.TrimLeft();
	strSrc.TrimRight();

	int count = strSrc.GetLength();
	char *pSrc2 = (char *)malloc(count+1);
	for(int i = 0; i < count; i++)
	{
		pSrc2[i] = strSrc[i];
	}

	char *edst = (char *)malloc(count+1);
	for(i = 0; i < count; i++)
	{
		edst[i] = ' ';
	}
	edst[i] = '\0';

	Decode(pSrc2, edst,count);

	CString result = edst;
	int summ = result.GetLength();
	result.TrimRight();
	m_EDIT3.SetWindowText(result);
}

//char*  ch32="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef"; 
char * ch32 = "abcdefghijklmnopqrstuvwxyz012345";
char * CMapCode::Encode(char *src, char *edst, long srclen)
// Base32 编码
{
	int n,buflen,i,j;
	int pading = 0;
	char *buf = NULL;
	char *dst = NULL; 

//	strlwr(src);
	buflen = srclen;
	n = srclen;

	if((n%5) != 0)  /* pad with '=' by using a temp buffer */
	{
		pading = 1;
		buflen = n + (5-n%5);
		buf = (char *)malloc(buflen + 1);
		memset(buf,0,buflen + 1);
		memcpy(buf,src,n);
		for(i = 0; i < (5-n%5); i++)
			buf[n+i] = '=';
	}
	else
	{
		buf = (char *)malloc(buflen + 1);
		memset(buf,0,buflen + 1);
		memcpy(buf,src,n);
	}

	dst = (char *)malloc((buflen*8)/5 + 1);
	memset(dst,0,(buflen*8)/5 + 1);
	for(i = 0, j = 0; i < buflen; i += 5,j += 8)
	{
		dst[j] = (buf[i]&0xF8)>>3;
		dst[j+1] = ((buf[i]&0x07)<<2) + ((buf[i+1]&0xC0)>>6);
		dst[j+2] = (buf[i+1]&0x3E)>>1;
		dst[j+3] = ((buf[i+1]&0x01)<<4) + ((buf[i+2]&0xF0)>>4);
		dst[j+4] = ((buf[i+2]&0x0F)<<1) + ((buf[i+3]&0x80)>>7);
		dst[j+5] = (buf[i+3]&0x7C)>>2;
		dst[j+6] = ((buf[i+3]&0x03)<<3) + ((buf[i+4]&0xE0)>>5);
		dst[j+7] = buf[i+4]&0x1F;

	}

	for(i = 0; i < (buflen*8)/5; i++) /* map 5 bit value to base32 ASCII character */
		dst[i] = ch32[dst[i]];
	if(pading)
		free(buf);

	memcpy(edst, dst, i*sizeof(char));
	free(dst);

	return edst;

}

char * CMapCode::Decode(char *src, char *edst,long srclen)
{	int n,i,j;
	char *p;
	char *dst; 

//	n = strlen(src);
	n = srclen;
	for(i = 0; i < n; i++) /* map base32 ASCII character to 5 bit value */
	{
		p = (char *)strchr(ch32,src[i]);
		if(!p)
			break;
		src[i] = p - ch32;
	}
	dst = (char *)malloc(n*5/8+1);
	memset(dst,0,n*5/8+1);
	for(i = 0, j = 0;i < n; i += 8, j += 5)
	{
		dst[j] = ((src[i]&0x1F)<<3) + ((src[i+1]&0x1C)>>2);
		dst[j+1] = ((src[i+1]&0x03)<<6) +((src[i+2]&0x1F)<<1) + ((src[i+3]&0x10)>>4);
		dst[j+2] = ((src[i+3]&0x0F)<<4) + ((src[i+4]&0x1E)>>1);
		dst[j+3] = ((src[i+4]&0x01)<<7) + ((src[i+5]&0x1F)<<2) + ((src[i+6]&0x18)>>3);
		dst[j+4] = ((src[i+6]&0x07)<<5) + (src[i+7]&0x1F);

	}

	memcpy(edst, dst, i*sizeof(char));
	free(dst);

	return edst;
}

⌨️ 快捷键说明

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