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


    // 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] = '';

    }

    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;
    }

  • 相关阅读:
    测试用例怎么写
    002-利润计算
    001-无重复数字组合
    ftp上传与gui button的练习
    文件操作
    py2exe制作python可执行.exe的setup.py
    猜数字大小的游戏
    GUI简单例子学习
    新的旅程
    回车键搜索兼容性问题
  • 原文地址:https://www.cnblogs.com/cappuccino/p/8287788.html
Copyright © 2020-2023  润新知