Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can I get a brief description of this C code to understand more. #include \"Decr

ID: 3827237 • Letter: C

Question

Can I get a brief description of this C code to understand more.

#include "Decrypt.h"

#include <stdio.h>

#include <string>

#include <iostream>

#include <stdlib.h>

#include <string.h>

#include <time.h>

#include <openssl/aes.h>

#include <openssl/rand.h>

static void hex_print(const void* pv, size_t len)

{

   const unsigned char * p = (const unsigned char*)pv;

   if (NULL == pv)

       printf("NULL");

   else

   {

       size_t i = 0;

       for (; i < len; ++i)

           printf("%02X ", *p++);

   }

   printf(" ");

}

void generateDecryptInfor(char* &EncryptedInfor)

{

   int keylength;

   unsigned char iv_enc[AES_BLOCK_SIZE], iv_dec[AES_BLOCK_SIZE];

   memcpy(iv_dec, iv_enc, AES_BLOCK_SIZE);

   size_t inputslength = sizeof(USR_INFOR);

   const size_t encslength = ((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;

   unsigned char* enc_out = new unsigned char[encslength];

   unsigned char* dec_out = new unsigned char[inputslength];

   AES_KEY enc_key, dec_key;

   //read in the encrypted information.

   char c1, c2;

   c1 = EncryptedInfor[0];

   c2 = EncryptedInfor[1];

   if (c1=='6'&&c2=='9')

   {

       keylength = 128;

   }

   else if (c1 == '8'&&c2 == '2')

   {

       keylength = 192;

   }

   else if (c1 == '7'&&c2 == '5')

   {

       keylength = 256;

   }

   else

   {

       cout << "invalid information!" << endl;

   }

   unsigned char* aes_key = new unsigned char[keylength / 8];

   for (int i = 0; i < strlen((char*)aes_key); i++)

   {

      

           aes_key[i] = EncryptedInfor[i+2];

   }

   for (int i = 0; i < strlen((char*)enc_out); i++)

   {

      

       enc_out[i] = EncryptedInfor[i + 2 + strlen((char*)aes_key)];

      

   }

   for (int i = 0; i < strlen((char*)iv_dec)-1; i++)// this part took about 2 weeks for debugging. we should take one off because there is a extra ''.

   {

  

       iv_dec[i] = EncryptedInfor[i + 2 + strlen((char*)aes_key) + strlen((char*)enc_out)];

   }

   // decrypt the information.

   AES_set_decrypt_key(aes_key, keylength, &dec_key);

   AES_cbc_encrypt(enc_out, dec_out, encslength, &dec_key, iv_dec, AES_DECRYPT);

   //print out the encrypted and decrypted information in hexadecimal

   cout<<"encrypt:"<<endl;

   hex_print(enc_out, sizeof(enc_out));

   cout << "decrypt:" << endl;

   hex_print(dec_out, sizeof(dec_out));

   //print out the decrypted information

   USR_INFOR * dyc = (USR_INFOR *)dec_out;

   cout<< "user name :"<<dyc->username<<endl;

   cout << "user Password :" << dyc->password << endl;

   return ;

}

Explanation / Answer

code is for encryption and decryption

1.it read first two characters of EncryptedInfor array and decide key length.

2.aes_key is char array and size is keylength/8.

3.EncrypedInfo is copied to aes_key

4.enc_out is char array and length equal to encslength. it is for encrypted output.

5.dec_out is char array and length equal to inputslength. it is for decrypted output.

6.enc_out,dec_out is printed on screen in hexadecimal form using hex_print()

7.dec_out is typecasted to dyc pointer.

8.username and pwd is printed using this dyc pointer.

c1 c2 key length 6 9 128 8 2 192 7 5 256
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote