在函数gcrycipher_encrypt C++上使用gcrypt时出错
Error using gcrypt on function gcry_cipher_encrypt C++
我正试图使用gcrypt将一个简单的加密技术变成一个txt文件。当我执行命令gcry_cipher_encrypt
时,我收到以下错误:
gcry_strsource => User defined source 1
gcry_strerror => Invalid length
当我使用一个txt文件的内容时会发生这种情况,如果我在代码中硬编码文本内容,这个函数会起作用,可能的原因是什么?
文本文件内容
test test test test test
地穴代码:
void myEncrypt::aesEncrypt(char *txtInput, int txtInputSize, char *txtOutput){
gcry_error_t gcryError;
gcry_cipher_hd_t gcryCipherHd;
size_t index;
size_t keyLength = gcry_cipher_get_algo_keylen(GCRY_CIPHER);
size_t blkLength = gcry_cipher_get_algo_blklen(GCRY_CIPHER);
//char * txtBuffer = "123456789 abcdefghijklmnopqrstuvwzyz ABCDEFGHIJKLMNOPQRSTUVWZYZ 123456789 abcdefghijklmnopqrstuvwzyz ABCDEFGHIJKLMNOPQRSTUVWZYZ";
char * txtBuffer = txtInput;
size_t txtLength = strlen(txtBuffer)+1; // string plus termination
char * encBuffer = (char *)malloc(txtLength);
char * outBuffer = (char *)malloc(txtLength);
char * aesSymKey = "one test AES key"; // 16 bytes
char * iniVector = "a test ini value"; // 16 bytes
gcryError = gcry_cipher_open(
&gcryCipherHd, // gcry_cipher_hd_t *
GCRY_CIPHER, // int
GCRY_C_MODE, // int
0); // unsigned int
if (gcryError)
{
printf("gcry_cipher_open failed: %s/%sn",
gcry_strsource(gcryError),
gcry_strerror(gcryError));
return;
}
printf("gcry_cipher_open workedn");
gcryError = gcry_cipher_setkey(gcryCipherHd, aesSymKey, keyLength);
if (gcryError)
{
printf("gcry_cipher_setkey failed: %s/%sn",
gcry_strsource(gcryError),
gcry_strerror(gcryError));
return;
}
printf("gcry_cipher_setkey workedn");
gcryError = gcry_cipher_setiv(gcryCipherHd, iniVector, blkLength);
if (gcryError)
{
printf("gcry_cipher_setiv failed: %s/%sn",
gcry_strsource(gcryError),
gcry_strerror(gcryError));
return;
}
printf("gcry_cipher_setiv workedn");
gcryError = gcry_cipher_encrypt(
gcryCipherHd, // gcry_cipher_hd_t
encBuffer, // void *
txtLength, // size_t
txtBuffer, // const void *
txtLength); // size_t
if (gcryError)
{
printf("gcry_cipher_encrypt failed: %s/%sn",
gcry_strsource(gcryError),
gcry_strerror(gcryError));
return;
}
printf("gcry_cipher_encrypt workedn");
gcryError = gcry_cipher_setiv(gcryCipherHd, iniVector, blkLength);
if (gcryError)
{
printf("gcry_cipher_setiv failed: %s/%sn",
gcry_strsource(gcryError),
gcry_strerror(gcryError));
return;
}
printf("gcry_cipher_setiv workedn");
gcryError = gcry_cipher_decrypt(
gcryCipherHd, // gcry_cipher_hd_t
outBuffer, // void *
txtLength, // size_t
encBuffer, // const void *
txtLength); // size_t
if (gcryError)
{
printf("gcry_cipher_decrypt failed: %s/%sn",
gcry_strsource(gcryError),
gcry_strerror(gcryError));
return;
}
printf("gcry_cipher_decrypt workedn");
printf("keyLength = %dn", keyLength);
printf("blkLength = %dn", blkLength);
printf("txtLength = %dn", txtLength);
printf("aesSymKey = %sn", aesSymKey);
printf("iniVector = %sn", iniVector);
printf("txtBuffer = %sn", txtBuffer);
printf("encBuffer = ");
for (index = 0; index<txtLength; index++)
printf("%02X", (unsigned char)encBuffer[index]);
printf("n");
printf("outBuffer = %sn", outBuffer);
// clean up after ourselves
gcry_cipher_close(gcryCipherHd);
free(encBuffer);
free(outBuffer);
}
嗯。。。我不知道libgcrypt,但是。。。
在本页中,在gcry_cipher_encrypt()
功能的描述中,我阅读了
根据所选算法和加密模式,缓冲区的长度必须是块大小的倍数。
如果我理解得很好,你使用AES作为算法,所以根据各种来源(例如维基百科),AES的块大小是16字节。
如果你看一下原始的txtBuffer
,你可以看到它是63个字节(9个数字,26+26个字母,2个空格),所以(加上+1表示终端零)txtLength
是64,正好是16的倍数。
你的文本是25字节,所以你的texLength
如果是26,那不是16的倍数。
顺便说一句:你使用的是C++,所以我强烈建议不要使用malloc()
,而是使用new []
;或者(更好地,IMHO)std::string
与reserve()
、c_str()
和data()
。
我想您应该使用txtBuffer
作为中间分配的缓冲区。
使用new []
解决方案,我想您应该以这种方式修改代码
std::size_t txtLength = strlen(txtInput)+1; // from txtInput, not txtBuffer
if ( 0U != (txtLength & 0xfU) )
txtLength += 0x10U - (txtLength & 0xfU);
char * txtBuffer = new char[txtLength];
char * encBuffer = new char[txtLength];
char * outBuffer = new char[txtLength];
std::strcpy(txtBuffer, txtInput);
附言:小心,代码未测试。
抱歉我英语不好。
相关文章:
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 在Google Kick start中提交时出错
- 在c++中访问int到类对象的映射时出错
- 分段错误当我试图运行程序时出错
- 使用dynamic_cast和构造函数时出错
- CHECK(调用)函数在Google Colab中出错
- 用pybind11包装C++抽象类时出错
- 为x86而非x64编译时出错
- 从R调用C++函数并对其进行集成时出错
- 这个函数哪里出错了
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 为重写std::exception的库生成swig接口时出错
- 导入/导出变量时出错
- 在函数gcrycipher_encrypt C++上使用gcrypt时出错