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

你可能感兴趣的:(Algorithm,session,function,Random,buffer,input)