CSP开发基础--开发实例一

来源:互联网 发布:安庆网络推广 编辑:程序博客网 时间:2024/05/09 21:52

本文先暂且实现一个加密的例子作为入门只用,同时具体感受一下一些最为基本的函数的使用。

下面的程序实例使用的是VS2005开发,如果使用VC6.0会出现编译问题,网友有的回答,可以添加语句

#define _WIN32_WINNT 0X0400但是,记过测试貌似也不太可行,估计是编译器的版本问题吧。

[cpp] view plaincopyprint?
  1. #include <stdio.h>  
  2. #include <windows.h>  
  3. #include <wincrypt.h>  
  4. #define MY_ENCODING_TYPE  (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)  
  5. #define KEYLENGTH  0x00800000  
  6. void HandleError(char *s);  
  7.   
  8. //--------------------------------------------------------------------  
  9. //  These additional #define statements are required.  
  10. #define ENCRYPT_ALGORITHM CALG_RC4   
  11. #define ENCRYPT_BLOCK_SIZE 8   
  12.   
  13. //   Declare the function EncryptFile. The function definition  
  14. //   follows main.  
  15.   
  16. BOOL EncryptFile(  
  17.      PCHAR szSource,   
  18.      PCHAR szDestination,   
  19.      PCHAR szPassword);   
  20.   
  21. //--------------------------------------------------------------------  
  22. //   Begin main.  
  23.   
  24. void main(void)   
  25. {   
  26.     CHAR szSource[100];   
  27.     CHAR szDestination[100];   
  28.     CHAR szPassword[100];   
  29.    
  30.    
  31.  printf("Encrypt a file. \n\n");  
  32.  printf("Enter the name of the file to be encrypted: ");  
  33.  scanf("%s",szSource);  
  34.  printf("Enter the name of the output file: ");  
  35.  scanf("%s",szDestination);  
  36.  printf("Enter the password:");  
  37.  scanf("%s",szPassword);  
  38.    
  39.  //--------------------------------------------------------------------  
  40.  // Call EncryptFile to do the actual encryption.  
  41.    
  42.  if(EncryptFile(szSource, szDestination, szPassword))  
  43.  {  
  44.   printf("Encryption of the file %s was a success. \n", szSource);  
  45.   printf("The encrypted data is in file %s.\n",szDestination);  
  46.  }  
  47.  else  
  48.  {  
  49.   HandleError("Error encrypting file!");   
  50.  }   
  51. // End of main  
  52.   
  53. //--------------------------------------------------------------------  
  54. //   Code for the function EncryptFile called by main.  
  55.   
  56. static BOOL EncryptFile(  
  57.       PCHAR szSource,   
  58.       PCHAR szDestination,   
  59.       PCHAR szPassword)  
  60.       //--------------------------------------------------------------------  
  61.       //   Parameters passed are:  
  62.       //     szSource, the name of the input, a plaintext file.  
  63.       //     szDestination, the name of the output, an encrypted file to be   
  64.       //         created.  
  65.       //     szPassword, the password.  
  66. {   
  67.  //--------------------------------------------------------------------  
  68.  //   Declare and initialize local variables.  
  69.    
  70.  FILE *hSource;   
  71.  FILE *hDestination;   
  72.    
  73.  HCRYPTPROV hCryptProv;   
  74.  HCRYPTKEY hKey;   
  75.  HCRYPTHASH hHash;   
  76.     
  77.  PBYTE pbBuffer;   
  78.  DWORD dwBlockLen;   
  79.  DWORD dwBufferLen;   
  80.  DWORD dwCount;   
  81.    
  82.  //--------------------------------------------------------------------  
  83.  // Open source file.   
  84.  if(hSource = fopen(szSource,"rb"))  
  85.  {  
  86.   printf("The source plaintext file, %s, is open. \n", szSource);  
  87.  }  
  88.  else  
  89.  {   
  90.   HandleError("Error opening source plaintext file!");  
  91.  }   
  92.   
  93.  //--------------------------------------------------------------------  
  94.  // Open destination file.   
  95.  if(hDestination = fopen(szDestination,"wb"))  
  96.  {  
  97.   printf("Destination file %s is open. \n", szDestination);  
  98.  }  
  99.  else  
  100.  {  
  101.   HandleError("Error opening destination ciphertext file!");   
  102.  }  
  103.   
  104.  //以下获得一个CSP句柄  
  105.  if(CryptAcquireContext(  
  106.   &hCryptProv,   
  107.   NULL,    //NULL表示使用默认密钥容器,默认密钥容器名  
  108. //为用户登陆名  
  109.   NULL,   
  110.   PROV_RSA_FULL,   
  111.   0))  
  112.  {  
  113.   printf("A cryptographic provider has been acquired. \n");  
  114.  }  
  115.  else  
  116.  {  
  117.   if(CryptAcquireContext(  
  118.    &hCryptProv,   
  119.    NULL,   
  120.    NULL,   
  121.    PROV_RSA_FULL,   
  122.    CRYPT_NEWKEYSET))//创建密钥容器  
  123.   {  
  124.    //创建密钥容器成功,并得到CSP句柄  
  125.    printf("A new key container has been created.\n");  
  126.   }  
  127.   else  
  128.   {  
  129.    HandleError("Could not create a new key container.\n");  
  130.   }  
  131.     
  132.  }  
  133.   
  134.  //--------------------------------------------------------------------  
  135.  // 创建一个会话密钥(session key)  
  136.  // 会话密钥也叫对称密钥,用于对称加密算法。  
  137.  // (注: 一个Session是指从调用函数CryptAcquireContext到调用函数  
  138.  //   CryptReleaseContext 期间的阶段。会话密钥只能存在于一个会话过程)  
  139.   
  140.  //--------------------------------------------------------------------  
  141.  // Create a hash object.   
  142.  if(CryptCreateHash(  
  143.   hCryptProv,   
  144.   CALG_MD5,   
  145.   0,   
  146.   0,   
  147.   &hHash))  
  148.     {  
  149.         printf("A hash object has been created. \n");  
  150.     }  
  151.     else  
  152.     {   
  153.   HandleError("Error during CryptCreateHash!\n");  
  154.     }    
  155.   
  156.  //--------------------------------------------------------------------  
  157.  // 用输入的密码产生一个散列  
  158.  if(CryptHashData(  
  159.   hHash,   
  160.   (BYTE *)szPassword,   
  161.   strlen(szPassword),   
  162.   0))  
  163.  {  
  164.   printf("The password has been added to the hash. \n");  
  165.  }  
  166.  else  
  167.  {  
  168.   HandleError("Error during CryptHashData. \n");   
  169.  }  
  170.   
  171.  //--------------------------------------------------------------------  
  172.  // 通过散列生成会话密钥  
  173.  if(CryptDeriveKey(  
  174.   hCryptProv,   
  175.   ENCRYPT_ALGORITHM,   
  176.   hHash,   
  177.   KEYLENGTH,   
  178.   &hKey))  
  179.  {  
  180.   printf("An encryption key is derived from the password hash. \n");   
  181.  }  
  182.  else  
  183.  {  
  184.   HandleError("Error during CryptDeriveKey!\n");   
  185.  }  
  186.  //--------------------------------------------------------------------  
  187.  // Destroy the hash object.   
  188.    
  189.  CryptDestroyHash(hHash);   
  190.  hHash = NULL;   
  191.    
  192.  //--------------------------------------------------------------------  
  193.  //  The session key is now ready.   
  194.    
  195.  //--------------------------------------------------------------------  
  196.  // 因为加密算法是按ENCRYPT_BLOCK_SIZE 大小的块加密的,所以被加密的  
  197. // 数据长度必须是ENCRYPT_BLOCK_SIZE 的整数倍。下面计算一次加密的  
  198. // 数据长度。  
  199.   
  200.  dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;   
  201.    
  202.  //--------------------------------------------------------------------  
  203.  // Determine the block size. If a block cipher is used,   
  204.  // it must have room for an extra block.   
  205.    
  206.  if(ENCRYPT_BLOCK_SIZE > 1)   
  207.   dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;   
  208.  else   
  209.   dwBufferLen = dwBlockLen;   
  210.    
  211.  //--------------------------------------------------------------------  
  212.  // Allocate memory.   
  213.  if(pbBuffer = (BYTE *)malloc(dwBufferLen))  
  214.  {  
  215.   printf("Memory has been allocated for the buffer. \n");  
  216.  }  
  217.  else  
  218.  {   
  219.   HandleError("Out of memory. \n");   
  220.  }  
  221.  //--------------------------------------------------------------------  
  222.  // In a do loop, encrypt the source file and write to the source file.   
  223.    
  224.  do   
  225.  {   
  226.     
  227.   //--------------------------------------------------------------------  
  228.   // Read up to dwBlockLen bytes from the source file.   
  229.   dwCount = fread(pbBuffer, 1, dwBlockLen, hSource);   
  230.   if(ferror(hSource))  
  231.   {   
  232.    HandleError("Error reading plaintext!\n");  
  233.   }  
  234.     
  235.   //--------------------------------------------------------------------  
  236.   // 加密数据  
  237.   if(!CryptEncrypt(  
  238.    hKey,   //密钥  
  239.    0,    //如果数据同时进行散列和加密,这里传入一个  
  240. //散列对象  
  241.    feof(hSource), //如果是最后一个被加密的块,输入TRUE.如果不是输.  
  242.        //入FALSE这里通过判断是否到文件尾来决定是否为  
  243. //最后一块。  
  244.    0,    //保留  
  245.    pbBuffer,  //输入被加密数据,输出加密后的数据  
  246.    &dwCount,  //输入被加密数据实际长度,输出加密后数据长度  
  247.    dwBufferLen)) //pbBuffer的大小。  
  248.   {   
  249.    HandleError("Error during CryptEncrypt. \n");   
  250.   }   
  251.     
  252.   //--------------------------------------------------------------------  
  253.   // Write data to the destination file.   
  254.     
  255.   fwrite(pbBuffer, 1, dwCount, hDestination);   
  256.   if(ferror(hDestination))  
  257.   {   
  258.    HandleError("Error writing ciphertext.");  
  259.   }  
  260.     
  261.  }   
  262.  while(!feof(hSource));   
  263.  //--------------------------------------------------------------------  
  264.  //  End the do loop when the last block of the source file has been  
  265.  //  read, encrypted, and written to the destination file.  
  266.    
  267.  //--------------------------------------------------------------------  
  268.  // Close files.  
  269.    
  270.  if(hSource)   
  271.   fclose(hSource);   
  272.  if(hDestination)   
  273.   fclose(hDestination);   
  274.    
  275.  //--------------------------------------------------------------------  
  276.  // Free memory.   
  277.    
  278.  if(pbBuffer)   
  279.   free(pbBuffer);   
  280.    
  281.  //--------------------------------------------------------------------  
  282.  // Destroy session key.   
  283.    
  284.  if(hKey)   
  285.   CryptDestroyKey(hKey);   
  286.    
  287.  //--------------------------------------------------------------------  
  288.  // Destroy hash object.   
  289.    
  290.  if(hHash)   
  291.   CryptDestroyHash(hHash);   
  292.    
  293.  //--------------------------------------------------------------------  
  294.  // Release provider handle.   
  295.    
  296.  if(hCryptProv)   
  297.   CryptReleaseContext(hCryptProv, 0);  
  298.  return(TRUE);   
  299. // End of Encryptfile  
  300.   
  301. //--------------------------------------------------------------------  
  302. //  This example uses the function HandleError, a simple error  
  303. //  handling function, to print an error message to the standard error   
  304. //  (stderr) file and exit the program.   
  305. //  For most applications, replace this function with one   
  306. //  that does more extensive error reporting.  
  307.   
  308. void HandleError(char *s)  
  309. {  
  310.     fprintf(stderr,"An error occurred in running the program. \n");  
  311.     fprintf(stderr,"%s\n",s);  
  312.     fprintf(stderr, "Error number %x.\n", GetLastError());  
  313.     fprintf(stderr, "Program terminating. \n");  
  314.     exit(1);  
  315. // End of HandleError  


0 0
原创粉丝点击