码迷,mamicode.com
首页 > 其他好文 > 详细

openssl des CBC

时间:2020-11-20 12:00:54      阅读:9      评论:0      收藏:0      [点我收藏+]

标签:failed   nbsp   col   tput   stream   malloc   data   des   put   

#include <iostream>
extern "C"
{
#include <libavutil/des.h>
}
#include <openssl/des.h>


int en(void *data, int size)
{

}

int de(void *data, void *out)
{

}






int main(int argc, char *argv[])
{


    printf("=============\n");

    const char *keystring = "this is my key";
    DES_cblock key;
    DES_key_schedule key_schedule;

    //生成一个 key  
    DES_string_to_key(keystring, &key);
    if (DES_set_key_checked(&key, &key_schedule) != 0) {
        printf("convert to key_schedule failed.\n");
        return -1;
    }

    //需要加密的字符串  
    unsigned char input[] = "this is a text being encrypted by openssl";
    size_t len = (sizeof(input) + 7) / 8 * 8;
    unsigned char *output = (unsigned char *)malloc(len + 1);
    //IV  
    DES_cblock ivec;

    //IV设置为0x0000000000000000  
    memset((char*)&ivec, 0, sizeof(ivec));

    //加密  
    DES_ncbc_encrypt(input, output, sizeof(input), &key_schedule, &ivec, DES_ENCRYPT);

    //输出加密以后的内容  
    for (int i = 0; i < len; ++i)
        printf("%02x", output[i]);
    printf("\n");

    memset((char*)&ivec, 0, sizeof(ivec));
    unsigned char ii[256] = { 0 };
    //解密  
    DES_ncbc_encrypt(output, ii, len, &key_schedule, &ivec, 0);

    printf("%s\n", ii);

    free(output);



    return 0;
}

 

openssl des CBC

标签:failed   nbsp   col   tput   stream   malloc   data   des   put   

原文地址:https://www.cnblogs.com/YZFHKMS-X/p/13983164.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!