c - OpenSSL 上的 AES CTR 256 加密操作模式

标签 c openssl aes

我是 OpenSSL 的新手,任何人都可以提示我如何从 C 文件初始化 AES CTR 模式。我知道这是该方法的签名,但我在参数方面遇到问题,没有太多文档,也没有清晰的示例如何进行简单加密。如果有人可以举例说明对此方法的调用,我将不胜感激。提前致谢!

void AES_ctr128_encrypt(const unsigned char *in, unsigned char *out,
    const unsigned long length, const AES_KEY *key,
    unsigned char ivec[AES_BLOCK_SIZE],
    unsigned char ecount_buf[AES_BLOCK_SIZE],
    unsigned int *num);

嗨,Caf,我非常感谢您的快速回答,它真的很有用,而且绝对是我在网络上找到的最好的例子。我试图打开一个长度不确定的文件,加密它并用生成的密文写入另一个文件,然后打开加密文件并恢复明文。我需要使用一个相当大的 MB 文件,因为我想对 CPU 的性能进行基准测试。但是我在解密时仍然遇到问题。不知何故,当解密相当大的 txt 文件 (1504KB) 时,它不会完全解密,我得到的一半是明文,另一半仍然是加密的。我认为这可能与 iv 的大小或我调用计数器的方式有关。这是我到目前为止所拥有的:

#include <openssl/aes.h>
#include <stdio.h>
#include <string.h>

struct ctr_state { 
    unsigned char ivec[16];   
    unsigned int num; 
    unsigned char ecount[16]; 
}; 

FILE *fp;
FILE *rp;
FILE *op;
size_t count;   
char * buffer; 
AES_KEY key; 

int bytes_read, bytes_written;   
unsigned char indata[AES_BLOCK_SIZE]; 
unsigned char outdata[AES_BLOCK_SIZE];  
unsigned char ckey[] =  "thiskeyisverybad"; // It is 128bits though..
unsigned char iv[8] = {0};//This should be generated by RAND_Bytes I will take into    consideration your previous post
struct ctr_state state;   

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

void encrypt(){ 
  //Opening files where text plain text is read and ciphertext stored      
  fp=fopen("input.txt","a+b");
  op=fopen("output.txt","w");
  if (fp==NULL) {fputs ("File error",stderr); exit (1);}   
  if (op==NULL) {fputs ("File error",stderr); exit (1);}      

  //Initializing the encryption KEY
  AES_set_encrypt_key(ckey, 128, &key); 

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

  fclose (fp); 
  fclose (op);
  free (buffer); 
}

void decrypt(){
  //Opening files where text cipher text is read and the plaintext recovered         
  rp=fopen("recovered.txt","w");
  op=fopen("output.txt","a+b");
  if (rp==NULL) {fputs ("File error",stderr); exit (1);}   
  if (op==NULL) {fputs ("File error",stderr); exit (1);} 

  //Initializing the encryption KEY
  AES_set_encrypt_key(ckey, 128, &key); 

  //Encrypting Blocks of 16 bytes and writing the output.txt with ciphertext   
  while (1) {     
    init_ctr(&state, iv);//Counter call
    bytes_read = fread(indata, 1, AES_BLOCK_SIZE, op);  
    AES_ctr128_encrypt(indata, outdata, bytes_read, &key, state.ivec, state.ecount, &state.num); 
    bytes_written = fwrite(outdata, 1, bytes_read, rp); 
    if (bytes_read < AES_BLOCK_SIZE) 
    break; 
    }   
  fclose (rp); 
  fclose (op);
  free (buffer); 
}

int main(int argc, char *argv[]){  
  encrypt();  
  //decrypt(); 
  system("PAUSE");  
  return 0;
}

每个加密和解密函数在不同的运行中被调用,所以所有的东西总是用相同的值初始化。再次感谢您提前向我提供的提示和问候!!!

最佳答案

通常,您会打算重复调用 AES_ctr128_encrypt() 以发送多条具有相同 key 和 IV 以及递增计数器的消息。这意味着您需要在调用之间跟踪“ivec”、“num”和“ecount”值 - 因此创建一个 struct 来保存这些值,以及一个初始化函数:

struct ctr_state {
    unsigned char ivec[16];  /* ivec[0..7] is the IV, ivec[8..15] is the big-endian counter */
    unsigned int num;
    unsigned char ecount[16];
};

int init_ctr(struct ctr_state *state, const unsigned char iv[8])
{
    /* aes_ctr128_encrypt requires 'num' and 'ecount' set to zero on the
     * first call. */
    state->num = 0;
    memset(state->ecount, 0, 16);

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

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

现在,当您开始与目的地通信时,您需要生成一个 IV 来使用和初始化计数器:

unsigned char iv[8];
struct ctr_state state;

if (!RAND_bytes(iv, 8))
    /* Handle the error */;

init_ctr(&state, iv);

然后您需要将 8 字节的 IV 发送到目的地。您还需要从原始 key 字节初始化一个 AES_KEY:

AES_KEY aes_key;

if (AES_set_encrypt_key(key, 128, &aes_key))
    /* Handle the error */;

您现在可以开始加密数据并将其发送到目的地,重复调用 AES_ctr128_encrypt(),如下所示:

AES_ctr128_encrypt(msg_in, msg_out, msg_len, &aes_key, state->ivec, state->ecount, &state->num);

(msg_in 是指向包含明文消息的缓冲区的指针,msg_out 是指向加密消息应该去的缓冲区的指针,msg_len 是消息长度)。

解密完全相同,只是您不使用 RAND_bytes() 生成 IV - 相反,您采用对方给您的值。

重要:

  1. 在加密过程中不要多次调用init_ctr()。在开始加密之前,计数器和 IV 必须初始化。

  2. 在任何情况下都不要试图从加密端的 RAND_bytes() 以外的任何地方获取 IV。不要将其设置为固定值;不要使用哈希函数;不要使用收件人的姓名;不要从磁盘读取它。使用 RAND_bytes() 生成它并将其发送到目的地。每当您从零计数器开始时,您必须从您以前从未使用过的全新 IV 开始。

  3. 如果您完全有可能在不更改 IV 和/或 key 的情况下发送 2**64 字节,则您需要测试计数器是否溢出。

  4. 不要忽略错误检查。如果某个功能失败而您忽略了它,您的系统很可能(甚至可能)看起来运行正常,但实际上运行完全不安全。

关于c - OpenSSL 上的 AES CTR 256 加密操作模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3141860/

相关文章:

c - 如何释放指向 int 的指针,使其成为指向函数结果的指针?

c - pthread 互斥体无法正常工作

java - 如何在 Java 中加密/解密文件中的文本

java - javascript中的AES加密和java中的解密

C、Socket编程 : 1 Server 2 Clients connected by HUB, 使用TCP的聊天应用

C 程序生成数字模式

ssl - nginx SSL 无起始行 : expecting: TRUSTED CERTIFICATE

c - 对 `SHA1' 的 undefined reference

c - SSL BIO 和 FFI

Java - SHA-256 哈希 : Invalid AES key length : 64 bytes