zoukankan      html  css  js  c++  java
  • c++ 基于wincrypt的DES CBC模式加解密

    des.h

     

    #pragma once
    #include <windows.h>
    #include <atlstr.h>
    #include <wincrypt.h>
    typedef struct
    {
    BLOBHEADER header;
    DWORD cbKeySize;
    BYTE rgbKeyData[8];
    }KeyBlob;
    const BYTE IV[] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
    DWORD DESEncrypt(CString data, CString password, BYTE * buffer, DWORD bufferLength);
    DWORD DESDecrypt(BYTE* buffer, DWORD bufferLength, CString password);

    ***************************************************************

    des.cpp

    #include "des.h"
    DWORD DESEncrypt(CString data, CString password, BYTE* buffer, DWORD bufferLength)
    {
    CT2CA passwd(password, CP_UTF8);
    CT2CA secret(data, CP_UTF8);
    DWORD dataLength = strlen(secret);
    if (buffer == NULL || bufferLength < dataLength + 8 - (dataLength % 8) || password.GetLength() < 8) return 0;
    memcpy(buffer, secret, dataLength);
    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, passwd, 8);
    bResult &= CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0);
    bResult &= CryptImportKey(hProv, (BYTE*)&blob, sizeof(blob), 0, 0, &hSessionKey);
    bResult &= CryptSetKeyParam(hSessionKey, KP_IV, (BYTE*)IV, 0);
    bResult &= CryptEncrypt(hSessionKey, NULL, TRUE, 0, (BYTE*)buffer, &dataLength, bufferLength);
    bResult &= CryptDestroyKey(hSessionKey);
    bResult &= CryptReleaseContext(hProv, 0);
    return bResult ? dataLength : 0;
    }
    DWORD DESDecrypt(BYTE* buffer, DWORD bufferLength, CString password)
    {
    CT2CA passwd(password, CP_UTF8);
    DWORD dataLength = bufferLength;
    if (buffer == NULL || password.GetLength() < 8) return 0;
    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, passwd, 8);
    bResult &= CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0);
    bResult &= CryptImportKey(hProv, (BYTE*)&blob, sizeof(blob), 0, 0, &hSessionKey);
    bResult &= CryptSetKeyParam(hSessionKey, KP_IV, (BYTE*)IV, 0);
    bResult &= CryptDecrypt(hSessionKey, NULL, TRUE, 0, buffer, &dataLength);
    bResult &= CryptDestroyKey(hSessionKey);
    bResult &= CryptReleaseContext(hProv, 0);
    return bResult ? dataLength : 0;
    }
    void main()
    {
    BYTE buffer[8] = { 0 };
    int dataLength = DESEncrypt("testt", "123456", buffer, sizeof(buffer));
    BYTE data[8] = { 0 };
    int bufferLength = sizeof(buffer);
    int _dataLength = DESDecrypt(buffer, bufferLength, "123456");
    char *dest = (char *)malloc((_dataLength + 1) * sizeof(char));
    memcpy(dest, buffer, _dataLength);
    dest[_dataLength] = '';//必须加结束符
    printf("result:%s", dest);
    getchar();
    }
  • 相关阅读:
    如何删除PHP数组中的元素,并且索引重排(unset,array_splice)?
    Windows下,MySQL root用户忘记密码解决方案
    MySQL 5.5开启慢查询功能
    MySQL Cluster导入数据表时报错:Got error 708 'No more attribute metadata records (increase MaxNoOfAttributes)' from NDBCLUSTER
    MySQL Cluster在线添加数据节点
    关闭Linux防火墙(iptables) 及 SELinux
    MySQL Cluster 7.3.5 集群配置实例(入门篇)
    磁盘爆满导致MySQL无法启动:Disk is full writing './mysql-bin.~rec~' (Errcode: 28). Waiting for someone to free space...
    cocos2dx 3.1创建工 mac
    跟我一起学extjs5(05--主界面上增加顶部和底部区域)
  • 原文地址:https://www.cnblogs.com/94cool/p/5801611.html
Copyright © 2011-2022 走看看