decryption problems

This is a discussion on decryption problems within the C Programming forums, part of the General Programming Boards category; I have a small encryption/decryption prog. It encrypts the way I want it to, but I am having trouble reversing ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    10

    decryption problems

    I have a small encryption/decryption prog. It encrypts the way I want it to, but I am having trouble reversing the process to decrypt the string. Please look at the code and watch the encryption process to understand its pattern. If you have more questions than what my comments have, please ask. thanks
    Code:
    ****************************************************
    
    /******************************
    * Water Works Encyrption Prog *
    *							  *
    *	     Created By:          *
    *                             *
    *		Travis Waters		  *
    *						      *
    ******************************/
    
    #include <stdio.h>
    #include <string.h>
    
    void shiftleft(char *in, char *out, unsigned int i, unsigned int c);
    void shiftright(char *in, char *out, unsigned int i, unsigned int c);
    char mapup(char in, int amt);
    char mapdown(char in, int amt);
    void encrypt();
    void decrypt();
    /* Main simply asks for input and jumps to the correct function for the selected option */
    int main()
    {
    	unsigned int select;
    	while(1)
    	{
    		printf("1 - Encrypt\n2 - Decrypt\n3 - Quit ");
    		printf("\nWhat is your selection:");
    		scanf("%u", &select);
    		
    		if(select == 1)
    		{
    			encrypt();
    		}
    		if(select == 2)
    		{
    			decrypt();
    		}
    		if(select == 3)
    		{
    			return 0;
    		}
    		else
    		{
    			printf("INVALID OPTION\n");
    		}
    	}
    }
    /* Asks for a string to encrypt, then the number of cycles to encrypt*/
    void encrypt()
    {
    	char in[10];
    	char out[10];
    	unsigned int i, b;
    	unsigned int c;
    	
    	printf("Input String to Encrypt:");
    	scanf("%s", in);
    
    	printf("How many encryptions:");
    	scanf("%u", &b);
    	
    	for(i = 0; i <= b; i++)
    	{
    		c = i % strlen(in)
    		shiftleft(in, out, c, i); //Shifts the string left i % strlen(in) times
    		strcpy(in, out); 
    	}
    }
    /* decrypts a given encrypted string */
    void decrypt()
    {
    	char in[10];
    	char out[10];
    	unsigned int i, b;
    	int c;
    
    	printf("Input String to Decrypt:");
    	scanf("%s", in);
    
    	printf("How many decryptions:");
    	scanf("%u", &b);
    
    	for(i = 0; i <= b; i++)
    	{
    		c = i % (strlen(in) - 1);
    		shiftright(in, out, c, i);
    		strcpy(in, out);
    	}
    }
    /* used within encrypt()
    	Shifts the input string left "i" times
    */
    void shiftleft(char *in, char *out, unsigned int i, unsigned int c)
    {
    	char buf[10];
    	unsigned int b;
    	unsigned int inlen;
    	
    	inlen = strlen(in);
    	printf("%i\n", i);
    	for(b = 0; b < i; b++)
    	{
    		buf[ b] = mapup(in[ b], i); //mapup increases any character that falls
    	}							  //off the string by the same number that the string
    	buf[i] = 0;					  //was shifted.
    	
    	for(b = i; b < inlen; b++)
    	{
    		out[b - i] = in[ b];
    	}
    	
    	out[inlen-i] = 0;
    	strcat(out, buf);
    	printf("%i pass: %s\n", c, out);
    	
    }
    /*Used within decrypt to do the exact opposite
    of shiftleft()...this is where my problems are.
    mapdown() is used to drop a character the same 
    number it was shifted.  The trouble is to get 
    the function to shift correctly, i must do it
    in the exact opposite order that was done to 
    encrypt the string. Encrypt starts at 0 shifts, 
    then 1 shift, then 2, 3, etc.  To decrypt i must 
    start with 3, then 2, then 1.
    */
    void shiftright(char *in, char *out, unsigned int i, unsigned int c)
    {
    	char buf[10];
    	unsigned int b;
    	unsigned int p;
    	unsigned int q;
    	unsigned int inlen;
    	
    	inlen = strlen(in);
    
    	for(b = 0, p = i; b <= i; b++, p--)
    	{
    		out[ b] = mapdown(in[inlen - p], i);
    	}
    	out[i] = 0;
     
    	for(b = 0; b < inlen + 1; b++)
    	{
    		buf[ b] = in[ b];
    	}
    	
    	buf[b - i - 1] = 0;
    	printf("%s\n", buf);
    
    	//printf("%s + %s = ", out, buf);
    	strcat(out, buf);
    	printf("%i pass: %s\n", c, out);
    }
    /*the if statements remove a bunch
    of the ascii table out of the available 
    characters.  now only " through ~ are available
    */
    char mapup(char in, int amt)
    {
    	unsigned char ret;
    	
    	memset(&ret, 0, 1);
    	ret = in + amt;  //increases the input character by the number amt
    					 // example: A becomes B
    	if(ret > 126)
    	{
    		ret -= 93;
    		return ret;
    	}
    	if(ret < 34)
    	{
    		ret += 34;
    		return ret;
    	}
    	return ret;
    }
    
    char mapdown(char in, int amt)
    {
    	unsigned char ret;
    
    	memset(&ret, 0, 1);
    	ret = in - amt;		//decreases the input character by the number amt
    						//example, B becomes A
    	if(ret > 126)
    	{
    		ret -= 34;
    		return ret;
    	}
    	if(ret < 34)
    	{
    		ret += 93;
    		return ret;
    	}
    	return ret;
    }

    Code tags added by Hammer

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please use code tags when posting code, see my signature for an example and a link to further help on this matter.

    And please can you be more specific. I (and others probably) don't really want to have to read 200 lines of code to understand how your program works, and what problems your are having.

    Can you give examples of what it's doing right/wrong, and point out where you think it's going wrong in the code.

    [edit]Ooops, I didn't see your comments in the middle... I'll have a closer read next time....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  4. Encryption and Decryption Program in C++ using a class
    By goron350 in forum C++ Programming
    Replies: 7
    Last Post: 06-05-2005, 09:29 PM
  5. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21