ocx中对称密钥加密解密的简单实例
2011-04-07 14:05
267 查看
//------------------------------------------------------------------
#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;
}
#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;
}
相关文章推荐
- C++ 简单的文件加密和解密 实例
- AES加密解密简单实例
- html简单 字母数字 加密 解密 实例
- C++ 简单的文件加密和解密 实例
- C++ 简单的文件加密和解密 实例
- 一个简单的加密解密方法实例
- Java使用异或运算实现简单的加密解密算法实例代码
- C++ 简单的文件加密和解密 实例
- C++ 简单的文件加密和解密 实例
- C++ 简单的文件加密和解密 实例
- javascript对页面简单的加密和解密
- php rsa加密解密实例
- 简单的信息加密解密
- 通过代理截取并修改非对称密钥加密信息 加密、解密算法总的来说可以分称对称密钥加密以及非对称密钥加密算法。 对称密钥加密算法要求加密和解密都用同一把密钥。这可能是对称密码体制的主要弱点(为了让对方能够
- Qt对中文文件的简单加密解密
- 简单的内容加密解密,在嵌入式上实现
- 一个简单的加密解密程序
- c++实现简单的加密与解密
- java 加密解密简单实现
- java实现RSA+AES的简单加密解密