openssl des ECB

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







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));

    //memset((char*)&input, 0, sizeof(input));
    unsigned char ii[256] = { 0 };

    //解密  
    DES_ncbc_encrypt(output, ii, len, &key_schedule, &ivec, 0);

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

    free(output);






    return 0;
}

 

posted @ 2020-11-16 00:12  YZFHKMS-X  阅读(173)  评论(0编辑  收藏  举报