- 论坛徽章:
- 0
|
采用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
难道这种很成熟的算法还存在差异性吗?
- int EVP_aes256_encrypt( char *src, int srcLen, char *dst, int dstLen)
- {
- char* ckey = "D915581AA2EF37B5";
- char* ivec = "5B73FE2AA185519D";
- char* srcTmp = NULL;
- char* dstTmp = NULL;
- int encrypt_once = 1*1024;
- int b_success = 0;
- int outLen1 = 0;
- int outLen1Sum = 0;
- int outLen2 = 0;
- EVP_CIPHER_CTX ctx;
-
- if(src == NULL || dst == NULL || srcLen == 0 || dstLen == 0)
- {
- printf( "ims_aes256_encrypt input param error....\n");
- return ERROR;
- }
-
- memset(&ctx, 0, sizeof(ctx));
- (void)EVP_CIPHER_CTX_init(&ctx);
- (void)EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, (const unsigned char *)ckey, (const unsigned char *)ivec);
- srcTmp = src;
- dstTmp = dst;
- while(srcLen > 0)
- {
- encrypt_once = MIN(encrypt_once, srcLen);
- b_success = EVP_EncryptUpdate(&ctx, (unsigned char*)dstTmp, &outLen1, (unsigned char*)srcTmp, encrypt_once);
- if(!b_success)
- {
- printf( "EVP_EncryptUpdate() failed...\n");
- (void)EVP_CIPHER_CTX_cleanup(&ctx);
- return ERROR;
- }
- outLen1Sum += outLen1;
- srcTmp += encrypt_once;
- dstTmp += outLen1;
- srcLen -= encrypt_once;
- }
-
- //MEGA_TRACE_DEBUG(MEGA_LEVEL_INFO, "outLen1 [%d]\n", outLen1);
-
- b_success = EVP_EncryptFinal_ex(&ctx, (unsigned char*)dstTmp, &outLen2);
- if(!b_success)
- {
- printf( "EVP_EncryptFinal() failed...\n");
- (void)EVP_CIPHER_CTX_cleanup(&ctx);
- return ERROR;
- }
-
- (void)EVP_CIPHER_CTX_cleanup(&ctx);
-
- if((outLen1Sum + outLen2) > dstLen)
- {
- printf( "OverLow dst buffer...\n");
- return ERROR;
- }
- return (outLen1Sum + outLen2);
- }
- int EVP_aes256_decrypt( char *src, int srcLen, char *dst, int dstLen)
- {
- char* ckey = "D915581AA2EF37B5";
- char* ivec = "5B73FE2AA185519D";
- int encrypt_once = 1*1024;
- int b_success = 0;
- int outLen1 = 0;
- int outLen2 = 0;
- int outLen1Sum = 0;
- EVP_CIPHER_CTX ctx;
- char* srcTmp = NULL;
- char* dstTmp = NULL;
-
- if(src == NULL || dst == NULL || srcLen <= 0 || dstLen <= 0)
- {
- printf( "ims_aes256_encrypt input param error....\n");
- return ERROR;
- }
- memset(&ctx, 0, sizeof(ctx));
-
- (void)EVP_CIPHER_CTX_init(&ctx);
- (void)EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, (const unsigned char *)ckey, (const unsigned char *)ivec);
- srcTmp = src;
- dstTmp = dst;
- while(srcLen > 0)
- {
- encrypt_once = MIN(encrypt_once, srcLen);
- b_success = EVP_DecryptUpdate(&ctx, (unsigned char*)dstTmp, &outLen1, (unsigned char*)srcTmp, encrypt_once);
- if(!b_success)
- {
- printf( "EVP_DecryptUpdate() failed!!!\n");
- (void)EVP_CIPHER_CTX_cleanup(&ctx);
- return ERROR;
- }
- outLen1Sum += outLen1;
- srcTmp += encrypt_once;
- dstTmp += outLen1;
- srcLen -= encrypt_once;
- }
- printf("outLen1 [%d]\n", outLen1);
- printf("outLen1Sum [%d]\n", outLen1Sum);
-
- b_success = EVP_DecryptFinal_ex(&ctx, (unsigned char*)dstTmp, &outLen2);
- if(!b_success)
- {
- printf( "EVP_DecryptFinal_ex() failed!!!\n");
- (void)EVP_CIPHER_CTX_cleanup(&ctx);
- return ERROR;
- }
- printf("outLen2 [%d]\n", outLen2);
-
- (void)EVP_CIPHER_CTX_cleanup(&ctx);
- if((outLen1Sum + outLen2) > dstLen)
- {
- printf( "OverLow dst buffer...\n");
- return ERROR;
- }
- return (outLen1Sum + outLen2);
- }
复制代码
|
|