#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    30
    Rep Power
    5

    AES Encrypt/Decrypt in C


    Hi how can i encrypt a file in aes with 256 key in c?
    Thank you very much!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    30
    Rep Power
    5
    Originally Posted by iassael
    Hi how can i encrypt a file in aes with 256 key in c?
    Thank you very much!
    or c++
    any suggestions?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    64
    Rep Power
    94
    Alright...
    First, get a decent AES- implementation.
    There are many freely available; I picked this one at random; it's BSD- licensed (meaning you can use it for whatever you want, just mention Brian Gladman in a copyright notice).

    For building the library (I didn't bother with the slightly faster ASM- version), I just used:
    Code:
    gcc -c -O2 -fomit-frame-pointer aescrypt.c aeskey.c aestab.c aes_modes.c
    ar rcs libaes.a *.o
    If you use Visual Studio, get the corresponding project files...

    Here is an example of how your program could look like:
    c Code:
    #include <stdlib.h>
    #include <time.h>
    #include <stdio.h>
    #include "aes.h"
     
     
    void encrypt(const char *fileIn, const char *fileOut,
        const unsigned char *key);
    void decrypt(const char *fileIn, const char *fileOut,
        const unsigned char *key);
     
    int main() {
      const unsigned char key[] = "my key";
     
      srand(time(NULL));
     
      aes_init();
      encrypt("main.c", "main.c.encrypted", key);
      decrypt("main.c.encrypted", "main.c.decrypted", key);
     
      return 0;
    }
     
    void encrypt(const char *fileIn, const char *fileOut, 
        const unsigned char *key) {
      int i;
      aes_encrypt_ctx ctx[1];
      unsigned char iv[16]; /* initialisation vector */
      unsigned char inBuffer[200], outBuffer[200];
      FILE *inFile = fopen(fileIn, "rb");
      FILE *outFile = fopen(fileOut, "wb");
     
      /* pick a random initialisation vector */
      for(i = 0; i < 16; ++i)
        iv[i] = rand() & 0xFF;
      fwrite(iv, 1, 16, outFile);
     
      aes_encrypt_key256(key, ctx);
      while((i = fread(inBuffer, 1, sizeof(inBuffer), inFile)) > 0) {
        aes_ofb_crypt(inBuffer, outBuffer, i, iv, ctx);
        fwrite(outBuffer, 1, i, outFile);
      }
     
      fclose(inFile);
      fclose(outFile);
    }
     
    void decrypt(const char *fileIn, const char *fileOut, 
        const unsigned char *key) {
      int i;
      aes_encrypt_ctx ctx[1];
      unsigned char iv[16]; /* initialisation vector */
      unsigned char inBuffer[200], outBuffer[200];
      FILE *inFile = fopen(fileIn, "rb");
      FILE *outFile = fopen(fileOut, "wb");
     
      /* read initialization vector from file */
      if(fread(iv, 1, 16, inFile) < 16) {
        fclose(inFile);
        fclose(outFile);
        return; /* error: file doesn't even contain an initialisation vector */
      }
     
      aes_encrypt_key256(key, ctx);
      while((i = fread(inBuffer, 1, sizeof(inBuffer), inFile)) > 0) {
        aes_ofb_crypt(inBuffer, outBuffer, i, iv, ctx);
        fwrite(outBuffer, 1, i, outFile);
      }
     
      fclose(inFile);
      fclose(outFile);
    }


    You have to link with the library you previously created, and "aes.h", "brg_types.h" must be in your includepath.

    Note that I'm pretty clueless about cryptography- storing the initialisation vector as plaintext inside the encrypted file may or may not be "secure", but I guess it's better than using always the same one, and seems to be common practise...

    --edit: fixed typos
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    30
    Rep Power
    5
    Originally Posted by h4rdc0ded
    Alright...
    First, get a decent AES- implementation.
    There are many freely available; I picked this one at random; it's BSD- licensed (meaning you can use it for whatever you want, just mention Brian Gladman in a copyright notice).

    For building the library (I didn't bother with the slightly faster ASM- version), I just used:
    Code:
    gcc -c -O2 -fomit-frame-pointer aescrypt.c aeskey.c aestab.c aes_modes.c
    ar rcs libaes.a *.o
    If you use Visual Studio, get the corresponding project files...

    Here is an example of how your program could look like:
    c Code:
    #include <stdlib.h>
    #include <time.h>
    #include <stdio.h>
    #include "aes.h"
     
     
    void encrypt(const char *fileIn, const char *fileOut,
        const unsigned char *key);
    void decrypt(const char *fileIn, const char *fileOut,
        const unsigned char *key);
     
    int main() {
      const unsigned char key[] = "my key";
     
      srand(time(NULL));
     
      aes_init();
      encrypt("main.c", "main.c.encrypted", key);
      decrypt("main.c.encrypted", "main.c.decrypted", key);
     
      return 0;
    }
     
    void encrypt(const char *fileIn, const char *fileOut, 
        const unsigned char *key) {
      int i;
      aes_encrypt_ctx ctx[1];
      unsigned char iv[16]; /* initialisation vector */
      unsigned char inBuffer[200], outBuffer[200];
      FILE *inFile = fopen(fileIn, "rb");
      FILE *outFile = fopen(fileOut, "wb");
     
      /* pick a random initialisation vector */
      for(i = 0; i < 16; ++i)
        iv[i] = rand() & 0xFF;
      fwrite(iv, 1, 16, outFile);
     
      aes_encrypt_key256(key, ctx);
      while((i = fread(inBuffer, 1, sizeof(inBuffer), inFile)) > 0) {
        aes_ofb_crypt(inBuffer, outBuffer, i, iv, ctx);
        fwrite(outBuffer, 1, i, outFile);
      }
      aes_ofb_crypt(inBuffer, outBuffer, i, iv, ctx);
      fwrite(outBuffer, 1, i, outFile);
     
      fclose(inFile);
      fclose(outFile);
    }
     
    void decrypt(const char *fileIn, const char *fileOut, 
        const unsigned char *key) {
      int i;
      aes_encrypt_ctx ctx[1];
      unsigned char iv[16]; /* initialisation vector */
      unsigned char inBuffer[200], outBuffer[200];
      FILE *inFile = fopen(fileIn, "rb");
      FILE *outFile = fopen(fileOut, "wb");
     
      /* read initialization vector from file */
      if(fread(iv, 1, 16, inFile) < 16)
        return; /* error: file doesn't even contain an initialisation vector */
     
      aes_encrypt_key256(key, ctx);
      while((i = fread(inBuffer, 1, sizeof(inBuffer), inFile)) > 0) {
        aes_ofb_crypt(inBuffer, outBuffer, i, iv, ctx);
        fwrite(outBuffer, 1, i, outFile);
      }
     
      fclose(inFile);
      fclose(outFile);
    }


    You have to link with the library you previously created, and "aes.h", "brg_types.h" must be in your includepath.

    Note that I'm pretty clueless about cryptography- storing the initialisation vector as plaintext inside the encrypted file may or may not be "secure", but I guess it's better than using always the same one, and seems to be common practise...

    --edit: fixed typos
    thanks man!!! Its perfect i will check it in a couple of hours :)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    30
    Rep Power
    5
    Originally Posted by iassael
    thanks man!!! Its perfect i will check it in a couple of hours :)
    although i included the source and was found i get a lot of "unidentified reference to aes_init()" like errors ...
    Any suggestions?
    Thanks!
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    Set your project path settings to point to the AES lib
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    30
    Rep Power
    5
    Originally Posted by Scorpions4ever
    Set your project path settings to point to the AES lib
    of course i did it.... :)
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    30
    Rep Power
    5
    u r right it works perfect many thanks! :)
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Unhappy need your help doesn't work


    hi :hi:
    I try to execute the code but it doesn't work :( please if you don't mind telling me the steps in details because I don't have a good experience in c and I will be thankful :D
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    Originally Posted by 7anoun
    hi :hi:
    I try to execute the code but it doesn't work :( please if you don't mind telling me the steps in details because I don't have a good experience in c and I will be thankful :D
    Do you have a text file named main.c in the same folder as your successfully compiled binary?

    Have you built the library as follows?

    gcc -c -O2 -fomit-frame-pointer aescrypt.c aeskey.c aestab.c aes_modes.c
    ar rcs libaes.a *.o
    Have you compiled your test app as follows (assuming the source is named test.cpp?

    gcc test.cpp libaes.a

IMN logo majestic logo threadwatch logo seochat tools logo