• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

谢大神给的C++和C# DES加解密代码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

// CPPdesTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
//#include <windows.h>
//#include <fstream>
//#include "DES.h"

#include <stdio.h>
#include <windows.h>
#include <wincrypt.h>
#pragma comment( lib, "Advapi32.lib" )

std::string string_To_UTF8(const std::string & str)
{
int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);

wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴
ZeroMemory(pwBuf, nwLen * 2 + 2);

::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);

int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);

char * pBuf = new char[nLen + 1];
ZeroMemory(pBuf, nLen + 1);

::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

std::string retStr(pBuf);

delete[]pwBuf;
delete[]pBuf;

pwBuf = NULL;
pBuf = NULL;

return retStr;
}

//
// for DES
//

typedef struct
{
BLOBHEADER header;
DWORD cbKeySize;
BYTE rgbKeyData[8];
}KeyBlob;

LPSTR ConvertGBKToUtf8(LPSTR strGBK)
{
int len = MultiByteToWideChar(CP_ACP, 0, (LPSTR)strGBK, -1, NULL, 0);
wchar_t * wszUtf8 = new wchar_t[len + 1];
memset(wszUtf8, 0, len * 2 + 2);
MultiByteToWideChar(CP_ACP, 0, (LPSTR)strGBK, -1, wszUtf8, len);

len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL);
char *szUtf8 = new char[len + 1];
memset(szUtf8, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL, NULL);

return szUtf8;
}

void Base64Encode(BYTE *src, int src_len, BYTE *dst)
{

int i = 0, j = 0;

char base64_map[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

for (; i < src_len - src_len % 3; i += 3) {

dst[j++] = base64_map[(src[i] >> 2) & 0x3F];

dst[j++] = base64_map[((src[i] << 4) & 0x30) + ((src[i + 1] >> 4) & 0xF)];

dst[j++] = base64_map[((src[i + 1] << 2) & 0x3C) + ((src[i + 2] >> 6) & 0x3)];

dst[j++] = base64_map[src[i + 2] & 0x3F];

}

if (src_len % 3 == 1) {

dst[j++] = base64_map[(src[i] >> 2) & 0x3F];

dst[j++] = base64_map[(src[i] << 4) & 0x30];

dst[j++] = '=';

dst[j++] = '=';

}
else if (src_len % 3 == 2) {

dst[j++] = base64_map[(src[i] >> 2) & 0x3F];

dst[j++] = base64_map[((src[i] << 4) & 0x30) + ((src[i + 1] >> 4) & 0xF)];

dst[j++] = base64_map[(src[i + 1] << 2) & 0x3C];

dst[j++] = '=';

}

dst[j] = '\0';

}

DWORD DesEncrypt(char* szEncrypt, char* szKey, BYTE* szOut, DWORD nOutLen)
{
char* sz_utf8_buff = ConvertGBKToUtf8(szEncrypt);

DWORD dwEncrypt = strlen(sz_utf8_buff);

if (szOut == NULL || \
nOutLen < dwEncrypt + 8 - (dwEncrypt % 8) || \
strlen(szKey) < 8)
return 0;
memcpy(szOut, sz_utf8_buff, dwEncrypt);

//
// init
//

HCRYPTPROV hProv = NULL;
HCRYPTKEY hSessionKey = NULL;
BOOL bResult = TRUE;

KeyBlob blob;
blob.header.bType = PLAINTEXTKEYBLOB;
blob.header.bVersion = CUR_BLOB_VERSION;
blob.header.reserved = 0;
blob.header.aiKeyAlg = CALG_DES;
blob.cbKeySize = 8;
memcpy(blob.rgbKeyData, szKey, 8);

BYTE IV[9] = { 0 };
memcpy(IV, szKey, 8);

//
// start
//

CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0);
CryptImportKey(hProv, (BYTE*)&blob, sizeof(blob), 0, 0, &hSessionKey);
CryptSetKeyParam(hSessionKey, KP_IV, (BYTE*)IV, 0);

//
// Do
//

CryptEncrypt(hSessionKey, NULL, TRUE, 0, (BYTE*)szOut, &dwEncrypt, nOutLen);

//
// Clean
//

CryptDestroyKey(hSessionKey);
CryptReleaseContext(hProv, 0);
delete sz_utf8_buff;

return dwEncrypt;
}

char* Encrypt(char* sz_encrypt, char* sz_key)
{
BYTE szOut[1024] = { 0 };

//
// do Des crypt
//

DWORD dwRet = DesEncrypt(sz_encrypt, sz_key, szOut, 1024);

//
// do base64
//

int nLen = strlen((char*)szOut);
int nLenOut = nLen * 4 / 3;
BYTE* szBase64 = new BYTE[nLenOut + 1];

memset(szBase64, 0, nLenOut + 1);

Base64Encode(szOut, nLen, szBase64);

return (char*)szBase64;
}


int _tmain(int argc, _TCHAR* argv[])
{
string strParameter = "currentpage=1&pagesize=1000000&starttime=1514736000&endtime=1514822399&paystatus=";
string k = "punr8ucu";
string strRequest = "";//xlthPeJpBC4cqLKTf2zpi8I8c3vzPf606LZkwaKLpdaJ6GwXiPaVKg0SWEB4gWspMnO9st2IDJdSmNH8r1VQkpPahPkQPeb+caXgbdb0hO62GEQOV8fxrw==

strParameter = string_To_UTF8(strParameter);
k = string_To_UTF8(k);

strRequest = Encrypt("currentpage=1&pagesize=1000000&starttime=1514736000&endtime=1514822399&paystatus=", "punr8ucu");
getchar();
return 0;
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
使用C#做为游戏开发的服务器语言方案发布时间:2022-07-10
下一篇:
C# WinForm 多线程使用委托操作窗体控件发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap