Question

In: Computer Science

A big problem of the blocking behavior of /dev/random is that blocking can lead to denial...

A big problem of the blocking behavior of /dev/random is that blocking can lead to denial of service attacks. Therefore, it is recommended that we use /dev/urandom to get random numbers. To do that in our program, we just need to read directly from this device file. The following code snippet shows how:

#define LEN 16 // 128 bits
unsigned char *key = (unsigned char *) malloc(sizeof(unsigned char)*LEN);

FILE* random = fopen("/dev/urandom", "r"); fread(key, sizeof(unsigned char)*LEN, 1, random);

fclose(random);

Please modify the above code snippet to generate a 256-bit encryption key. Please compile and run your code; print out the numbers and include the screenshot in the report.

Solutions

Expert Solution

Code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LEN 64

char hexa(int n)   // Function to convert decimal to hexadecimal
{
   char array[16] ={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
   return(array[n]);
}

int main()
{

unsigned char *key = ( unsigned char *) malloc(sizeof(unsigned char)*LEN);
int a[LEN],i;
char en[LEN]="";  

FILE* random = fopen("/dev/urandom","r"); // It returns random characters
fread(key, sizeof(unsigned char)*LEN, 1, random); //Storing all the characters in key (a character array)

fclose(random);

/*All the random characters have int value less then 256,
storing their corresponding integer values (mod 16) in integer array*/

for(int i=0;i<LEN;i++)
{
   a[i] = ((int)key[i])%16;
}


for(int i=0;i<LEN;i++)
{
   en[i] = hexa(a[i]);
}
printf("Encryption key is:\n");

for(int i=0;i<LEN;i++)
{
   printf("%c",en[i]);
}

printf("\n");

return 0;
}

Here we are creating 256 bit hexadecimal encryption (as in AES 256-bit key). Each hexadecimal digit require 4 bit for representation, hence total number of characters in key will be 64.

Screenshot:


Related Solutions

In marketing, attitudes are usually considered to lead to behavior. However, in some cases a behavior...
In marketing, attitudes are usually considered to lead to behavior. However, in some cases a behavior may change an attitude. Please explain the theories behind this phenomenon
What can you say in general about the limiting behavior of a random walk on an...
What can you say in general about the limiting behavior of a random walk on an even-length cycle compared to that of the lazy random walk?
Budgeting can be costly, lengthy, and even lead to some pretty dysfunctional employee behavior, so why...
Budgeting can be costly, lengthy, and even lead to some pretty dysfunctional employee behavior, so why budget? Do the advantages really outweigh the disadvantages? Be specific.
Budgeting can be costly, lengthy, and even lead to some pretty dysfunctional employee behavior, so why...
Budgeting can be costly, lengthy, and even lead to some pretty dysfunctional employee behavior, so why budget? Do the advantages really outweigh the disadvantages? Be specific.
Budgeting can be costly, lengthy, and even lead to some pretty dysfunctional employee behavior, so why...
Budgeting can be costly, lengthy, and even lead to some pretty dysfunctional employee behavior, so why budget? Do the advantages really outweigh the disadvantages? Be specific. How does your company employ the budgeting process? At least 100 words.
* Example for instructions x is a Normally distributed random variable….. Mean = 100 Std dev...
* Example for instructions x is a Normally distributed random variable….. Mean = 100 Std dev = 25 a. What is the probability of a value of x that is more than 140? Z = 1.60 =(140-B6)/B7 Table prob = .4452 p(x>140) = .5-.4452 = 0.0548 The probability of a value of x that is more than 140 is 0.0548 *****Complete #3 two ways*********             i. Same as in Example above             ii. using the Excel NORMDIST and NORMINV functions...
budgeting can lead to some pretty dysfunctional employee behavior, so why budget? Do the advantages really...
budgeting can lead to some pretty dysfunctional employee behavior, so why budget? Do the advantages really outweigh the disadvantages?
1) The variation in the output of processes that are out of control can be entirely attributed to random behavior.
Please answer the following True or False  1) The variation in the output of processes that are out of control can be entirely attributed to random behavior. 2) The elimination of common causes of variation is typically the responsibility of workers, not management. 3) Special causes of variation can often be diagnosed and eliminated by workers or their immediate supervisors. 4) Most processes are naturally in a state of statistical control.
Random variable X is drawn from a normal distribution with mean 5.44 and std dev 2.54....
Random variable X is drawn from a normal distribution with mean 5.44 and std dev 2.54. Calculate the probability of X being less than 3.29. What is the probability of X exceeding 4.61? What is the probability of X lying between 5.79 and 7.8? Verify your answers to parts 1 2 and 3 above using numerical sampling.
mean=101 Std Dev=10 What is the probability that a random sample of 29 pregnancies has a...
mean=101 Std Dev=10 What is the probability that a random sample of 29 pregnancies has a mean gestation period of 97 days or​ less? The probability that the mean of a a random sample of 29 pregnancies is less than 97 days is approximately. Please show formula and work that is easy to understand, thank you!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT