免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2849 | 回复: 0
打印 上一主题 下一主题

[应用] openssl AES 256 cbc加解密EVP_EncryptFinal_ex报错问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2017-01-12 10:25 |只看该作者 |倒序浏览
采用AES 256 CBC加密和解密一个文件。
在X86&linux上调用EVP_aes256_encrypt接口加密一个文件,同时在X86&linux上面调用EVP_aes256_decrypt解密,是可以成功的。
但是将EVP_aes256_decrypt接口移植到嵌入式系统中,EVP_aes256_decrypt接口,就失败报错。
padding采用缺省的,即开启了padding。


X86上面的openssl库版本是OpenSSL 1.0.1m 19 Mar 2015
嵌入式上面使用的openssl库版本是:1.0.1l

难道这种很成熟的算法还存在差异性吗?

  1. int EVP_aes256_encrypt( char *src, int srcLen, char *dst, int dstLen)
  2. {
  3.         char* ckey = "D915581AA2EF37B5";
  4.         char* ivec = "5B73FE2AA185519D";
  5.         char* srcTmp = NULL;
  6.         char* dstTmp = NULL;
  7.         int encrypt_once = 1*1024;
  8.         int b_success = 0;
  9.         int outLen1 = 0;
  10.         int outLen1Sum = 0;
  11.         int outLen2 = 0;
  12.         EVP_CIPHER_CTX ctx;
  13.        
  14.         if(src == NULL || dst == NULL || srcLen == 0 || dstLen == 0)
  15.         {
  16.         printf( "ims_aes256_encrypt input param error....\n");
  17.         return ERROR;
  18.         }
  19.        
  20.         memset(&ctx, 0, sizeof(ctx));

  21.         (void)EVP_CIPHER_CTX_init(&ctx);
  22.         (void)EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, (const unsigned char *)ckey, (const unsigned char *)ivec);
  23.         srcTmp = src;
  24.         dstTmp = dst;
  25.         while(srcLen > 0)
  26.         {
  27.                 encrypt_once = MIN(encrypt_once, srcLen);
  28.                 b_success = EVP_EncryptUpdate(&ctx, (unsigned char*)dstTmp, &outLen1, (unsigned char*)srcTmp, encrypt_once);
  29.                 if(!b_success)
  30.                 {
  31.                         printf( "EVP_EncryptUpdate() failed...\n");
  32.                         (void)EVP_CIPHER_CTX_cleanup(&ctx);
  33.                         return ERROR;
  34.                 }
  35.                 outLen1Sum += outLen1;
  36.                 srcTmp += encrypt_once;
  37.                 dstTmp += outLen1;
  38.                 srcLen -= encrypt_once;
  39.         }
  40.        
  41.         //MEGA_TRACE_DEBUG(MEGA_LEVEL_INFO, "outLen1 [%d]\n", outLen1);
  42.        
  43.         b_success = EVP_EncryptFinal_ex(&ctx, (unsigned char*)dstTmp, &outLen2);
  44.         if(!b_success)
  45.         {
  46.                 printf( "EVP_EncryptFinal() failed...\n");
  47.                 (void)EVP_CIPHER_CTX_cleanup(&ctx);
  48.         return ERROR;
  49.         }
  50.        
  51.         (void)EVP_CIPHER_CTX_cleanup(&ctx);
  52.        
  53.         if((outLen1Sum + outLen2) > dstLen)
  54.         {
  55.                 printf( "OverLow dst buffer...\n");
  56.         return ERROR;
  57.         }

  58.         return (outLen1Sum + outLen2);
  59. }

  60. int EVP_aes256_decrypt( char *src, int srcLen, char *dst, int dstLen)
  61. {
  62.         char* ckey = "D915581AA2EF37B5";
  63.         char* ivec = "5B73FE2AA185519D";
  64.         int encrypt_once = 1*1024;
  65.         int b_success = 0;
  66.         int outLen1 = 0;
  67.         int outLen2 = 0;
  68.         int outLen1Sum = 0;
  69.         EVP_CIPHER_CTX ctx;
  70.         char* srcTmp = NULL;
  71.         char* dstTmp = NULL;
  72.        
  73.         if(src == NULL || dst == NULL || srcLen <= 0 || dstLen <= 0)
  74.         {
  75.                 printf( "ims_aes256_encrypt input param error....\n");
  76.                 return ERROR;
  77.         }

  78.         memset(&ctx, 0, sizeof(ctx));
  79.        
  80.         (void)EVP_CIPHER_CTX_init(&ctx);
  81.         (void)EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, (const unsigned char *)ckey, (const unsigned char *)ivec);
  82.         srcTmp = src;
  83.         dstTmp = dst;
  84.         while(srcLen > 0)
  85.         {
  86.                 encrypt_once = MIN(encrypt_once, srcLen);
  87.                 b_success = EVP_DecryptUpdate(&ctx, (unsigned char*)dstTmp, &outLen1, (unsigned char*)srcTmp, encrypt_once);
  88.                 if(!b_success)
  89.                 {
  90.                         printf( "EVP_DecryptUpdate() failed!!!\n");
  91.                         (void)EVP_CIPHER_CTX_cleanup(&ctx);
  92.                         return ERROR;
  93.                 }
  94.                 outLen1Sum += outLen1;
  95.                 srcTmp += encrypt_once;
  96.                 dstTmp += outLen1;
  97.                 srcLen -= encrypt_once;
  98.         }
  99.         printf("outLen1 [%d]\n", outLen1);
  100.         printf("outLen1Sum [%d]\n", outLen1Sum);
  101.        
  102.         b_success = EVP_DecryptFinal_ex(&ctx, (unsigned char*)dstTmp, &outLen2);
  103.         if(!b_success)
  104.         {
  105.                 printf( "EVP_DecryptFinal_ex() failed!!!\n");
  106.                 (void)EVP_CIPHER_CTX_cleanup(&ctx);
  107.                 return ERROR;
  108.         }
  109.         printf("outLen2 [%d]\n", outLen2);
  110.        
  111.         (void)EVP_CIPHER_CTX_cleanup(&ctx);

  112.         if((outLen1Sum + outLen2) > dstLen)
  113.         {
  114.                 printf( "OverLow dst buffer...\n");
  115.                 return ERROR;
  116.         }

  117.         return (outLen1Sum + outLen2);
  118. }
复制代码



您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP