Hey guys i am writing an encryption program that uses bitshifts to encrypt characters in a message and then receives a key from the user to perform an opposite shift to restore the original message.I divided the program into three functions: read_msg(), encrypt(), decrypt(). but it fails at the encryption part. i donot want to use another array there i just want to perform the bitshift on the same array and store the values.
Code:
/****************************************************************************************************************************
* FileName: crypto.c *
* Developer: Nyah Check, INK Corp. *
* Usage : This program encrypts data following a key entered by the user. and prints the encrypted message. *
* *
* Copyright: The code for this program is free to be distributed *
* and licenced freely following the opensource idea *
* no money should be demanded for its usage and the *
* user is responsible for what he/she does with it. *
* INK Corporation, 2012.. *
****************************************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 140
#define getchar() getc(stdin)
int read_line(unsigned char s[]);
int encrypt(unsigned char s[], int n);
int decrypt(unsigned char s[], int key);
int i, key;
/**************************************************************************************************************************************
*Main: This calls three functions read_line(), encrypt(), and decrypt() for the decryption sequence given by the key. *
* *
* *
**************************************************************************************************************************************/
int main()
{
unsigned char msg[MAX + 1];
printf("\nThanks for using our encryption program that encrypts text messages.Takes a maximum of 140 characters. ");
printf("\nDecryption is done using an input key. If you wish to quit the program, ENTER '&'(ampersand)...\n");
printf("\nINK Corporation, 2012.\n\n");
printf("\nEnter the plain text: ");
read_line(msg);
printf("\nEncrpted text: ");
encrypt(msg, i);
printf("Enter decryption code: ");
key = getc(stdin);
switch(key)
{
case ' ': case '\n': case '\0': printf("\nNo key entered. Try again!!!");
break;
default: decrypt (msg, key);
break;
}
return 0;
}
/**************************************************************************************************************************************
*Function Name: read_line(): *
*Function: This function reads input from the keyboard and then passes them to the encrypting function which then encrypts the *
* message.
*Precondition: (plaintext). *
*Postcondition: Encrypted message. *
**************************************************************************************************************************************/
int read_line( unsigned char s[])
{
int ch;
while ( (ch = getchar()) != '\n')
{
if ( ch == '&')
{
printf("\nThanks for trusting the services of INK Corporation");
printf(". We would like to receive your comments and feedbacks to better our services");
printf("\nINK Corporation, PALO ALTO CA. 2012.");
exit(EXIT_FAILURE);
}
else if ( i < MAX)
s[i++] = ch;
}
s[i] = '\0';
if (i < 0)
{
printf("A matching failure occured while writing message.");
exit(EXIT_FAILURE);
}
return i;
}
/******************************************************************************
*encrypt(): This function encrypts a message depending on a predetermined key*
* then prints the encrypted message. *
******************************************************************************/
int encrypt(unsigned char s[], int i)
{
int t = 0;
unsigned char nstr[i];
while ( s[t] != '\0')
{
if ( t < i)
nstr[t++] = 20 << s[t];
}
nstr[t] = '\0';
if (nstr == NULL)
{
printf("\nAn matching failure occured while encrypting message.");
exit(EXIT_FAILURE);
}
printf("%s\n",nstr);
return t;
}
/*********************************************************************************
*decrypt(): This function decrypts the message entered by the user depending on *
* the decryption key entered by the user. *
*********************************************************************************/
int decrypt( unsigned char s[], int key)
{
int res, t = 0;
while ( s[t] != '\0')
{
if ( t < i)
s[t++] = key >> s[t];
}
s[t] = '\0';
if (s == NULL)
{
printf("\nAn error occured while decrypting message.");
exit(EXIT_FAILURE);
}
printf("\nDecrypted message: %s", s);
printf("\nWas the Decryption Succesful (y, n)? ");
if ((res = getc(stdin)) == 'y' || res == 'Y')
printf("\nCongratulations on decrypting your messages.");
else
printf("\nTry better next time.. Courage!!!!");
return t;
}