C 如何存储具有精确大小计算的AES_加密大小密码?

C 如何存储具有精确大小计算的AES_加密大小密码?,c,encryption,cryptography,aes,caesar-cipher,C,Encryption,Cryptography,Aes,Caesar Cipher,我面临AES加密文本的问题,需要一些帮助,这里有一个解释,我正在使用此函数将纯文本转换为密码文本 /* * Encrypt *len bytes of data * All data going in & out is considered binary (unsigned char[]) */ unsigned char *aes_encrypt(EVP_CIPHER_CTX *e, unsigned char *plaintext, int *len) { /* max c

我面临AES加密文本的问题,需要一些帮助,这里有一个解释,我正在使用此函数将纯文本转换为密码文本

/*
 * Encrypt *len bytes of data
 * All data going in & out is considered binary (unsigned char[])
 */
unsigned char *aes_encrypt(EVP_CIPHER_CTX *e, unsigned char *plaintext, int *len)
{
  /* max ciphertext len for a n bytes of plaintext is n + AES_BLOCK_SIZE -1 bytes */
  int c_len = *len + AES_BLOCK_SIZE, f_len = 0;
  unsigned char *ciphertext = palloc(c_len);

  /* allows reusing of 'e' for multiple encryption cycles */
  EVP_EncryptInit_ex(e, NULL, NULL, NULL, NULL);

  /* update ciphertext, c_len is filled with the length of ciphertext generated,
    *len is the size of plaintext in bytes */
  EVP_EncryptUpdate(e, ciphertext, &c_len, plaintext, *len);

  /* update ciphertext with the final remaining bytes */
  EVP_EncryptFinal_ex(e, ciphertext+c_len, &f_len);

  *len = c_len + f_len;
  return ciphertext;
}
当我们使用加密时,这个函数非常有效,但是当我尝试使用strlen计算密文的长度时,我没有得到确切的长度, 我在谷歌上找到了一些关于密码文本打印的信息 使用%c的循环等于大小len的长度

我的问题是如何将密码文本存储在一个精确长度的文件中?由于strlen(密码文本)没有给出确切的长度,存储密码文本的方法是什么


任何帮助都将不胜感激:)提前感谢此界面的全部要点是
*len
现在包含密文的长度(我想它以纯文本的值开始)。EncryptFinal添加填充,以便更改长度。如果平原的长度为16 密文的长度为32

不能使用
strlen
计算长度。这可以在
argv
条目的情况下完成,因为它们是设计的字符串。这里的
键更像是密码。这是一个相当弱的加密系统(硬编码salt、5轮等)

ciphertext=aes\u加密(&en,(无符号字符*)输入[i],&len)之后
如果要将密文写入文件,只需执行以下操作

 FILE *cipher = NULL; 
 cipherfile=fopen("mycipher.txt", "wb"); 
 if (NULL ==cipherfile){ /* error handling here */
 }
 if (1 != fwrite(ciphertext, len, 1, cipherfile)){ /* error handling */
 }
 close(cipherfile);

因为密文的长度是
len
。最初的作者可能打算改用
olen
(现在不用了)。因此(更好)
&olen
可用于上述两个调用(
aes_encrypt
fwrite
),然后
len
保留输入长度,可用于解密检查,等等。

您使用的是什么密码模式?您的最后两句话包含在代码块中,因为您没有在它们之间添加空行。@luke我使用EVP_aes_256_cbc()和aes_block_SIZE 256 block SIZE
aes_block_SIZE
始终是16,从不256caesar cipher是错误的标签。。感谢您让我使用,但是,由于安全问题,我对将密码与文件一起存储有一个问题,所以我想将密码文本存储到数据库中,而无需存储和读取文件,您知道存储文本密码的更好方法吗?假设数据库使用字符串函数(如strcpy/strlen)存储密码,这就是我无法将密码文本存储到内存中的原因,你知道有没有其他方法可以实现它,这会很有帮助。提前感谢您的帮助,非常感谢。@Gaurav如果数据是加密的,将其存储在文件中有什么问题?如果需要将其存储为字符串,只需在解密之前对其进行hexify和unexify。或者使用base64,它效率更高,但有点复杂。