ok, heres the whole code, apologies that it is quite messy
(Dec2bin is a function that converts a decimal value into a binary string)
Also, this is schoolwork---its not for production! Any help appreciated
Code:
// Function gets the message to be encrypted (from a binary file)
#include <stdio.h> // Code to get message
#include <math.h>
#include <stdlib.h>
#include <string.h>
FILE *pfile = NULL; //declare a file pointer for file input/output
void getmsg (void)
{
char nbin[100]; //binary conversion of n
char e_array_bin[500]; //binary store encrypted message
char t_array_bin[500]; //temp binary store encrypted message
char extra[50]="0";
int msglength=0; // variable to store the length of the message
int nlength=0; // variable to store the length of nbin
int encryptlength=0;
int blocklength=0;
int messages=0;
int emsg;
char msg[500];
int m_array[50]; //array to store blocks
int e_array[500];
char txt_array[500];
int n=0;
int m=1;
int i=0;
int j=0;
int count=0;
int start=0;
int sum=0;
int x=0;
int num=0;
char *filename4 = "\message.txt";
//Get the message from the 'message' file
pfile = fopen(filename4, "r");
if (pfile == NULL)
{
printf("Erorr creating file %s for reading. Terminating....", filename4);
getchar();
}
fscanf(pfile, "%s", &msg);
printf("\nValue retrieved: %s\n",msg);
fclose(pfile);
getchar();
// gets the length of the message in bits
msglength=strlen(msg)-1;
printf("The length of the message is %d\n\n", msglength);
getchar();
n=get_n(); //get n from public key
// Convert the value of n into a binary string
dec2bin(n,nbin);
// gets the length of n in bits
nlength=strlen(nbin)-1;
printf("The length of the n is %d bits\n\n", nlength);
getchar();
//gets the length of n-1 in bits
encryptlength=nlength-1;
printf("The no. of bits to be encrypted per time is %d", encryptlength);
getchar();
//Pad the zeros out when the message does not split up evenly
while ((msglength/encryptlength)%2 != 0)
{
strcat(msg, extra); // Adds a '0' to the end of the message
msglength++;
}
printf("Message is now %s\n", msg);
// Get the number of individual messages to be encrypted
messages=msglength/encryptlength;
printf("Messages to encrypt is %d", messages);
getchar();
count=encryptlength; //count set to no of bits we need to sum for each block
printf("The value of count is %d", count);
getchar();
x=encryptlength-1;
for (i=0; i<messages; i++)
{
for (j=start; j<count; j++)
{
if (msg[j] == '1')
{
num = pow(2,x);// Multiply by power of 2 relevant
sum=sum+num; //sum will be in decimal
//printf("Sum = %d\n", sum);
}
x--;
}
printf("The message:%d is %d\n", m, sum );
m++;
m_array[i]=sum;
//printf("The value to be put in the array is %d\n", sum);
sum=0;
x=encryptlength-1;
start=count;
count=count+encryptlength;
getchar();
}
printf("Blocks to encrypt are as follows: ");
for (i=0; i<messages; i++)
{
printf("%d ", m_array[i]);
}
getchar();
//Display the results of encrypting each block
sum=0;
for (i=0; i<messages; i++)
{
e_array[i]=encrypt(m_array[i]);
printf("Encrypted value is: %d\n ", e_array[i]);
getchar();
}
//Display encrypted message
printf("Encrypted message:");
for (i=0; i<messages; i++)
{
printf("%d", e_array[i]);
}
//Convert whole encrypted string to binary
for (i=0; i<messages; i++)
{
dec2bin(e_array[i], t_array_bin);
printf("\nBinary message of %d is %s\n", e_array[i], t_array_bin);
strcat(e_array_bin, t_array_bin);
}
printf("\nThe binary message is %s", e_array_bin);
getchar();
store_emessage(e_array_bin); //put in file */
}