OpenSSL之EVP(三)——EVP对称算法编程示例

des对称算法

源代码如下:

#include <string.h>
#include <openssl/evp.h>

void show_hex(unsigned char * s, int len)
{
    for(int i=0; i<len; i++)
    {
        //以16进制输出,每一个字符占2位。
        printf("%02x",s[i]);
        if(19 == i % 20)
            printf("\n");
    }
    printf("\n\n");
}

int main()
{
    int ret,which=1;
    EVP_CIPHER_CTX ctx;
    const EVP_CIPHER *cipher;
    unsigned char key[24],iv[8],in[100],out[108],de[100];
    int i,len,inl,outl,total=0;
    for(i=0;i<24;i++)
    {
        memset(&key[i],i,1);
    }
    for(i=0;i<8;i++)
    {
        memset(&iv[i],i,1);
    }
    for(i=0;i<100;i++)
    {
        memset(&in[i],i+1,1);
    }
    EVP_CIPHER_CTX_init(&ctx);
    printf("please select :\n");
    printf("1: EVP_des_ede3_ofb\n");
    printf("2: EVP_des_ede3_cbc\n");
    scanf("%d",&which);
    if(which==1)
        cipher=EVP_des_ede3_ofb();
    else
        cipher=EVP_des_ede3_cbc();
    printf("原文:\n");
    show_hex(in, 100);
    ret=EVP_EncryptInit_ex(&ctx,cipher,NULL,key,iv);
    if(ret!=1)
    {
        printf("EVP_EncryptInit_ex err1!\n");
        return -1;
    }
    inl=50;
    len=0;
    EVP_EncryptUpdate(&ctx,out+len,&outl,in,inl);
    len+=outl;
    EVP_EncryptUpdate(&ctx,out+len,&outl,in+50,inl);
    len+=outl;
    EVP_EncryptFinal_ex(&ctx,out+len,&outl);
    len+=outl;
    printf("加密结果长度: %d\n",len);
    printf("加密结果:\n");
    show_hex(out, len); 
    EVP_CIPHER_CTX_cleanup(&ctx);

    /* 解密 */
    EVP_CIPHER_CTX_init(&ctx);
    ret=EVP_DecryptInit_ex(&ctx,cipher,NULL,key,iv);
    if(ret!=1)
    {
        printf("EVP_DecryptInit_ex err1!\n");
        return -1;
    }
    total=0;
    EVP_DecryptUpdate(&ctx,de+total,&outl,out,44);
    total+=outl;
    EVP_DecryptUpdate(&ctx,de+total,&outl,out+44,len-44);
    total+=outl;
    ret=EVP_DecryptFinal_ex(&ctx,de+total,&outl);
    total+=outl;
    printf("解密结果长度: %d\n",total);
    printf("解密结果:\n");
    show_hex(de, total);    
    if(ret!=1)
    {
        EVP_CIPHER_CTX_cleanup(&ctx);
        printf("EVP_DecryptFinal_ex err\n");
        return -1;
    }
    if((total!=100) || (memcmp(de,in,100)))
    {
        printf("err!\n");
        return -1;
    }
    EVP_CIPHER_CTX_cleanup(&ctx);
    printf("test ok!\n");
    return 0;
}

编译提示:

  1. 需要包含OpenSSL include 头文件目录
  2. 需要引入OpenSSL 库(libeay32.lib)。

运行结果:

这里写图片描述

这里写图片描述

注意事项

在运行EVP_des_ede3_cbc 算法时,运行结束时,会出现错误:
Run-Time Check Failure #2 - Stack around the variable 'de' was corrupted.
这里写图片描述
修复办法:
ref: http://www.cnblogs.com/flysnail/archive/2011/09/21/2184114.html

配置属性->c/c++->代码生成->基本运行时检查 为 默认值 就不会报该异常.
这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值