如何在AES中存储对称密钥

时间:2019-09-22 06:23:41

标签: c libtomcrypt

我正在使用libtomcrypt编写简单的AES解码器/编码器。我需要讲故事编码中使用的对称密钥,以便以后解码数据。问题是与“编码器”中使用的密钥相比,我从“存储”结构访问的密钥的值发生了变化。

我尝试将键值分配给全局symmetric_key数组元素的元素,但该值仍与原始值不同

#include <tomcrypt.h>
typedef struct {
  unsigned char * data;
  unsigned char * crc_value;
  symmetric_key skey;
}
aes_info_set;
aes_info_set aes_encrypter(unsigned char * data, unsigned char * key, int buf_size, int keysize, int rounds);
void main() {
  unsigned char * data = "hallo world";
  aes_info_set test;
  test = aes_encrypter(data, key, 80, 32, 14);
  printf("struct skey: %x \n", test.skey); 
}

aes_info_set aes_encrypter(unsigned char * data, unsigned char * key, int buf_size, int keysize, int rounds) {
  aes_info_set info_pack;
  unsigned char * text = data;
  unsigned char enc_out[buf_size];
  unsigned char * crc_value = (unsigned char * ) malloc(4 * sizeof(unsigned char));
  symmetric_key skey;
  crc_value = crc_check(text, strlen(text));
  aes_keysize( & keysize);
  aes_setup(key, keysize, rounds, & skey);
  aes_ecb_encrypt(text, enc_out, & skey);
  printf("FROM FUNC\n");
  info_pack.data = enc_out;
  info_pack.crc_value = crc_value;
  info_pack.skey = skey;
  printf("func skey: %x \n", skey);
  return info_pack;
}

//Output
>func skey: a15b56e0
>
>struct skey: a15b7890 

我希望它们是一样的

1 个答案:

答案 0 :(得分:0)

如果我正确理解,您会混淆需要加密/解密文件的密钥和库内部用于执行所需操作的“密钥”。

要存储的密钥是​​作为第一个参数传递给aes_setup()的密钥,而symmetric_key的实例并不重要。

请参见例如AES对rijndael_setup()的使用方式进行了aes.c:647测试(rijndael_setup()aes_setup()相同)。