Thread: Encryption problem

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    47

    Encryption problem

    I am encrypting text from a file and storing the newly encrypted message in a text file.

    If the ith char is an uppercase letter, shift it to be the char x[i] away where x = {3, -1, 0, 2, -2}
    If ith char is a lower case letter, shift it to be the char y[i] away where y = {2, 4, -1, -2}
    If ith char is a digit, shift it to be the char z[i] away where z = {1, -1, 2}
    If a char is a non-letter and non-digit, then it does not change

    Also A + 1 would become Z, z - 1 becomes a, and 9 + 1 becomes 0

    Example:
    “Hello!” would have the “H” shift 3 further away (“H” becomes “K”), “e” would be shift 4 away (becoming “i”), “l” shifts -1 away (becoming “k”), the second “l” shifts -2 away (“l” becomes “j”), “o” shifts 2 away since we reached the end of the y array (“o” becomes “q”). and “!” remains the same.

    So far I have:

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    	/*	array for shifting an uppercase letter */
    	int x[] = {3, -1, 0, 2, -2};
    
    	/*	tarray for shifting a lowercase letter */
    	int y[] = {2, 4, -1, -2};
    
    	/*	array for shifting a digit */
    	int z[] = {1, -1, 2};
    
    	int *xNext = x; 
    	int *yNext = y; 
    	int *zNext = z;
    
    	FILE *fp1, *fp2;			// file pointers
    
    	const int xArraySize = 5;
    	const int yArraySize = 4;
    	const int zArraySize = 3;
    
    	int totalChars = 0;
    	int iter = 0;
    
    	char inputTextArray[5000];
    	char inputFilename[30];		// Character Array to hold the users specified input file name
    	char currentCharacter;		//current character input from the file
    	char outputFilename[30];	// holds the users specified output file name
    	char *textArrayPointer = inputTextArray;
    
    	printf("Enter the name of the file to be scanned: "); 	
    	scanf("%s", inputFilename);			// Scans the input file specified by the user 		
    	fp1 = fopen(inputFilename, "r");	// open the file as read-only 
    
    	
    	printf("Enter the name of the file you wish to copy %s to: ",  inputFilename);	// ask user for outputFilename (copy of inputFilename)
    	scanf("%s", outputFilename);
    	fp2 = fopen(outputFilename, "w");	// open output file, write-only
    	
    
    	while((currentCharacter = getc(fp1)) != EOF)
    	{
    		*textArrayPointer = currentCharacter;
    		textArrayPointer++;
    	} 
    	
    
    
    	 while(*textArrayPointer != '\0')
    	{
    		/*	Check for capital letters A through Z
    			A in ASCII is 65, and Z is 90	*/
    		if (( *textArrayPointer >= 65 ) && ( *textArrayPointer <= 90 ))
    		{
    
    			iter = totalChars % xArraySize;
    			while(iter-- != 0)
    			{
    				xNext++;
    			}
    
    			*textArrayPointer = *textArrayPointer + *xNext;
    			if ( *textArrayPointer < 65)
    			{
    				*textArrayPointer += 26;
    				
    			}
    		
    			if ( *textArrayPointer > 90)
    			{
    				*textArrayPointer -= 26;
    			}
    
    
    			putc(*inputTextArray, fp2);
    		}
    
    		/*	Check for lower case letters a through z
    			a in ASCII is 97, and z is 122	*/
    		else if (( *textArrayPointer >= 97 ) && ( *textArrayPointer <= 122 ))
    		{
    
    			iter = totalChars % yArraySize;
    			while(iter-- != 0)
    			yNext++;
    		
    			*textArrayPointer = *textArrayPointer + *yNext;
    			if ( *textArrayPointer < 97)
    			{
    				*textArrayPointer += 26;
    			}
    
    			if ( *textArrayPointer > 122)
    			{
    				*textArrayPointer -= 26;
    			}
    
    			putc(*inputTextArray, fp2);
    		}
    
    		/*	Check for numbers 0 through 9
    			0 in ASCII is 48, and 9 is 57	*/
    		else if (( *textArrayPointer >= 48 ) && ( *textArrayPointer <= 57 ))
    		{
    
    			iter = totalChars % zArraySize;
    			while(iter-- != 0)
    			zNext++;
    
    			*textArrayPointer = *textArrayPointer + *zNext;
    			if ( *textArrayPointer < 48)
    			{
    				*textArrayPointer += 26;
    			}
    
    			if ( *textArrayPointer > 57)
    			{
    				*textArrayPointer -= 26;
    			}
    
    			putc(*inputTextArray, fp2);
    		}
    
    		/*	The criteria did not meet the previous
    			conditions. The character must be a symbol	*/
    		else 
    		{
    			putc(*inputTextArray, fp2);
    		}
    
    		*textArrayPointer++;
    		totalChars++;
    
    
    	}
    }
    I get a run time error and I am not sure what I have done wrong. Any assistance would be much appreciated.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    while((currentCharacter = getc(fp1)) != EOF)
    	{
    		*textArrayPointer = currentCharacter;
    		textArrayPointer++;
    	} 
    	
    
    
    	 while(*textArrayPointer != '\0')
    You need to "rewind" your pointer before starting this loop in red here. (Also, checking that you don't run off the end of your buffer would be a good idea.)

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    47
    I added:

    Code:
    	while((getc(fp1)) != EOF)
    	{
    		textArrayPointer--;
    	}
    after:

    Code:
    while((currentCharacter = getc(fp1)) != EOF)
    	{
    		*textArrayPointer = currentCharacter;
    		textArrayPointer++;
    	}
    but I don't think that was the correct solution because my output file has 4,896 "T"'s when the input file had only 85 characters.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Why are you reading the characters into an array? You're processing them one at a time anyway...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption problem!!
    By muran_pling in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2006, 07:55 AM
  2. help needed with edit control & encryption
    By willc0de4food in forum Windows Programming
    Replies: 2
    Last Post: 03-16-2006, 08:21 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM