(C语言)基于AES-CTR的加密解密工具乱码解密

(C language) Garbled decryption in AES-CTR based encryption decryption tool

解密和加密代码(包含在下面)可能有误。他们编译没有错误,但是,解密文件与明文文件不同。是乱码。

我正在寻找乱码的原因。我怀疑我的代码没问题。

我做了很多可能会解决问题的事情,但 none 奏效了。我有一种使用 AES-CTR 的特定方法,即我没有直接包含 OpenSSL。我依赖于 /dev/urandom /dev/random 的随机性(IV 等)。

那些东西包括我处理我的加密和解密函数的方式(比如:使用 *(ptr) 而不是 ptr[] 和其他几个等效的替换,这通常是不必要的),什么在最终保存加密或解密的文本之前,我正在传递并检查某些验证的参数。

我没有增加(或改变)初始化的 IV。 IV 是 16 个字节。

早些时候,我这样做是为了增加 IV 的最后一个字节:

size_t blocksize = CCA_STRENGTH; /* CCA_STRENGTH is "#define CCA_STRENGTH 16" */
char *iv = (char *) malloc(16 * sizeof(char));
ri(); /* PRNG initialized.*/
prng_getbytes(iv, blocksize);
*   
*
*
*
size_t iv_counter = 0;
while loop starts here...
*
*
*(iv +(block_size - 1)) += iv_counter;
*
*
while loop ends here...

下面是发生加密和解密的 while 循环(这部分暂时删除,因为它是安全要求,而不是编码标准或加密或解密功能的要求)。

这是我的解密代码:

#include "pv.h"

void decrypt_file (const char *ptxt_fname, void *raw_sk, size_t raw_len, int fin)
{
    int fd;
   if((fd = open(ptxt_fname, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0)
       {
           /*fd will become -1 on failure.*/
           perror("plaintext file permission error\n");
           exit(EXIT_FAILURE);
           }

    size_t block_size = CCA_STRENGTH;

  const char *aes_key;
  aes_key = (const char *) raw_sk;
  aes_ctx aes;

  aes_setkey(&aes, aes_key, block_size);
  char *iv;  
  iv = (char *) malloc(block_size * sizeof(char));
  size_t bytes_read;
  bytes_read = read(fin, iv, block_size);

  char *buf = (char *) malloc(block_size * sizeof(char)); /*ctxt-file read buffer*/
  char *ptxt = (char *) malloc(block_size * sizeof(char)); /*p-text buffer*/
  memset(ptxt, 0, block_size);

    bytes_read = read(fin, buf, block_size);

        while(bytes_read >= 1)
    {

        aes_encrypt(&aes, ptxt, iv); /* earlier it was "aes_decrypt(&aes, ptxt, iv);" which was not the correct reverse transformation function */
        for(loop_variable = 0; loop_variable < bytes_read; loop_variable++)
        {
            *(ptxt + loop_variable) = *(ptxt + loop_variable) ^ *(buf + loop_variable);
        }

        if((result = write_chunk(fd, ptxt, bytes_read)) == -1)
        {
            perror("Problem when writing to ptxt file... \n");
            close(fd);
            unlink(ptxt_fname); /*for file deletion*/
            aes_clrkey(&aes);
            free(ptxt);
            free(iv);
            free(buf);
            exit(EXIT_FAILURE);
        }
        if((bytes_read = read(fin, buf, block_size)) < 1)
        {
            close(fd);
            aes_clrkey(&aes);
            free(ptxt);
            free(iv);
            free(buf);
            break;
        }
    }

}

void 
usage (const char *pname)
{
  printf ("Simple File Decryption Utility\n");
  printf ("Usage: %s SK-FILE CTEXT-FILE PTEXT-FILE\n", pname);
  printf ("       Exits if either SK-FILE or CTEXT-FILE don't exist, or\n");
  printf ("       if a symmetric key sk cannot be found in SK-FILE.\n");
  printf ("       Otherwise, tries to use sk to decrypt the content of\n");
  printf ("       CTEXT-FILE: upon success, places the resulting plaintext\n");
  printf ("       in PTEXT-FILE; if a decryption problem is encountered\n"); 
  printf ("       after the processing started, PTEXT-FILE is truncated\n");
  printf ("       to zero-length and its previous content is lost.\n");

  exit (1);
}

int main (int argc, char **argv)
{
  int fdsk, fdctxt;
  char *sk = NULL;
  size_t sk_len = 0;

  if (argc != 4) {
    usage (argv[0]);
  }   
  else if (((fdsk = open (argv[1], O_RDONLY)) == -1)
       || ((fdctxt = open (argv[2], O_RDONLY)) == -1)) {
    if (errno == ENOENT) {
      usage (argv[0]);
    }
    else {
      perror (argv[0]);

      exit (-1);
    }
  }   
  else {
    setprogname (argv[0]);

    if (!(sk = import_sk_from_file (&sk, &sk_len, fdsk))) {
      printf ("%s: no symmetric key found in %s\n", argv[0], argv[1]);

      close (fdsk);
      exit (2);
    }
    close (fdsk);

    decrypt_file (argv[3], sk, sk_len, fdctxt);    
    bzero(sk, sk_len);
    free(sk);


    close (fdctxt);
  }

  return 0;
}

这是我的加密代码:

#include "pv.h"

void encrypt_file (const char *ctxt_fname, void *raw_sk, size_t raw_len, int fin)
{

    size_t block_size = CCA_STRENGTH;
    int fd; /* ctxt fd */
    if((fd = open(ctxt_fname,O_WRONLY|O_TRUNC|O_CREAT,0600)) < 0)
    {
        perror("Ciphertext file permission error\n");
        exit(EXIT_FAILURE);
    }

    char *iv;
    iv = (char *) malloc(block_size * sizeof(char));

    ri(); /*IV initialized*/
    prng_getbytes(iv, block_size);

    struct aes_ctx aes;
    const char *aes_key = aes_key = (const char *) raw_sk;

    aes_setkey(&aes, aes_key, block_size); /*sets the encryption key.*/

    char *buf = buf = (char *) malloc(block_size * sizeof(char)); /*file read buffer*/
    char *ctxt = ctxt = (char *) malloc(block_size * sizeof(char)); /*ciphertext buffer*/
    int result;
    size_t looper = 0;
    size_t bytes_read;
    result = write_chunk(fd, iv, block_size); 

    if(result == -1)
    {
        exit(-1);
    }

    bytes_read = read(fin, buf, block_size); /*returns how many bytes read*/
    while(bytes_read >= 1)
    {
        aes_encrypt(&aes, ctxt, iv);
        for(looper = 0; looper < bytes_read; looper++)
        {

            *(ctxt + looper) = *(ctxt + looper) ^ *(buf + looper);
        }

        result = write_chunk(fd, ctxt, bytes_read);
        if(result == -1)
        {
            perror("Problem when writing to ctxt file... \n");
            close(fd);
            unlink(ctxt_fname); /*for file deletion*/
            aes_clrkey(&aes);
            free(ctxt);
            free(iv);
            free(buf);
            exit(EXIT_FAILURE);
        }       
        printf("crossed written to file\n");

        if((bytes_read = read(fin, buf, block_size)) < 1)
        {
            close(fd);
            aes_clrkey(&aes);
            free(ctxt);
            free(iv);
            free(buf);
            break;
        }

    }
}

void usage (const char *pname)
{
    printf ("Personal Vault: Encryption \n");
    printf ("Usage: %s SK-FILE PTEXT-FILE CTEXT-FILE\n", pname);
    printf ("       Exits if either SK-FILE or PTEXT-FILE don't exist.\n");
    printf ("       Otherwise, encrpyts the content of PTEXT-FILE under\n");
    printf ("       sk, and place the resulting ciphertext in CTEXT-FILE.\n");
    printf ("       If CTEXT-FILE existed, any previous content is lost.\n");

    exit (1);
}

int main (int argc, char **argv)
{
    int fdsk, fdptxt;
    char *raw_sk;
    size_t raw_len;



    if (argc != 4)
    {
        usage (argv[0]);
    }
    else if (((fdsk = open (argv[1], O_RDONLY)) == -1) || ((fdptxt = open (argv[2], O_RDONLY)) == -1))   
    {
        if (errno == ENOENT)
        {
            usage (argv[0]);
        }
        else
        {
            perror (argv[0]);

            exit (-1);
        }
    }
    else
    {
        setprogname (argv[0]);

        if (!(import_sk_from_file (&raw_sk, &raw_len, fdsk)))   
        {
            printf ("%s: no symmetric key found in %s\n", argv[0], argv[1]);

            close (fdsk);
            exit (2);
        }
        close (fdsk);


        bzero(raw_sk, raw_len);
        free(raw_sk);


        close (fdptxt);
    }

    return 0;
}

加密代码首先将 16 个字节的 IV 添加到密文文件中(我猜应该是这样;我们在解密过程中从 ctxt 文件中读取了这 16 个字节),然后进行实际加密(考虑到它作为一个黑盒子)我们发送 IV 和密钥;返回 16 个字节。返回的那 16 个字节必须与明文文件缓冲区进行异或(最多 16 个字节,如果文件超过 16 个字节,则每一轮)。

Post 将异或字节(最多 16 个字节)写入文件。这件事一直发生到最后一轮,最终打破了试图读取没有更多内容可读文件的循环。 read 函数在每次调用时尝试读取下一个可用字节(最多指定的字节数)。

这不是一个完美的实现,因为我放宽了安全方面(为每个周期随机化 IV),但是,它必须首先像这样工作。

从数学上讲,我正在尝试做的应该是这个概念的精确复制:

ciphertext_block = message_block ^ [AES(IV, Key)]

message_block = ciphertext_block ^ [AES(IV, Key)]

这里,ciphertext/message_block指的是一个字符块,第1轮到第n-1轮为16字节,但最后一轮可以或不能为16字节。

在任何情况下,XORing 将在 16 字节字符(AES-CTR 的输出)和另一个块(用于加密的消息块,用于解密的密文块,无论如何,它可以达到 16 字节,因此它们是异或约束,即它们将决定异或输出的长度。

因为它们是决策者,所以一旦 XORing 操作覆盖了它们的长度,XORing 循环就会停止,我们继续将其写入文件(加密的 ctxt 文件,解密的 ptxt 文件)。

应该没有任何填充要求。

我不确定是否应该在任何地方使用 realloc 函数。

如果任何 reader 在理解我正在尝试做的事情时遇到问题,我很乐意提供额外的文档。

该程序有几个库依赖项,但是,代码编译没有错误。

这是我的编译命令:

gcc -g -O2 -ansi -Wall -Wsign-compare -Wchar-subscripts -Werror -I. -I/usr/include/ -I/home/devel/libdcrypt/include/ -c pv_keygen.c pv_misc.c
gcc -g -O2 -ansi -Wall -Wsign-compare -Wchar-subscripts -Werror -o pv_keygen pv_keygen.o pv_misc.o -L. -L/usr/lib/ -L/home/devel/libdcrypt/lib/ -ldcrypt  -lgmp

这只是为了编译我的加密文件。几乎相同(并且同样严格)的命令用于我的注册机和解密文件。注册机代码似乎工作正常(我还没有把它包括在这里);我能够生成一个密钥并将其序列化到一个文件中。该密钥文件实际上包含两个密钥,我只阅读 AES-CTR 的前半部分。下半部分将用于 MACing 目的(使用 AES-CBC)。

文件大小详细信息

纯文本文件:x 字节

密文文件:(x+16)字节

解密后的文本文件:x字节

数据是对的,内容是错的。解密后的文本文件和明文文件必须相同。

我正在尝试:diff plaintext_file decrypted_file 在 RedHat 上进行文件比较。

密钥文件实际上是32个字节,其中前16个字节用于加密,后16个字节将用于MACpost加密完成

密钥文件(序列化为 base-64)(十六进制):

0000000 4662 6e4b 6631 7268 4876 676c 772f 664e
0000010 4d5a 6f32 384e 5141 7139 6635 3442 7245
0000020 646c 4a77 5553 4c30 4f63 3d6f 000a     
000002d

输入纯文本文件(十六进制):

0000000 6161 6161 6161 6161 6161 6161 610a 6161
0000010 000a                                   
0000011

加密文件(十六进制):

0000000 540e 0e30 d74d 5000 78c1 13e3 0476 d4a2
0000010 61c9 76ac e717 cd6d 013e e872 8e16 4827
0000020 00a2                                   
0000021

解密文件(十六进制):

0000000 8bea 616a 1f1b d6b0 fd13 da46 5824 ec00
0000010 0081                                   
0000011

外部参考(我已将可能存在错误的内容删减):

1.) http://www.scs.stanford.edu/nyu/05sp/lab/lab1.html

2) http://www.scs.stanford.edu/nyu/05sp/lab/install-libs.html

为什么不直接在 CTR 模式下使用 AES,不需要对每 16 个字节进行 AES 调用,然后将输出与数据进行异或运算。

CTR 模式接受纯文本、密钥和 IV,并生成 AES CTR 模式加密数据。 IOW CTR 模式执行所有异或运算和计数器递增。

注意点击率模式,同一对 key/IV 只能使用一次。

大多数支持 AES 和其他块密码的库都具有低级别的块加密,并使用它来实现更高级的模式,例如 CBC、CFB、CTR、OFB 和选项,例如 PKCS#7 née PKCS#5 填充。

修复方法如下:

问题存在于我想做的方式和我正在做的方式。由于没有实现相关的问题,问题存在于我的解密代码中。

在我的解密代码中,没有必要包含 aes_decrypt 函数,因为我实际要查找的是 反向转换(关于解密的正确技术术语w.r.t。AES)相反,相同的密钥被用于解密相同的文件。

作为修正,aes_decrypt 应该只是 aes_encrypt,它会成功的。

现在,我可以继续进行额外的添加(安全加强和数据完整性注意事项)。