Encryption and Decryption
I have a school assignment in which asks me to create a program that does the following on encryption and decryption:
Write a program that encrypts and decrypts a text (string). The program reads a text into a character pointer: char * originaltext. Write a function that encrypts originaltext and store the encrypted text into: char * ciphertext. The encryption algorithm is replacing each character by a character having 10 added to its ASCII code. Write another function that decrypts ciphertext and store the result into: char * deciphertext. The decryption algorithm is replacing each character by a character having 10 subtracted from its ASCII code. Display the three strings originaltext, ciphertext,and deciphertext.
It then gives the following hints to start the program:
- Use malloc or calloc functions to allocate dynamically a memory space for strings.
- Assume that the text has a maximum of 100 characters.
- Don’t forget to deallocate the memory using free function.
Could somebody help me to write the program, I'm really stuck and don't know how to start.
Modified Encryption Attempt
Ok so I modified the program slightly and got it to display the original string I type, but I can't get the encrypt and decrypt functions
Code:
#include <stdio.h> // Calls for the libraries
#include <string.h>
#include <stdlib.h>
int encrypt (char * originaltext) // Function to encrypt a string
{
char * ciphertext;
if (ciphertext)
{
ciphertext = (char *)malloc(100);
*ciphertext = *originaltext + 10;
return *originaltext + 10; // Exits encrypt function
}
}
int decrypt (char * originaltext) // Function to decrypt a string
{
char * ciphertext;
char * deciphertext;
if (deciphertext)
{
deciphertext = (char *)malloc(100);
*ciphertext = *originaltext + 10;
*deciphertext = *ciphertext - 10;
return *ciphertext - 10; // Exits decrypt function
}
}
int main (void) // Start of program
{
char * originaltext; // Defines pointers
char * ciphertext;
char * deciphertext;
if ((originaltext=(char *)malloc(100))==NULL) // Sets the maximum number of characters for string
{
printf("\nMemory allocation error!\n");
exit(1);
}
printf("Type In Your String:\n\n"); // Asks for the string to be typed
scanf("%100s",originaltext); // Allows user to type string
encrypt (originaltext); // Calls for the encrypt function
decrypt (originaltext); // Calls for the decrypt function
printf("\n\nOriginal String:\n\n %s \n\nLength Of Original String: %i",originaltext, strlen(originaltext)); // Displays the original string
printf("\n\nEncrypted String:\n\n %s \n\nLength Of Encrypted String: %i",ciphertext, strlen(ciphertext)); // Displays the encrypted string
printf("\n\nDecrypted String:\n\n %s \n\nLength Of Decrypted String: %i",deciphertext, strlen(deciphertext)); // Displays the decrypted string
free(originaltext); // Frees the original string
getchar();
getchar();
return 0; // End of program
}
I'm using *ciphertext = *originaltext + 10; for encrypt because it's supposed to lead to each character of a string replaced by a character having 10 added, and I'm using *deciphertext = *ciphertext - 10: for decrypt as it's supposed to be the previous *ciphertext subtracted by 10 (I would think that this means *deciphertext points the same as *originaltext)