-2

I have encrypted a message using RSA algorithm in C programming language. I want to encrypt multiple files which is kept in a particular folder using same key. Iam working in openSSL environment. The code that i used to encrypt a particular message is,

 // Get the message to encrypt
printf("Message to encrypt: ");
fgets(msg, KEY_LENGTH-1, stdin);
msg[strlen(msg)-1] = '\0';

// Encrypt the message
encrypt = malloc(RSA_size(keypair));
int encrypt_len;
err = malloc(130);
if((encrypt_len = RSA_public_encrypt(strlen(msg)+1, (unsigned char*)msg, (unsigned char*)encrypt,
                                     keypair, RSA_PKCS1_OAEP_PADDING)) == -1) {
    ERR_load_crypto_strings();
    ERR_error_string(ERR_get_error(), err);
    fprintf(stderr, "Error encrypting message: %s\n", err);
    goto free_stuff;
}

Now i want to use RSA algorithm to encrypt a folder which contains many files using the same public key in C

Thanks in advance!

Arjun
  • 63
  • 6

2 Answers2

0

You can find information on how to traverse directories in UNIX-based Oses in this SO question.

If you need something multiplatform, this also can be found in StackOverflow here.

Maybe the best advice for you is to learn how to pinpoint your needs and make some good researches.

Community
  • 1
  • 1
Magix
  • 4,989
  • 7
  • 26
  • 50
0

You'll need to use the opendir and readdir functions to open a directory and iterate through the files in it. Then for each file name, check to see if it's a regular file and if so open it up and do the encryption.

Files will most likely be larger than the RSA key size, so you'll need to encrypt it in pieces.

Here's a quick example:

void encrypt_files(char *dirname)
{
    DIR *dir;
    struct dirent *de;
    int rval;
    struct stat statbuf;
    FILE *f;

    if ((dir = opendir(dirname)) == NULL) {
        perror("Failed to open directory %s", dirname);
        return;
    }

    // always reset errno before calling readdir
    // otherwise a false error could be reported when you reach the end of the directory
    while ((errno = 0, de = readdir(dir)) != NULL) {
        rval = lstat(de->d_name, &statbuf);
        if (rval == -1) {
            perror("Error getting file status for %s", de->d_name);
            return;
        }
        if (S_ISREG(statbuf.st_mode)) {
            f = fopen(de->d_name, "r");
            if (f == NULL) {
                perror("Error opening file");
                return;
            }

            // read from f and encrypt

            fclose(f);
        }
    }
    closedir(dir); 
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 1
    If the file sizes are greater than the RSA key size then using RSA is not a god solution. RSA is not designed to encrypt large data, in that case the data is usually encrypted with a symmetric algorithm and then the symmetric is encrypted with RSA. – zaph Feb 12 '16 at 13:26
  • I have splitted up a large file into multiple files which are smaller than the RSA key size. Now how to encrypt it.? – Arjun Feb 12 '16 at 13:31
  • @Arjun Just like you do as in your question. Read the contents of the file, encrypt it, and write the results to another file. – dbush Feb 12 '16 at 13:37
  • @dbush I have read a file using opendir(). Now i have an error in encyption. if((encrypt_len = RSA_public_encrypt(strlen(msg)+1,f , (unsigned char*)encrypt, keypair, RSA_PKCS1_OAEP_PADDING)) == -1) – Arjun Feb 12 '16 at 14:27
  • You'd better use a cryptosystem designed for file encryption. PGP could be an option, for instance, if used right. If you're going to design your own you may very well run into problems. Splitting up a file for direct RSA encryption is [certainly not a good idea](http://crypto.stackexchange.com/a/32693/1172). – Maarten Bodewes Feb 12 '16 at 21:46