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

c++ 基于wincrypt的DES CBC模式加解密的更多相关文章

  1. python 实现 DES CBC模式加解密

    # -*- coding=utf-8-*- from Crypto.Cipher import DES import base64 """ des cbc加密算法 pad ...

  2. python 实现 AES CBC模式加解密

    AES加密方式有五种:ECB, CBC, CTR, CFB, OFB 从安全性角度推荐CBC加密方法,本文介绍了CBC,ECB两种加密方法的python实现 python 在 Windows下使用AE ...

  3. python 实现 AES ECB模式加解密

    AES ECB模式加解密使用cryptopp完成AES的ECB模式进行加解密. AES加密数据块分组长度必须为128比特,密钥长度可以是128比特.192比特.256比特中的任意一个.(8比特 == ...

  4. golang 3des/ecb/cbc/pkcs5 加解密

    本人新手,参考文档: http://blog.studygolang.com/2013/01/go%E5%8A%A0%E5%AF%86%E8%A7%A3%E5%AF%86%E4%B9%8Bdes/ h ...

  5. Android DES加密的CBC模式加密解密和ECB模式加密解密

    DES加密共有四种模式:电子密码本模式(ECB).加密分组链接模式(CBC).加密反馈模式(CFB)和输出反馈模式(OFB). CBC模式加密: import java.security.Key; i ...

  6. AES CBC/CTR 加解密原理

    So, lets look at how CBC works first. The following picture shows the encryption when using CBC (in ...

  7. Java 使用AES/CBC/PKCS7Padding 加解密字符串

    介于java 不支持PKCS7Padding,只支持PKCS5Padding 但是PKCS7Padding 和 PKCS5Padding 没有什么区别要实现在java端用PKCS7Padding填充, ...

  8. android中的MD5、Base64、DES/3DES/ADES加解密

    MD5摘要算法: <span style="font-size:18px;">主要代码: String s = edit.getText().toString(); i ...

  9. DES,AeS加解密,MD5,SHA加密

    1.DES一共就有4个参数参与运作:明文.密文.密钥.向量.其中这4者的关系可以理解为: 密文=明文+密钥+向量: 明文=密文-密钥-向量: 为什么要向量这个参数呢?因为如果有一篇文章,有几个词重复, ...

随机推荐

  1. Android studio .9图片造成的错误总结

    前言,今天早晨遇到一个很奇葩的问题,导致我花费了很长的时间来解决.从eclipse 项目里复制过来4张.9图片,粘贴到android studio 项目里, 一运行发现报错,开始是 找不到这几张.9图 ...

  2. 安卓开发_慕课网_Fragment实现Tab(App主界面)

    学习内容来自“慕课网” 这里用Fragment来实现APP主界面 思路: 底部横向排列4个LinearLayout,每个LinearLayout包含一个图片按钮和一个文字 1.默认显示第一个功能(微信 ...

  3. cocoaPods框架管理工具使用

    前言:文中的"$"表示命令行,使用使不需要包含 cocoaPods的安装 因为cocoaPods需要用Gem进行安装,所以如果系统比较旧需要先升级Gem $ sudo gem up ...

  4. Android 图片的缩放与旋转

    本文实现Android中的图片的缩放效果 首先设计布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res ...

  5. Xcode cannot launch because the device is locked.

    When you plug in your iPhone, it will ask you to trust the computer. If you already trust and unlock ...

  6. NSArray Sort 排序

    打算实现tableview的生序降序排序 ```js NSArray * rs= [oneArray sortedArrayUsingComparator:NSComparisonResult(RFI ...

  7. rsa && sha1 js code

    jsbn.js /* * Copyright (c) 2003-2005 Tom Wu * All Rights Reserved. * * Permission is hereby granted, ...

  8. 使用PL/SQL编写存储过程访问数据库

    一.实验目的 熟悉使用存储过程来进行数据库应用程序的设计. 二.实验内容 对学生-课程数据库,编写存储过程,完成下面功能: 1.统计离散数学的成绩分布情况,即按照各分数段统计人数: 2.统计任意一门课 ...

  9. JavaScript Patterns 7.1 Singleton

    7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...

  10. php 页面传递数组元素

    前台页面的表单中添加多个input元素,如下: <form action="a.php">  <input type="text" name= ...