Use EVP to generate random key and IV.

 

The EVP have provided a function EVP_BytesToKey() to generate session key and initial vector. And we can use the function to separate the input password to gain our secrete key or session key. And we don’t have to write a hash function or other algorithm to derive a session key.

int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,

            const unsigned char *salt, const unsigned char *data, int datal,

            int count, unsigned char *key, unsigned char *iv)

 

EVP_BytesToKey() derives a key and IV from various parameters.

<type> define the length of key and IV.

<md> is the message digest to use.

<salt> parameter is used as a salt in the derivation: it should point to an 8 byte buffer or NULL if no salt is used.

<data> is a buffer containing

<datal> bytes which is used to derive the keying data.

<count> is the iteration count to use. Define the times to digest.

<key>, <iv> output.

 

Test code:

    const EVP_CIPHER *type = EVP_des_cbc();

    const EVP_MD *md = EVP_sha1();

    const unsigned char salt[16] = {0};

    const unsigned char data[16] = "you are the one";

    int datal = strlen((char*)data) + 1;

    int count = 1;

    unsigned char key[8];

    unsigned char iv[16];

    int iLen = 0;

 

    iLen = EVP_BytesToKey (type, md, salt, data, datal, count, key, iv);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值