使用OpenSSL库在C中进行AES CTR加密

使用OpenSSL库在C中进行AES CTR加密,c,encryption,openssl,cryptography,aes,C,Encryption,Openssl,Cryptography,Aes,我正在尝试使用aes ctr和256位密钥加密和解密一个大文件(100MB)。我从这个网站下载了这个代码。我正在使用Xcode运行它,但有一个问题,它给我“读取文件为空”作为输出。如果有人能帮我或给我一个提示,我将不胜感激 注意:我添加了文本文件,如下图所示: struct ctr_state{ unsigned char ivec[AES_BLOCK_SIZE]; unsigned int num; unsigned char ecount[AES_BLOCK_SIZE];}; FILE

我正在尝试使用aes ctr和256位密钥加密和解密一个大文件(100MB)。我从这个网站下载了这个代码。我正在使用Xcode运行它,但有一个问题,它给我“读取文件为空”作为输出。如果有人能帮我或给我一个提示,我将不胜感激

注意:我添加了文本文件,如下图所示:

struct ctr_state{

unsigned char ivec[AES_BLOCK_SIZE];
unsigned int num;
unsigned char ecount[AES_BLOCK_SIZE];};

FILE *readFile;
FILE *writeFile;
AES_KEY key;

int bytes_read, bytes_written;
unsigned char indata[AES_BLOCK_SIZE];
unsigned char outdata[AES_BLOCK_SIZE];
unsigned char iv[AES_BLOCK_SIZE];
struct ctr_state state;

int init_ctr(struct ctr_state *state, const unsigned char iv[16])
{
   state->num = 0;
   memset(state->ecount, 0, AES_BLOCK_SIZE);

  /* Initialise counter in 'ivec' to 0 */
   memset(state->ivec + 8, 0, 8);

  /* Copy IV into 'ivec' */
   memcpy(state->ivec, iv, 8);

   return 0;
}

void fencrypt(char* read, char* write, const unsigned char* enc_key)
{
    if(!RAND_bytes(iv, AES_BLOCK_SIZE))
   {
       fprintf(stderr, "Could not create random bytes.");
       exit(1);
   }

   readFile = fopen(read,"rb"); // The b is required in windows.
   writeFile = fopen(write,"wb");

   if(readFile==NULL)
   {
      fprintf(stderr, "Read file is null.");
      exit(1);
   }

   if(writeFile==NULL)
   {
      fprintf(stderr, "Write file is null.");
      exit(1);
   }

  fwrite(iv, 1, 8, writeFile); // IV bytes 1 - 8
  fwrite("\0\0\0\0\0\0\0\0", 1, 8, writeFile); // Fill the last 4 with null bytes 9 - 16

//Initializing the encryption KEY
if (AES_set_encrypt_key(enc_key, 128, &key) < 0)
{
    fprintf(stderr, "Could not set encryption key.");
    exit(1);
}

init_ctr(&state, iv); //Counter call
//Encrypting Blocks of 16 bytes and writing the output.txt with ciphertext
while(1)
{
    bytes_read = fread(indata, 1, AES_BLOCK_SIZE, readFile);
    AES_ctr128_encrypt(indata, outdata, bytes_read, &key, state.ivec, state.ecount, &state.num);

    bytes_written = fwrite(outdata, 1, bytes_read, writeFile);
    if (bytes_read < AES_BLOCK_SIZE)
    {
        break;
    }
}

fclose(writeFile);
fclose(readFile);
 }

  void fdecrypt(char* read, char* write, const unsigned char* enc_key)
 {

  readFile=fopen(read,"rb"); // The b is required in windows.
  writeFile=fopen(write,"wb");

  if(readFile==NULL)
  {
      fprintf(stderr,"Read file is null.");
      exit(1);
  }

  if(writeFile==NULL)
  {
     fprintf(stderr, "Write file is null.");
     exit(1);
  }

  fread(iv, 1, AES_BLOCK_SIZE, readFile);

  //Initializing the encryption KEY
  if (AES_set_encrypt_key(enc_key, 128, &key) < 0)
  {
      fprintf(stderr, "Could not set decryption key.");
      exit(1);
  }

  init_ctr(&state, iv);//Counter call
  //Encrypting Blocks of 16 bytes and writing the output.txt with ciphertext
  while(1)
  {
      bytes_read = fread(indata, 1, AES_BLOCK_SIZE, readFile);
      //printf("%i\n", state.num);
      AES_ctr128_encrypt(indata, outdata, bytes_read, &key, state.ivec, state.ecount, &state.num);

      bytes_written = fwrite(outdata, 1, bytes_read, writeFile);
      if (bytes_read < AES_BLOCK_SIZE)
      {
          break;
      }
  }
  fclose(writeFile);
  fclose(readFile); 
 }

 int main(int argc, char *argv[])
{
     fencrypt("encme.txt", "enced.enc", (unsigned const char*)"1234567812345678");
     fdecrypt("enced.enc", "unenced.txt", (unsigned const char*)"1234567812345678");
    getc(stdin);
    return 0;
}

结构中心状态{ 无符号字符ivec[AES_块大小]; 无符号整数; 无符号字符计数[AES_BLOCK_SIZE];}; 文件*readFile; 文件*可写文件; AES_密钥; 读取整数字节,写入字节; 无符号字符indata[AES_BLOCK_SIZE]; 无符号字符输出数据[AES_块大小]; 无符号字符iv[AES_块大小]; 结构中心状态; int init_ctr(struct ctr_state*state,const unsigned char iv[16]) { 状态->数值=0; memset(state->ecount,0,AES\u块大小); /*将“ivec”中的计数器初始化为0*/ memset(状态->ivec+8,0,8); /*将IV复制到“ivec”*/ memcpy(州->ivec,iv,8); 返回0; } void-fencrypt(字符*读,字符*写,常量无符号字符*加密键) { if(!RAND_字节(iv,AES_块大小)) { fprintf(stderr,“无法创建随机字节”); 出口(1); } readFile=fopen(读“rb”);//windows中需要b。 writeFile=fopen(写“wb”); if(readFile==NULL) { fprintf(stderr,“读取文件为空”); 出口(1); } if(writeFile==NULL) { fprintf(stderr,“写入文件为空”); 出口(1); } fwrite(iv,1,8,writeFile);//iv字节1-8 fwrite(“\0\0\0\0\0\0\0”,1,8,writeFile);//用空字节9-16填充最后4个 //初始化加密密钥 如果(AES设置加密密钥(加密密钥,128和密钥)<0) { fprintf(stderr,“无法设置加密密钥”); 出口(1); } init_ctr(&state,iv);//计数器调用 //加密16字节的块并使用密文写入output.txt 而(1) { 字节\读取=fread(indata,1,AES \块\大小,读取文件); AES加密(indata、outdata、字节读取和密钥、state.ivec、state.ecoount和state.num); bytes_write=fwrite(outdata,1,bytes_read,writeFile); if(字节读取您是否确保文件存在于您所在的工作目录中?是的,我编辑了文章以包含pic,它显示了我添加文本文件的位置。使用CTR模式,必须永远不要重复使用相同的密钥/计数器对,这是一个常见的故障。如果目标是Mac或iOS设备,请使用通用加密,它是
Security.framework
的一部分,支持加密硬件,比纯代码实现快100到1000倍。。此外,还可以读取、加密和写入一次包含一个块的较大数据块。考虑使用<代码> NStPixStuts和 NSuttPoStuts。我认为运行程序的工作目录与EnME.txt文件所在的目录不一样。标识工作目录或使用完整路径。