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

原创粉丝点击