vc++网络安全编程范例(13)-openssl engine编程

本文介绍OpenSSL Engine机制,该机制允许OpenSSL使用第三方软件加密库或硬件加密设备。通过一个VC++编程实例展示了如何加载自定义Engine,进行对称加密及解密操作。

Engine机制 Engine机制的出现是在OpenSSL的0.9.6版的事情,开始的时候是将普通版本跟支持Engine的版本分开的,到了OpenSSL的0.9.7版,Engine机制集成到了OpenSSL的内核中,成为了OpenSSL不可缺少的一部分。 Engine机制目的是为了使OpenSSL能够透明地使用第三方提供的软件加密库或者硬件加密设备进行加密。OpenSSL的Engine机制成功地达到了这个目的,这使得OpenSSL已经不仅仅使一个加密库,而是提供了一个通用地加密接口,能够与绝大部分加密库或者加密设备协调工作。当然,要使特定加密库或加密设备更OpenSSL协调工作,需要写少量的接口代码,但是这样的工作量并不大,虽然还是需要一点密码学的知识。Engine机制的功能跟Windows提供的CSP功能目标是基本相同的。目前,OpenSSL的0.9.7版本支持的内嵌第三方加密设备有8种,包括:CryptoSwift、nCipher、Atalla、Nuron、UBSEC、Aep、SureWare以及IBM 4758 CCA的硬件加密设备。现在还出现了支持PKCS#11接口的Engine接口,支持微软CryptoAPI的接口也有人进行开发。当然,所有上述Engine接口支持不一定很全面,比如,可能支持其中一两种公开密钥算法

我们来实现基于VC++的openssl engine编程,请见代码实现与注释讲解

[cpp]  view plain  copy
  1. #include "commonlib.h"  
  2. #include "simple_engine_def.h"  
  3.   
  4. #include <openssl/evp.h>  
  5.   
  6. int    main()  
  7. {  
  8.   
  9.        ENGINE        *e;  
  10.        const EVP_CIPHER    *cipher=EVP_des_ecb();  
  11.        EVP_CIPHER_CTX ciph_ctx;  
  12.        unsigned char key[16],iv[16];  
  13.        const char* info="this is a simple engine test";  
  14.        unsigned char out[100],dec_data[100];  
  15.        int      outl,total;   
  16.   
  17.   
  18.        OpenSSL_add_all_algorithms();  
  19.        ENGINE_load_simplecipher();//加载自定义的engine  
  20.        e=ENGINE_by_id(SIMPLE_ENGINE_ID);  
  21.        printf("Engine 名称 :%s \n",(char *)ENGINE_get_name(e));  
  22.   
  23.        //随机数生成  
  24.        if( RAND_set_rand_engine(e)==0 )  
  25.               int_error("RAND_set_rand_engine 错误\n");  
  26.   
  27.        //利用自定义随机数算法生成随机数并填充到key中用于加解密  
  28.        RAND_bytes(key,sizeof(key));  
  29.        printf("生成的随机数字符串为:");  
  30.        for(int i = 0; i < sizeof(key); i++) printf("%c", key[i]);  
  31.        printf("\n");  
  32.   
  33.        //对称加密  
  34.        EVP_CIPHER_CTX_init(&ciph_ctx);  
  35.        //采用Engine对称算法  
  36.        if( EVP_EncryptInit_ex(&ciph_ctx,cipher,e,key,iv)==0 )  
  37.               int_error("EVP_EncryptInit_ex 错误\n");  
  38.   
  39.        total=0;  
  40.        if( EVP_EncryptUpdate(&ciph_ctx,out,&outl,(const unsigned char*)info,(int)strlen(info))==0 )  
  41.               int_error("EVP_EncryptUpdate 错误\n");  
  42.        total+=outl;  
  43.   
  44.        if( EVP_EncryptFinal(&ciph_ctx,out+total,&outl)==0 )  
  45.               int_error("EVP_EncryptFinal 错误\n");  
  46.        total+=outl;  
  47.        printf("加密后的数据为:\n");  
  48.        forint j = 0; j < total; j++) printf("%02x", out[j]);  
  49.        printf("\n");  
  50.   
  51.        //解密  
  52.        if( EVP_DecryptInit_ex(&ciph_ctx,cipher,e,key,iv)==0 )  
  53.               int_error("EVP_DecryptInit_ex 错误\n");  
  54.   
  55.        if( EVP_DecryptUpdate(&ciph_ctx,dec_data,&outl,out,total)==0 )  
  56.               int_error("EVP_DecryptUpdate 错误\n");  
  57.        total=outl;  
  58.   
  59.        if( EVP_DecryptFinal(&ciph_ctx,dec_data+total,&outl)==0 )  
  60.               int_error("EVP_DecryptFinal 错误");  
  61.        total+=outl;  
  62.   
  63.        dec_data[total]=0;  
  64.        printf("解密之后内容(长度=%d):[%s]\n",total,dec_data);  
  65.        return 0;  
  66. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值