OpenSSL AES 256 CBC via EVP api in C

前端 未结 2 727
梦谈多话
梦谈多话 2020-12-05 21:03

What I am trying to do: Write a program in C that opens a file of arbitrary size and reads its contents. Once The contents are read it will encrypt them in

2条回答
  •  粉色の甜心
    2020-12-05 21:44

    Here is my version of your code. Naturally I like it better, but I offer it just as an alternative. Note the complete absence of error checking: real code would have it.

    #include 
    #include 
    #include 
    #include 
    #include 
    
    #ifndef TRUE
    #define TRUE 1
    #endif
    
    #ifndef FALSE
    #define FALSE 0
    #endif
    
    
    /**
     * Encrypt or decrypt, depending on flag 'should_encrypt'
     */
    void en_de_crypt(int should_encrypt, FILE *ifp, FILE *ofp, unsigned char *ckey, unsigned char *ivec) {
    
        const unsigned BUFSIZE=4096;
        unsigned char *read_buf = malloc(BUFSIZE);
        unsigned char *cipher_buf;
        unsigned blocksize;
        int out_len;
        EVP_CIPHER_CTX ctx;
    
        EVP_CipherInit(&ctx, EVP_aes_256_cbc(), ckey, ivec, should_encrypt);
        blocksize = EVP_CIPHER_CTX_block_size(&ctx);
        cipher_buf = malloc(BUFSIZE + blocksize);
    
        while (1) {
    
            // Read in data in blocks until EOF. Update the ciphering with each read.
    
            int numRead = fread(read_buf, sizeof(unsigned char), BUFSIZE, ifp);
            EVP_CipherUpdate(&ctx, cipher_buf, &out_len, read_buf, numRead);
            fwrite(cipher_buf, sizeof(unsigned char), out_len, ofp);
            if (numRead < BUFSIZE) { // EOF
                break;
            }
        }
    
        // Now cipher the final block and write it out.
    
        EVP_CipherFinal(&ctx, cipher_buf, &out_len);
        fwrite(cipher_buf, sizeof(unsigned char), out_len, ofp);
    
        // Free memory
    
        free(cipher_buf);
        free(read_buf);
    }
    
    int main(int argc, char *argv[]) {
    
        unsigned char ckey[] = "thiskeyisverybad";
        unsigned char ivec[] = "dontusethisinput";
        FILE *fIN, *fOUT;
    
        if (argc != 2) {
            printf("Usage:  /path/to/file/exe");
            return -1;
        }
    
        // First encrypt the file
    
        fIN = fopen("plain.txt", "rb"); //File to be encrypted; plain text
        fOUT = fopen("cyphertext.txt", "wb"); //File to be written; cipher text
    
        en_de_crypt(TRUE, fIN, fOUT, ckey, ivec);
    
        fclose(fIN);
        fclose(fOUT);
    
        //Decrypt file now
    
        fIN = fopen("cyphertext.txt", "rb"); //File to be read; cipher text
        fOUT = fopen("decrypted.txt", "wb"); //File to be written; cipher text
    
        en_de_crypt(FALSE, fIN, fOUT, ckey, ivec);
    
        fclose(fIN);
        fclose(fOUT);
    
        return 0;
    }
    

提交回复
热议问题