Thread: My decrypt does not work. What am I doing wrong?

  1. #1
    Registered User
    Join Date
    Jun 2020
    Posts
    9

    My decrypt does not work. What am I doing wrong?

    I am trying to finish this project and my decrypt does not work.
    Key should be an array and should be updated at each character.

    Code:
    #include <stdio.h>
    #include<string.h>
    #include<ctype.h>
    #include<math.h>
    
    
    int key=65;
    
    
    unsigned int easyendecrypt(char c);
    
    
    int main(void) {
    	int c;
    
    
    	while ((c = getc(stdin)) !=EOF) {
    		if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
    			putc(easyendecrypt(c), stdout);
    			key = c;
    		}
    		else {
    			scanf("%*s %d", &c);	
    			printf("Error", stderr);
    		}
    	}
    	return 0;
    }
    unsigned int easyendecrypt(char c) {
    	unsigned int k;
    
    #ifdef DECRYPT
    	fprintf(stderr, "decrypting with %c\n",key);
    	k = c - key;
    	key = k;
    	return k;
    #else
    	fprintf(stderr,"encrypting with %c\n",key);
    	k = c + key;
    	key = c;
    	return k;
    #endif
    }
    

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,839
    Looking at your past posts, you mentioned trying to implement an autokey cipher. Going by the Wikipedia article, this involves the use of a "primer" that is prepended to the plaintext to generate the key, and then the plaintext and the key are encrypted together character by character through a tabula recta... although you don't need such a lookup table if you're going with ASCII as you then use the current key character to compute the offset which is then applied to the current plaintext character to obtain the current ciphertext character. This character-level encryption appears to be what you have done in your code; what you need to do is to extend it to an entire key, not just a key character.

    Consequently, I would expect a pair of functions like these:
    Code:
    void encrypt(const char *plaintext, const char *primer, char *ciphertext);
    void decrypt(const char *ciphertext, const char *primer, char *plaintext);
    You can combine them into a single function, but resist that urge for now: you can always do so later. For now, it is more important to keep in mind what you're trying to implement, i.e., encryption or decryption. You should not be using a macro constant to determine whether to encrypt or decrypt as that would mean you have to recompile your program to switch functionality.

    Notice that for encrypt, I declared plaintext and primer with the const keyword, indicating that their contents are not to be changed. You should assume that what ciphertext points to is long enough to store the entire ciphertext... if you want to do this in a more secure way (i.e., to make a buffer overflow vulnerability less likely), there should be an additional parameter for the maximum length of the ciphertext.

    Instead of dynamically allocating space for the key, what you can do is to first iterate over both the plaintext and the primer to encrypt (but remember to terminate the loop as soon as you reach the end of either of them), and then iterate over the remaining portion of the plaintext and the plaintext (used as the key). As you iterate, you write the encrypted character to successive characters of the ciphertext (or you just write the character as-is to the ciphertext, if it is not alphabetic).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2020
    Posts
    9
    How would I combine them into a single function?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,839
    Quote Originally Posted by maryprogrammer
    How would I combine them into a single function?
    By using another parameter to determine which functionality to invoke. But that's irrelevant until you have implemented them, so don't ask me for an example until you show us your implementation of encrypt and decrypt.

    Speaking of that, start with encrypt. You can do decrypt after you're done with encrypt.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-11-2017, 06:19 AM
  2. Decrypt help
    By Tot Eu Ma in forum C++ Programming
    Replies: 8
    Last Post: 10-05-2013, 02:01 PM
  3. My code is wrong? It doesn't work! Plz Help.
    By antirain in forum C++ Programming
    Replies: 2
    Last Post: 05-21-2013, 06:42 AM
  4. Decrypt this!
    By francoissoft in forum Contests Board
    Replies: 21
    Last Post: 07-29-2007, 11:11 AM
  5. Replies: 3
    Last Post: 12-05-2003, 06:36 PM

Tags for this Thread