-1

I have created two things for pset2 a caesar cipher encryptor and decryptor. My encryptor works and it is called caesar.c, but my decryptor(decryptor.c) doesn't work and I can't figure out why? Here is my code.

Caesar.c

#include <cs50.h> 
#include <string.h> 
#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 
#include <ctype.h>
#include <string.h>
#include "encryptorFunction.c"


int main(int argc, string argv[]) {
    //checks for enough args
    if (argc >= 2) {
        //converts the second arg to an integer
        int k = atoi(argv[1]);
        string stringToBeEncrypted = GetString();
        printf("%s\n", encryptor(stringToBeEncrypted, k));
    } else {
        printf("Please add a key argument\n");
        return 1;
    }
}

Decryptor.c

#include <cs50.h> 
#include <string.h> 
#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 
#include <ctype.h>
#include "encryptorFunction.c"

// almost same as caesar


int main(void) {
     const string stringToBeEncrypted = GetString();
        // string stringToBeEncrypted = GetString();
        //checks all possible decryptions
        for(int k=1;k<26;k++) {
            printf("%s\n", encryptor(stringToBeEncrypted, k));
        }
}

encryptorFunction.c

#include <cs50.h> 
#include <string.h> 
#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 
#include <ctype.h>

string encryptor(string encryptString, int key) {
    //loops through every character
    for (int i = 0, n = strlen(encryptString); i < n; i++) {
        //converts the specific char to its ascii value
        int ascii = (int) encryptString[i];
        //makes key 31 is the same as key 5
        key = key % 26;
        //checks if it is an alphabetical letter
        if (isalpha(encryptString[i])) {
            //checks if lowercase
            if (islower(encryptString[i])) {
                //handles the corner case of being z since 26 % 26 is not 26 it's 0
                if ((ascii-96+key) == 26) {
                    ascii = 122;
                }else {
                    //takes the lowercase alphabetical numbers to the 1-26 alphabet then checks mod 26 to cycle back and adds it back.
                    ascii = ((ascii-96+key) % 26) + 96;
                }
                //else part is for uppercase case
            }else {
                //same logic as above
                if ((ascii-64+key) == 26) {
                    ascii = 90;

                }else {
                    ascii = ((ascii-64+key) % 26) + 64;
                }
            }
        }
        //converts the ascii back to a char
        char newChar = (char) ascii;
        //sets the new char into the string
        encryptString[i] = newChar;
    }
    return encryptString;
}
2

I don't know where you are on the "comfortable" spectrum, but I will assume that you're closer to "more" since you are trying this and you are using const. I hope I can explain it adequately. This statement const string stringToBeEncrypted = GetString(); is declaring that you cannot change the value of stringToBeEncrypted, which is a char pointer. It is not, however, declaring that you cannot change the contents of that pointer. [Ditto encryptString in encryptor()] Each call to encryptorFunction changes the contents, so each subsequent call encrypts the previously encrypted value. You need to send the original contents of stringToBeEncrypted to each call to encryptor().

8
  • How would I send the original contents though? – Epic Defeater Apr 18 '16 at 18:45
  • You know that you do not want stringToBeEncrypted to mutate. You could create another char* /string, allocated on the heap for enough bytes to hold stbe (don't forget the null terminator!), populate it with a strcpy or strcat or memcopy, and send that as the argument to to encryptor(). (You would need to "refresh" it before every call, because it will be mutated by the function). To "close the loop" remember to free it so as not to leak memory. – DinoCoderSaurus Apr 18 '16 at 19:00
  • I thought C on default was call by value. – Epic Defeater Apr 18 '16 at 19:01
  • Could you give me some code? Also, I am a newbie to C. – Epic Defeater Apr 18 '16 at 19:02
  • You should be able to do this yourself once you complete week 4. This code will (probably) get you the result you want string orig = calloc(strlen(stringToBeEncrypted) + 1,1); for(int k=1;k<26;k++) { strcpy(orig,stringToBeEncrypted); printf("%s\n", encryptor(orig, k));} You'll need to continue with the course to understand why. – DinoCoderSaurus Apr 18 '16 at 19:45
0

I'm a bit confused here. Why would you need a decryptor? It isn't required for the PSET.

To decrypt the message you can use the exact same code you used to encrypt it, except the the key needs to be subtracted from 26.

1
  • I want to be able to decrypt anything you give me. I don't know the key, if I have to decrypt a string that a don't know the key to. – Epic Defeater Apr 18 '16 at 19:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .