博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++ 基于wincrypt的DES CBC模式加解密
阅读量:6421 次
发布时间:2019-06-23

本文共 2819 字,大约阅读时间需要 9 分钟。

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

 

转载地址:http://rjlra.baihongyu.com/

你可能感兴趣的文章
使用putty远程连接linux
查看>>
【comparator, comparable】小总结
查看>>
计算机常识 - 收藏集 - 掘金
查看>>
MySQL主从复制读写分离及奇怪的问题
查看>>
Spark学习笔记之相关记录
查看>>
Node 版本管理
查看>>
input的行数自动增减
查看>>
.pyc 想到的一些问题
查看>>
k个最大的数及变种小结
查看>>
【5+】跨webview多页面 触发事件(二)
查看>>
微信开源mars源码分析1—上层samples分析
查看>>
技术攻略】php设计模式(一):简介及创建型模式
查看>>
(十五)java多线程之并发集合ArrayBlockingQueue
查看>>
js学习笔记
查看>>
JS学习笔记——闭包
查看>>
MobX
查看>>
Netty 4.1 源代码学习:线程模型
查看>>
Diomidis Spinellis:有效的调试
查看>>
从后端到前端的转变:如何选择框架?
查看>>
Git 2.19 对Diff、Branch和Grep等做了改进
查看>>