ocx中对称密钥加密解密的简单实例
来源:互联网 发布:Linux系统可以玩游戏吗 编辑:程序博客网 时间:2024/04/29 05:48
//------------------------------------------------------------------
#define _WIN32_WINNT 0x0400
#include <stdio.h>
#include <windows.h>
#include <wincrypt.h>
#include <iomanip.h>
#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
#define KEYLENGTH 0x00800000
// These additional #define statements are required.
#define ENCRYPT_ALGORITHM CALG_RC4
#define ENCRYPT_BLOCK_SIZE 8
//--------------------------------------------------------------------
=========mfc中的octx控件中的对称密钥加密解密函数代码===================
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//------------------------加密代码--------------------------------------------------
BOOL COcxCryptCtrl::encrypt(LPCTSTR szSource, LPCTSTR szDestination, LPCTSTR szPassword)
{
FILE *hSource;
FILE *hDestination;
HCRYPTPROV hCryptProv;
HCRYPTKEY hKey;
HCRYPTHASH hHash;
PBYTE pbBuffer;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;
//----------open source file-----------------------------------------
if(hSource=fopen(szSource,"rb"))
{
cout<<"the source plaintext file"<<szSource<<"is open"<<endl;
}
else
{
cout<<"error opening source file"<<endl;
return FALSE;
}
//----------open destination file----------------------------------------------------
if(hDestination=fopen(szDestination,"wb"))
{
cout<<" the destination file is open"<<endl;
}
else
{
cout<<"error opening destination file" <<endl;
return FALSE;
}
//----------以下获得一个CSP句柄------------------------------------------------------
if(CryptAcquireContext(
&hCryptProv,
NULL,
NULL,
PROV_RSA_FULL,
0
))
{
cout<<"a cryptographic provider has been acquired"<<endl;
}
else
{
if(CryptAcquireContext(
&hCryptProv,
NULL,
NULL,
PROV_RSA_FULL,
CRYPT_NEWKEYSET //创建密钥容器
))
{
cout<<"a new key container has been created "<<endl;
}
else
{
cout<<"could not create a new key container"<<endl;
return FALSE;
}
}
//----------创建一个对称密钥用于对称加密--------------------------------------------
//-------create a hash object-------------------------------------------------------
if(CryptCreateHash(
hCryptProv,
CALG_MD5,
0,
0,
&hHash
))
{
cout<<"a hash object has been created"<<endl;
}
else
{
cout<<"error during CryptCreatehash object"<<endl;
//return false;
}
//----------用输入的密码产生一个散列---------------------------------------------------------------
if(CryptHashData(
hHash,
(BYTE *)szPassword,
strlen(szPassword),
0
))
{
cout<<"the password has been addd to the hash"<<endl;
}
else
{
cout<<"error during cryptHashdata "<<endl;
return FALSE;
}
//---------通过散列生成会话密钥-------------------------------------------------------
if(CryptDeriveKey(
hCryptProv,
ENCRYPT_ALGORITHM,
hHash,
KEYLENGTH,
&hKey
))
{
cout<<"an encryption key is derived from the password hash"<<endl;
}
else
{
cout<<"error during crypt cryptDeriveKey"<<endl;
return FALSE;
}
//---------destroy the hash object---------------------------------------------------------------
CryptDestroyHash(hHash);
hHash=NULL;
//----------------------------------------------------------
// the session key is now ready
//------------------------------------------------------------
dwBlockLen=1000-1000%ENCRYPT_BLOCK_SIZE;
if(ENCRYPT_BLOCK_SIZE >1)
dwBufferLen=dwBlockLen+ENCRYPT_BLOCK_SIZE;
else
dwBufferLen=dwBlockLen;
//-----allocate memory--------------------------------------------------------
if(pbBuffer=(BYTE *)malloc(dwBufferLen))
{
cout<<"memory has been allocated for the buffer "<<endl;
}
else
{
cout<<"out of memory"<<endl;
return FALSE;
}
//--- In a do loop,encrypt the source file and write to destination file-----------------------------------------------------------
do{
//-----read up to dwBlockLen bytes from source file
dwCount=fread(pbBuffer,1,dwBlockLen,hSource);
if(ferror(hSource))
{
cout<<"error reading source file"<<endl;
return FALSE;
}
//-----加密数据-------------------------------------------
if(!CryptEncrypt(
hKey, //密钥
0, //如果同时进行散列和加密,这里传一个散列对象
feof(hSource),//如果是最后一个加密块,返回true否则返回false
0, //保留
pbBuffer,//输入保存源数据,输出保存加密数据
&dwCount,//输入被加密的数据实际长度,输出加密后的数据长度
dwBufferLen))
{
cout<<"error during cryptEncrypt"<<endl;
return FALSE;
}
//--write data to the destination file---------------------------------------------------------
fwrite(pbBuffer,1,dwCount,hDestination);
if(ferror(hDestination))
{
cout<<"error write ciphertext"<<endl;
return FALSE;
}
}while(!feof(hSource));
//-----close files----------------------------------------------------------
if(hSource)
fclose(hSource);
if(hDestination)
fclose(hDestination);
//-----free memory-------------------------------------------------------
if(pbBuffer)
free(pbBuffer);
//-- destroy session key-------------------------------------------------------
if(hKey)
CryptDestroyKey(hKey);
//----destroy hash object---------------------------------------------------------
if(hHash)
CryptDestroyHash(hHash);
//----release provider handle-------------------------------------------------------
if(hCryptProv)
CryptReleaseContext(hCryptProv,0);
return TRUE;
}
//-----------解密代码----------------------------------------------------------------
BOOL COcxCryptCtrl::decrypt(LPCTSTR szSource, LPCTSTR szDestination, LPCTSTR szPassword)
{
FILE *hSource;
FILE *hDestination;
HCRYPTPROV hCryptProv;
HCRYPTKEY hKey;
HCRYPTHASH hHash;
PBYTE pbBuffer;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;
//----------open source file-----------------------------------------
if(hSource=fopen(szSource,"rb"))
{
cout<<"the source plaintext file"<<szSource<<"is open"<<endl;
}
else
{
cout<<"error opening source file"<<endl;
return FALSE;
}
//----------open destination file----------------------------------------------------
if(hDestination=fopen(szDestination,"wb"))
{
cout<<"the destination file is opening"<<endl;
}
else
{
cout<<"error opening source file"<<endl;
return FALSE;
}
//----------以下获得一个CSP句柄------------------------------------------------------
if(CryptAcquireContext(
&hCryptProv,
NULL,
NULL,
PROV_RSA_FULL,
0
))
{
cout<<"a cryptographic provider has been acquired"<<endl;
}
else
{
if(CryptAcquireContext(
&hCryptProv,
NULL,
NULL,
PROV_RSA_FULL,
CRYPT_NEWKEYSET //创建密钥容器
))
{
cout<<"a new key container has been created "<<endl;
}
else
{
cout<<"could not create a new key container"<<endl;
return FALSE;
}
}
//----------创建一个对称密钥用于对称加密--------------------------------------------
//-------create a hash object-------------------------------------------------------
if(CryptCreateHash(
hCryptProv,
CALG_MD5,
0,
0,
&hHash
))
{
cout<<"a hash object has been created"<<endl;
}
else
{
cout<<"error during CryptCreatehash object"<<endl;
return FALSE;
}
//----------用输入的密码产生一个散列---------------------------------------------------------------
if(CryptHashData(
hHash,
(BYTE *)szPassword,
strlen(szPassword),
0
))
{
cout<<"the password has been addd to the hash"<<endl;
}
else
{
cout<<"error during cryptHashdata "<<endl;
return FALSE;
}
//---------通过散列生成会话密钥-------------------------------------------------------
if(CryptDeriveKey(
hCryptProv,
ENCRYPT_ALGORITHM,
hHash,
KEYLENGTH,
&hKey
))
{
cout<<"an encryption key is derived from the password hash"<<endl;
}
else
{
cout<<"error during crypt cryptDeriveKey"<<endl;
return FALSE;
}
//---------destroy the hash object---------------------------------------------------------------
CryptDestroyHash(hHash);
hHash=NULL;
//----------------------------------------------------------
// the session key is now ready
//------------------------------------------------------------
dwBlockLen=1000-1000%ENCRYPT_BLOCK_SIZE;
if(ENCRYPT_BLOCK_SIZE >1)
dwBufferLen=dwBlockLen+ENCRYPT_BLOCK_SIZE;
else
dwBufferLen=dwBlockLen;
//-----allocate memory--------------------------------------------------------
if(pbBuffer=(BYTE *)malloc(dwBufferLen))
{
cout<<"memory has been allocated for the buffer "<<endl;
}
else
{
cout<<"out of memory"<<endl;
return FALSE;
}
//--- In a do loop,encrypt the source file and write to destination file-----------------------------------------------------------
do{
//-----read up to dwBlockLen bytes from source file
dwCount=fread(pbBuffer,1,dwBlockLen,hSource);
if(ferror(hSource))
{
cout<<"error reading source file"<<endl;
return FALSE;
}
//-----解密数据-------------------------------------------
if(!CryptDecrypt(
hKey, //密钥
0, //如果同时进行散列和解密,这里传一个散列对象
feof(hSource),//如果是最后一个加密块,返回true否则返回false
0, //保留
pbBuffer,//输入保存源数据,输出保存加密数据
&dwCount))//被解密的实际长度
{
cout<<"error during cryptdecrypt"<<endl;
//return false;
}
//--write data to the destination file---------------------------------------------------------
fwrite(pbBuffer,1,dwCount,hDestination);
if(ferror(hDestination))
{
cout<<"error write ciphertext"<<endl;
return FALSE;
}
}while(!feof(hSource));
//-----close files----------------------------------------------------------
if(hSource)
fclose(hSource);
if(hDestination)
fclose(hDestination);
//-----free memory-------------------------------------------------------
if(pbBuffer)
free(pbBuffer);
//-- destroy session key-------------------------------------------------------
if(hKey)
CryptDestroyKey(hKey);
//----destroy hash object---------------------------------------------------------
if(hHash)
CryptDestroyHash(hHash);
//----release provider handle-------------------------------------------------------
if(hCryptProv)
CryptReleaseContext(hCryptProv,0);
return TRUE;
}
- ocx中对称密钥加密解密的简单实例
- 加密解密---非对称密钥/对称密钥
- SQLServer 数据加密解密:在多个服务器实例中创建相同对称密钥(三)
- 使用密钥加密、解密实例
- 对称加密的密钥交换
- 对称加密的对称密钥分发
- 对称加密中DES的加密与解密算法
- 密码学---非对称密码的加密解密采用公钥还是密钥
- 通过代理截取并修改非对称密钥加密信息 加密、解密算法总的来说可以分称对称密钥加密以及非对称密钥加密算法。 对称密钥加密算法要求加密和解密都用同一把密钥。这可能是对称密码体制的主要弱点(为了让对方能够
- 用对称密钥对 XML 元素进行加密解密
- Des 对称加密算法 加密\解密实例
- AES 对称加密算法 加密\解密实例
- 简单PHP对称加密解密方法、具有时效性的php加密解密函数
- 一个简单的加密解密方法实例
- 简单PHP对称加密解密方法
- PHP简单对称加密、解密方法
- 简单PHP对称加密解密方法
- 报表服务器无法解密用于访问报表服务器数据库中的敏感数据或加密数据的对称密钥。必须还原备份密钥或删除所有加密的内容。
- 使用C#调用外部Ping命令获取网络连接情况
- 使用DOM4J创建XML
- 数据库 SQL面试题
- 语言
- 手机浏览器页面知识收集
- ocx中对称密钥加密解密的简单实例
- 安全性测试——Buffer overrun
- 使用DOM4J读取XML文档
- 91461452469259f108dee0593bece4cb
- 虚拟机安装tools的过程中遇到一个c header 问题(我的镜像是:ubuntu-10.10-desktop-i386.iso)
- C++学习之main函数以及带形参的main函数
- bgiframe 解决IE6 DIV 档不住下拉选择框
- VS2008中配置openCV和GDAL
- EXT-grid 操作大全