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] = '\0';//必须加结束符
printf("result:%s", dest);
getchar();
}



本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/p/5801611.html,如需转载请自行联系原作者
相关文章
|
设计模式 Java uml
C++设计模式之 依赖注入模式探索
C++设计模式之 依赖注入模式探索
691 0
|
数据采集 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK设置软件触发模式(C++)
Baumer工业相机堡盟工业相机如何通过NEOAPI SDK设置软件触发模式(C++)
270 0
|
设计模式 编译器 API
【C/C++ Pimpl模式】隐藏实现细节的高效方式 (Pimpl Idiom: An Efficient Way to Hide Implementation Details)
【C/C++ Pimpl模式】隐藏实现细节的高效方式 (Pimpl Idiom: An Efficient Way to Hide Implementation Details)
1652 1
|
消息中间件 负载均衡 监控
【ZMQ PUB模式指南】深入探究ZeroMQ的PUB-SUB模式:C++编程实践、底层原理与最佳实践
【ZMQ PUB模式指南】深入探究ZeroMQ的PUB-SUB模式:C++编程实践、底层原理与最佳实践
4021 1
|
设计模式 中间件 程序员
【C/C++ 奇异递归模板模式 】C++中CRTP模式(Curiously Recurring Template Pattern)的艺术和科学
【C/C++ 奇异递归模板模式 】C++中CRTP模式(Curiously Recurring Template Pattern)的艺术和科学
974 3
|
算法 网络安全 区块链
2023/11/10学习记录-C/C++对称分组加密DES
本文介绍了对称分组加密的常见算法(如DES、3DES、AES和国密SM4)及其应用场景,包括文件和视频加密、比特币私钥加密、消息和配置项加密及SSL通信加密。文章还详细展示了如何使用异或实现一个简易的对称加密算法,并通过示例代码演示了DES算法在ECB和CBC模式下的加密和解密过程,以及如何封装DES实现CBC和ECB的PKCS7Padding分块填充。
296 4
2023/11/10学习记录-C/C++对称分组加密DES
|
算法 数据安全/隐私保护 C++
超级好用的C++实用库之Des加解密
超级好用的C++实用库之Des加解密
396 0
|
算法 编译器 程序员
深入理解C++编译模式:了解Debug和Release的区别
深入理解C++编译模式:了解Debug和Release的区别
2214 3
|
消息中间件 存储 监控
【ZeroMQ的SUB视角】深入探讨订阅者模式、C++编程实践与底层机制
【ZeroMQ的SUB视角】深入探讨订阅者模式、C++编程实践与底层机制
1590 1
|
设计模式 算法 编译器
【C/C++ PIMPL模式 】 深入探索C++中的PIMPL模式
【C/C++ PIMPL模式 】 深入探索C++中的PIMPL模式
982 0