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

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 646
    Answer it

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

    }

    when i run it project.exe has stopped pops up.Please help me out

 1 Answer(s)

  • hi there,

         This might help you to encrypt your message :)

    #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;
    }
    
Sign In
                           OR                           
                           OR                           
Register

Sign up using

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