Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C Libmcrypt无法成功加密/解密_C_Encryption_Mcrypt_Rijndael - Fatal编程技术网

C Libmcrypt无法成功加密/解密

C Libmcrypt无法成功加密/解密,c,encryption,mcrypt,rijndael,C,Encryption,Mcrypt,Rijndael,我正在使用c语言中的libmcrypt,并尝试使用rijndael-256作为选择的算法来实现一个简单的加密和解密测试。我已经将这个测试实现与rijndael的手册页示例非常接近,而不是他们选择的算法。当使用字符串gcc-o encryption_test main.c-lmcrypt编译时,以下源代码生成类似以下内容的输出: 加密消息缓冲区包含j��A.��8.�qj��%`��jh���=ZЁ�J 原来的字符串是��m“�C��D�����Y�G�v6��s��zh� 显然,解密部分失败了,

我正在使用c语言中的libmcrypt,并尝试使用rijndael-256作为选择的算法来实现一个简单的加密和解密测试。我已经将这个测试实现与rijndael的手册页示例非常接近,而不是他们选择的算法。当使用字符串gcc-o encryption_test main.c-lmcrypt编译时,以下源代码生成类似以下内容的输出: 加密消息缓冲区包含j��A.��8.�qj��%`��jh���=ZЁ�J 原来的字符串是��m“�C��D�����Y�G�v6��s��zh�

显然,解密部分失败了,但由于它只是一个函数调用,这让我相信加密方案的行为也不正确。如果你能给我指出正确的方向,我有几个问题要问libmcrypt专家

首先,是什么导致此代码产生此中断的输出

第二,当处理强制的固定大小时,如键大小和块大小,例如256位键,函数是否期望32字节的键+尾随的空字节,31字节的键+尾随的空字节,或者32字节的键与第33字节无关?同样的问题也适用于块大小

最后,我提到的一个例子使用mhash生成密钥文本的散列以提供给加密调用,这当然是更好的,但它被注释掉了,mhash中的链接似乎失败了。在使用libmcrypt时,处理这种类型的密钥转换的公认方法是什么?我选择将任何此类复杂性保留在ut是为了防止已经损坏的代码进一步复杂化,但我想将其纳入最终设计中。下面是有问题的源代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mcrypt.h>

int main(int argc, char *argv[])
{
MCRYPT mfd;
char *key;
char *plaintext;
char *IV;
unsigned char *message, *buffered_message, *ptr;
int i, blocks, key_size = 32, block_size = 32;

message = "Test Message";

/** Buffer message for encryption */    
blocks              = (int) (strlen(message) / block_size) + 1;
buffered_message    = calloc(1, (blocks * block_size));

key = calloc(1, key_size);
strcpy(key, "&*GHLKPK7G1SD4CF%6HJ0(IV#X6f0(PK");

mfd = mcrypt_module_open(MCRYPT_RIJNDAEL_256, NULL, "cbc", NULL);

if(mfd == MCRYPT_FAILED)
{
    printf("Mcrypt module open failed.\n");
    return 1;
}

/** Generate random IV */
srand(time(0));
IV = malloc(mcrypt_enc_get_iv_size(mfd));
for(i = 0; i < mcrypt_enc_get_iv_size(mfd); i++)
{
    IV[i] = rand();
}

/** Initialize cipher with key and IV */
i = mcrypt_generic_init(mfd, key, key_size, IV);
if(i < 0)
{
    mcrypt_perror(i);
    return 1;
}

strncpy(buffered_message, message, strlen(message));    
mcrypt_generic(mfd, buffered_message, block_size);
printf("The encrypted message buffer contains %s\n", buffered_message);
mdecrypt_generic(mfd, buffered_message, block_size);
printf("The original string was %s\n", buffered_message);
mcrypt_generic_deinit(mfd);
mcrypt_module_close(mfd);
return 0;
}
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
MCRYPT-mfd;
字符*键;
字符*纯文本;
char*IV;
无符号字符*消息,*缓冲消息,*ptr;
int i,块,键大小=32,块大小=32;
message=“测试消息”;
/**用于加密的缓冲消息*/
块=(int)(strlen(消息)/块大小)+1;
缓冲的_消息=calloc(1,(块*块大小));
键=calloc(1,键大小);
strcpy(键“&*GHLKPK7G1SD4CF%6HJ0(IV#X6f0(PK”);
mfd=mcrypt_模块打开(mcrypt_RIJNDAEL_256,空,“cbc”,空);
如果(mfd==MCRYPT_失败)
{
printf(“Mcrypt模块打开失败。\n”);
返回1;
}
/**生成随机IV*/
srand(时间(0));
IV=malloc(mcrypt_enc_get_IV_size(mfd));
对于(i=0;i
您需要重新初始化描述符
mfd
进行解密,不能将同一描述符用于加密和解密。

您需要重新初始化描述符
mfd
进行解密,不能将同一描述符用于加密和解密