使用openssl中的EVP通用加密算法接口之--加密测试例子

/*********************************************************************
 * Author  : Samson
 * Date    : 01/22/2014
 * Test platform:
 *               3.6.10-4.fc18.i686.PAE
 *               GNU bash, version 4.2.39
 * *******************************************************************/

此测试例子是使用的openssl库中提供的EVP系列函数对数据进行加密的一个测试,选择的算法为:AES128,加密模式为:CBC,此加密算法模式下需要的参数为:16字节的加密key,16字节的IV(初始化向量),数据的长度根据需要对宏DATA_LEN进行修改。若是要选择其它的算法,请自己man一下相关的函数接口是否存在。例如要使用的算法为AES192 CBC的算法的话,那么在下面加红的那句中把     rv = EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(),NULL, key, iv); 中的EVP_aes_128_cbc修改为EVP_aes_192_cbc,但是对应的参数也要进行相应长度的值的输入,EVP_aes_192_cbc算法的加密key的长度应该为24字节,IV的长度还是16字节,这个具体算法要根据具体算法进行参数的输入,进行举一反三。


#include <stdio.h>

#include <string.h>
#include <errno.h>
#include <resolv.h>
#include <stdlib.h>
#include <unistd.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/x509.h>

#define DATA_LEN 32
#define EVP_MAX_KEY_LENGHT 64

int main()
{
        EVP_CIPHER_CTX ctx;
        unsigned char key[EVP_MAX_KEY_LENGHT] = "\xc2\x86\x69\x6d\x88\x7c\x9a\xa0\x61\x1b\xbb\x3e\x20\x25\xa4\x5a";
        unsigned char iv[EVP_MAX_KEY_LENGHT] = "\x56\x2e\x17\x99\x6d\x09\x3d\x28\xdd\xb3\xba\x69\x5a\x2e\x6f\x58";
        unsigned char out[1024] = {0};
        int outl, tmp, i;
        unsigned char msg[1024] = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
    
        int rv;
        OpenSSL_add_all_algorithms();
        EVP_CIPHER_CTX_init(&ctx);
    
        rv = EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(),NULL, key, iv);
        if(rv != 1)
        {   
                printf("Error");
                return -1;
        }   
        outl = 0;
        rv = EVP_EncryptUpdate(&ctx, out, &outl, msg, DATA_LEN);
        if(rv != 1)
        {   
                printf("Error");
                return -1;
        }   
        rv = EVP_EncryptFinal_ex(&ctx, out + outl, &tmp);
        outl = outl + tmp;    
     printf("----cipher_algo is AES128 cipher_mode is CBC  enc outdata is :-----------\n");
        for(i = 0; i < DATA_LEN; i++)
                printf("%02x ", out[i]);
        printf("\r\n");
        return 0;
}

编译:

[ufo@localhost fuck]$ gcc testenc_evp.c -Wall -g -lssl -lcrypto -o testenc


测试结果:
[ufo@localhost fuck]$ ./testenc
----cipher_algo is AES128 cipher_mode is CBC  enc outdata is :-----------
d2 96 cd 94 c2 cc cf 8a 3a 86 30 28 b5 e1 dc 0a 75 86 60 2d 25 3c ff f9 1b 82 66 be a6 d6 1a b1



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
0、此例程调试环境 运行uname -a的结果如下: Linux freescale 3.0.35-2666-gbdde708-g6f31253 #1 SMP PREEMPT Thu Nov 30 15:45:33 CST 2017 armv7l GNU/Linux 简称2017 armv7l GNU/Linux 1、openssl 直接处理AES的API 在openssl/aes.h定义。是基本的AES库函数接口,可以直接调用,但是那个接口是没有填充的。而如果要与Java通信,必须要有填充模式。所以看第2条。 2、利用openssl EVP接口openssl/evp.h定义。在这个接口提供的AES是默认是pkcs5padding方式填充方案。 3、注意openssl新老版本的区别 看如下这段 One of the primary differences between master (OpenSSL 1.1.0) and the 1.0.2 version is that many types have been made opaque, i.e. applications are no longer allowed to look inside the internals of the structures. The biggest impact on applications is that: 1)You cannot instantiate these structures directly on the stack. So instead of: EVP_CIPHER_CTX ctx; you must instead do: EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); .... EVP_CIPHER_CTX_free(ctx); 2)You must only use the provided accessor functions to access the internals of the structure. 4、注意加密的内容是数据不限制是否为字符串 openssl接口加密的是数据,不限制是否为字符串,我看到有些人在加密使用strlen(),来获取要加密的长度,如果是对字符串加密的话没有问题,如果不是字符串的话,用它获取的长度是到第一个0处,因为这个函数获取的是字符串长度,字符串是以零为终止的。 5、在调用EVP_EncryptFinal_ex时不要画蛇添足 正常加解密处理过程,引用网友的代码如下,经测试正确。 int kk_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext) { EVP_CIPHER_CTX *ctx; int len; int ciphertext_len; ctx = EVP_CIPHER_CTX_new(); EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv); //EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, iv); EVP_EncryptUpdate(ctx, ciphertext, &len;, plaintext, plaintext_len); ciphertext_len = len; EVP_EncryptFinal_ex(ctx, ciphertext + len, &len;); ciphertext_len += len; EVP_CIPHER_CTX_free(ctx); return ciphertext_len; } int kk_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext) { EVP_CIPHER_CTX *ctx; int len; int plaintext_len; ctx = EVP_CIPHER_CTX_new(); EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv); //EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, iv); EVP_DecryptUpdate(ctx, plaintext, &len;, ciphertext, ciphertext_len); plaintext_len = len; EVP_DecryptFinal_ex(ctx, plaintext + len, &len;); plaintext_len += len; EVP_CIPHER_CTX_free(ctx); return plaintext_len; } 我看到有人提供的代码在加密长度正好是16字节的整数倍时特意不去调用EVP_EncryptFinal_ex,这实在是画蛇添足啊,不论什么情况下,最后一定要调用EVP_EncryptFinal_ex一次,然后结束加密过程。 6、Base64陷阱 如果用到了base64,要注意如下: 1)base64算法是将3个字节变成4个可显示字符。所以在如果数据长度不是3字节对齐时,会补0凑齐。 2)在解密时先要解base64,再解AES。在解base64后,要减掉补上的0。算法就去查看base64后的字符串尾处有几个=号,最多是2个,如果正好要加密的数据是3的倍数,不需要补0,那么base64后的数据尾处就没有=,如果补了1个0,就有一个=号。 算法如下: int encode_str_size = EVP_EncodeBlock(base64, en, el); int length = EVP_DecodeBlock(base64_out, base64, encode_str_size ); //EVP_DecodeBlock内部同样调用EVP_DecodeInit + EVP_DecodeUpdate + Evp_DecodeFinal实现,但是并未处理尾部的'='字符,因此结果字符串长度总是为3的倍数 while(base64[--encode_str_size] == '=') length--; 算法网友提供,测试正确。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值