Ejemplo n.º 1
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);
}
Ejemplo n.º 2
0
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);
}
Ejemplo n.º 3
0
void decrypt(const char *path, const unsigned char *key) {
  int i;
  aes_encrypt_ctx ctx[1];
  unsigned char iv[16];
  unsigned char temp_name[50];
  unsigned char inBuffer[200], outBuffer[200];
  sprintf(temp_name, "%s.tmp", path);
  FILE *dfile = fopen(path, "rb");
  FILE *ofile = fopen(temp_name, "wb");
  if (dfile != NULL) {
    printf("File.\n");
    fflush(stdout);
  } else {
    return;
  }


  if (fread(iv, 1, 16, dfile) < 16) {
    printf("Decryption error.\n");
    fclose(dfile);
    fclose(ofile);
    return;
  }

  aes_encrypt_key256(key, ctx);
  while ((i = fread(inBuffer, 1, sizeof(inBuffer), dfile)) > 0) {
    aes_ofb_crypt(inBuffer, outBuffer, i, iv, ctx);
    fwrite(outBuffer, 1, i, ofile);
  }

  fclose(dfile);
  fclose(ofile);

  if (remove(path) != 0)
    printf("Error removing encrypted file.\n");
  if (rename(temp_name, path) != 0)
    printf("Error renaming file.\n");

}
Ejemplo n.º 4
0
void encrypt(const char *path, const unsigned char *key) {

  int i;
  aes_encrypt_ctx ctx[1];
  unsigned char iv[16];
  unsigned char temp_name[50];
  unsigned char inBuffer[200], outBuffer[200];
  FILE *dfile;
  FILE *ofile;
  sprintf(temp_name, "%s.tmp", path);
  dfile = fopen(path, "rb");
  ofile = fopen(temp_name, "wb");
  if (dfile != NULL) {
    printf("File.\n");
    fflush(stdout);
  } else {
    return;
  }


  for (i = 0; i < 16; ++i)
    iv[i] = rand() & 0xFF;
  fwrite(iv, 1, 16, ofile);

  aes_encrypt_key256(key, ctx);
  while ((i = fread(inBuffer, 1, sizeof(inBuffer), dfile)) > 0) {
    aes_ofb_crypt(inBuffer, outBuffer, i, iv, ctx);
    fwrite(outBuffer, 1, i, ofile);
  }

  fclose(dfile);
  fclose(ofile);
  if (remove(path) != 0)
    printf("Error removing infile.\n");
  if (rename(temp_name, path) != 0)
    printf("Error renaming file.\n");
}