VB icon

Encryption

Email
Submitted on: 1/2/2015 7:06:00 AM
By: Deepti Kotnis (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 637
 
     This code implements 3 encryption algorithms one after another i.e. 3 level encryption is achieved. 3 Algorithms used are: 1.Reversing Algorithm 2.Grille's Method 3.Word-Alphabet Correlation Algorithm
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :Encryption
//**************************************
#include <stdio.h>
#include <string.h>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Encryption
// Description:This code implements 3 encryption algorithms one after another i.e. 3 level encryption is achieved.
3 Algorithms used are:
1.Reversing Algorithm
2.Grille's Method
3.Word-Alphabet Correlation Algorithm
// By: Deepti Kotnis (from psc cd)
//
// Inputs:Enter any word or phrase without any white spaces containing only lowercase letters for best results.
//
// Returns:Gives you back the word/phrase entered in the encrypted format.
//**************************************

#include <stdio.h>
#include <string.h>
void Encryption(char *originalStr);
void Decryption(int *encrypt,int origlen,int encryptlen);
#define kEncryptionKey 3
char GrillArray[2][100],phrase[] = {"the quick brown fox jumps over the lazy dog"}; 
int grilllen = 0;
int main (void)
{
char originalStr[100];
printf("please enter a word or phrase without any white spaces containing only lowercase characters(a-z):");
scanf("%s",originalStr);
Encryption(originalStr);
return 0;
}
void Encryption(char *originalStr)
{
	char rev[100],grill[100];
	int encrypt[100];
	int choice = 0,origlen = strlen(originalStr);
	
	int i,k,l,m=0,index=0,wordCnt=0,letterCnt=0;
	
	k = strlen(originalStr)-1;
	for(i=0;i<strlen(originalStr);i++)
	{
		rev[i] = originalStr[k];
		k--;
	}
	rev[i] = '\0';
	
	k = 0;
	for(i=0;i<strlen(originalStr);i++)
	{
		if(i==index)
		{
			GrillArray[0][k] = i;
			GrillArray[1][k] = rev[i];
			index = index + kEncryptionKey;
			k++;
		}
		else
		{
			grill[m] = rev[i];
			m++;
		}
		
	}
	grilllen = k;
	grill[m] = '\0';
	
	k = 0;
	i=0;
	for(l=0;l<strlen(grill);l++)
	{
		k = 0;
		wordCnt = 0;
		letterCnt = 1;
		while(grill[l] != phrase[k])
		{
			if(phrase[k] == ' ')
			{
				wordCnt++;
				letterCnt = 0;
			}
			k++;
			if(phrase[k] != ' ')
				letterCnt++;
		}
		if(grill[l] == phrase[k])
		{
			encrypt[i] = wordCnt+1;
			i++;
			encrypt[i] = letterCnt;
			i++;
		}
	}
	encrypt[i] = '\0';
	
	printf("\nEncrypted phrase is:");
	for(l=0;l<i;l++)
		printf("%d",encrypt[l]);
		
	printf("\n\nDo you want to decrypt it? 0:YES 1:NO\n");
	scanf("%d",&choice);
	
	if(choice == 0)
		Decryption(encrypt,origlen,i);
	else
		printf("Thanx");
}
void Decryption(int *encrypt,int origlen,int encryptlen)
{
	int i,wordCnt=0,k=0,letterCnt,j,m=0,firstindex=0,secondindex;
	char word[6],first[100],second[100],decrypt[100];
	for(i=0;i<encryptlen;i=i+2)
	{
		wordCnt=0,letterCnt=1;
		k=0;
		while(wordCnt != encrypt[i])
		{
			if(phrase[k] == ' ')
			{
				wordCnt++;
				letterCnt = 0;
			}
			letterCnt++;
			k++;
		}
		j=k-2;
		while(phrase[j] != ' ')
		{
			j--;
			if(j == 0)
				break;
		}
		if(j!=0)
			j++;
		m=0;
		while(phrase[j] != ' ')
		{
			word[m] = phrase[j];
			j++;
			m++;
		}
		word[m] = '\0';
		m=0;
		while(m != encrypt[i+1])
			m++;
		first[firstindex] = word[m-1];
		firstindex++;
	}
	first[firstindex] = '\0';
	
	secondindex = 0;
	i=0;
	j=0;
	while(secondindex < origlen)
	{
		if(secondindex == GrillArray[0][i])
		{
			second[secondindex] = GrillArray[1][i];
			i++;
		}
		else
		{
			second[secondindex] = first[j];
			j++;
		}
		secondindex++;
	}
	second[secondindex] = '\0';	
	
	k = strlen(second)-1;
	for(i=0;i<strlen(second);i++)
	{
		decrypt[i] = second[k];
		k--;	
	}
	decrypt[i] = '\0';
	printf("\nDecrypted phrase is:%s",decrypt);
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.