What is the “length” parameter of AES EVP_Decrypt?

前端 未结 1 1313
一向
一向 2020-12-29 16:17

This is linked to EVP_DecryptFinal_ex Error on OpenSSL

I was trying to find out why the AES decrypt won\'t work and finally I have found what the problem is and now

1条回答
  •  时光说笑
    2020-12-29 16:53

    First, don't return NULL from main(). Second, I fixed it and annotated it so hopefully you can see what the length variables mean. I think the key point you're missing is that you're giving the OpenSSL functions a buffer where it can write its data. Like many functions that take a buffer, you give it a buffer size and it returns to you the number of bytes it actually wrote into the buffer. Why? Because you have to know when your buffer is full, or if you're filling your buffer incrementally you have to know where to write the next chunk of data.

    Also, I think you should read some tutorials on how to work with binary data and how it differs from a C-style string. The OpenSSL EVP functions work with binary data, which is why you need to tell every function how many bytes your data is.

    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char **argv)
    {
    
      EVP_CIPHER_CTX en;
      EVP_CIPHER_CTX de;
      EVP_CIPHER_CTX_init(&en);
      EVP_CIPHER_CTX_init(&de);
      const EVP_CIPHER *cipher_type;
      unsigned char *passkey, *passiv, *plaintxt;
      unsigned char *plaintext = NULL;
      unsigned char *ciphertext = NULL;
      int input_len = 0;
    
      unsigned char iv[] = { 0x00, 0x01, 0x02, 0x03,
                             0x04, 0x05, 0x06, 0x07,
                             0x08, 0x09, 0x0a, 0x0b,
                             0x0c, 0x0d, 0x0e, 0x0f };
    
      unsigned char key[] = { 0x2b, 0x7e, 0x15, 0x16,
                              0x28, 0xae, 0xd2, 0xa6,
                              0xab, 0xf7, 0x15, 0x88,
                              0x09, 0xcf, 0x4f, 0x3c };
    
      const char *string_to_encrypt = "hi this is patrick immling\n'Doctor'.\n'Doctor' who ?\nPrecisely! 123910!§$$§% !%%$&$(/=))?=(#ü++Ü**<,.here we go sometimes it i s difficult but 187! 1$5 78@2 14  .TӒ��틪�ձ1z.$�?�U���

    0 讨论(0)
提交回复
热议问题