OpenSSL AES 256 CBC via EVP api in C

前端 未结 2 733
梦谈多话
梦谈多话 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:55

    This code works, if anyone has some suggestions as to how it would be cleaner or more efficient please drop a comment.

    #include 
    #include 
    #include 
    #include 
    #include 
    
    void encrypt(FILE *ifp, FILE *ofp)
    {
        //Get file size
        fseek(ifp, 0L, SEEK_END);
        int fsize = ftell(ifp);
        //set back to normal
        fseek(ifp, 0L, SEEK_SET);
    
        int outLen1 = 0; int outLen2 = 0;
        unsigned char *indata = malloc(fsize);
        unsigned char *outdata = malloc(fsize*2);
        unsigned char ckey[] =  "thiskeyisverybad";
        unsigned char ivec[] = "dontusethisinput";
    
        //Read File
        fread(indata,sizeof(char),fsize, ifp);//Read Entire File
    
        //Set up encryption
        EVP_CIPHER_CTX ctx;
        EVP_EncryptInit(&ctx,EVP_aes_128_cbc(),ckey,ivec);
        EVP_EncryptUpdate(&ctx,outdata,&outLen1,indata,fsize);
        EVP_EncryptFinal(&ctx,outdata + outLen1,&outLen2);
        fwrite(outdata,sizeof(char),outLen1 + outLen2,ofp);
    }
    
    void decrypt(FILE *ifp, FILE *ofp)
    {
        //Get file size
        fseek(ifp, 0L, SEEK_END);
        int fsize = ftell(ifp);
        //set back to normal
        fseek(ifp, 0L, SEEK_SET);
    
        int outLen1 = 0; int outLen2 = 0;
        unsigned char *indata = malloc(fsize);
        unsigned char *outdata = malloc(fsize);
        unsigned char ckey[] =  "thiskeyisverybad";
        unsigned char ivec[] = "dontusethisinput";
    
        //Read File
        fread(indata,sizeof(char),fsize, ifp);//Read Entire File
    
        //setup decryption
        EVP_CIPHER_CTX ctx;
        EVP_DecryptInit(&ctx,EVP_aes_128_cbc(),ckey,ivec);
        EVP_DecryptUpdate(&ctx,outdata,&outLen1,indata,fsize);
        EVP_DecryptFinal(&ctx,outdata + outLen1,&outLen2);
        fwrite(outdata,sizeof(char),outLen1 + outLen2,ofp);
    }
    
    int main(int argc, char *argv[])
    {        
        if(argc != 2){
            printf("Usage:  /path/to/file/exe");
            return -1;
        }
        FILE *fIN, *fOUT;
        fIN = fopen("plain.txt", "rb");//File to be encrypted; plain text
        fOUT = fopen("cyphertext.txt", "wb");//File to be written; cipher text
    
        encrypt(fIN, fOUT);
        fclose(fIN);
        fclose(fOUT);
        //Decrypt file now
        fIN = fopen("cyphertext.txt", "rb");//File to be written; cipher text
        fOUT = fopen("decrypted.txt", "wb");//File to be written; cipher text
        decrypt(fIN,fOUT);
        fclose(fIN);
        fclose(fOUT);
    
        return 0;
    }
    

    Also According to this post the EVP api will handle an arbitrary sized input

    AES Encryption- large files

提交回复
热议问题