Join the social network of Tech Nerds, increase skill rank, get work, manage projects...

• # Encrypt a message

• 0
• 0
• 0
• 1
• 0
• 0
• 0
• 406

I have to write a function to encrypt a message given as a string input using the given encryption key. The function should output the encrypted message as a string to encryptedMessage in the argument list. The function prototype must be as follows: void encryptMessage(char *encryptedMessage, char *message, char *encryptionKey); The function must take the encryption key and convert each of its characters, which represent hexadecimal digits, to their equivalent decimal values as integers. I already wrote a fucntion to convert hex2decimal. The message must then be encrypted by adding the first of these integer values to the ASCII value of the first character in the message, and the second of the integer values to the second character in the message, and so on, and start again with the first integer value after every 16. This will be necessary if the message is longer than the encryption key, which will usually be the case.

here is some of my code so far:

void encryptMessage(char encryptedMessage, char *message, char *encryptionKey) { int *arr = malloc(sizeof(int)getStringLength(encryptionKey)); int i = 0;

```while(encryptionKey[i]!='\0'){
arr[i] = hexDigit2Dec(encryptionKey[i]);
Message[i] =(char)arr[i] + message[i];
i++;
if(i >= 15){
i = 0;
}
}
```

}

• over 5 years ago

hi there,

```#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LIM 1000

void hexToDec(int a[],char *key, int n)
{
int i;
int keyLen;

keyLen = strlen(key);
for ( i = 0; i < keyLen; i++) {
if( key[i] > 64 && key[i] < 71) {
a[i] = key[i] - 55;
} else {
a[i] = key[i]-48;
}
}

if ( i < n) {
int value = 0;
while( i != n) {
a[i] = value;
value++;
i++;
if ( value == 16) {
value = 0;
}
}
}

return;
}

void fun(char *encryptedMessage, char *message, char *encryptionKey)
{

int msgLen = strlen(message);
int arr[msgLen];
int i;

hexToDec(arr,encryptionKey,msgLen); // used to fill the array arr to repective decimal value

for ( i = 0; i < msgLen; i++) {
encryptedMessage[i] = message[i] + arr[i];
}
encryptedMessage[i] = '\n';

return;
}

int main()
{
char message[LIM];
char encryptionKey[LIM];
char encryptedMessage[] = "";
printf("Enter the message : ");
scanf("%[^\n]%*c",message);

printf("Enter the encryption key : ");
scanf("%[^\n]%*c",encryptionKey);

printf("Message : %s\n",message);
fun(encryptedMessage,message,encryptionKey);
printf("Encrypted Message : %s\n",encryptedMessage);

return 0;
}
```
OR
OR
Register

OR
Fill out the form below and instructions to reset your password will be emailed to you:

• Hire
• Post Projects

### Post Projects

• All at 0 Cost ....
• Post Tech Job
• Select Best Bidder
• Track the Project
• Approve Work and Pay safely
• Browse Nerds
• Work
• Find Projects Find Projects
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
View more...
View less...
• Manage
• Company Company

### Manage Company

• All at 0 Cost ....
• Manage Company and Employee Profiles
• Company wide Employee Productivity Reports
• Knowledge Sharing and Collaboration Tools
• Get Sales Lead and Bid for Tech Projects
• Send Invoices and Receive Payment Safely
• Learn
• Nerd Digest Nerd Digest
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
View more...
View less...
• Tech Q & A Tech Q & A
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
View more...
View less...