gpt4 book ai didi

c - C 中的 Libgcrypt 返回 42

转载 作者:太空宇宙 更新时间:2023-11-03 23:58:13 27 4
gpt4 key购买 nike

我想先说明一下,我是 C 的新手,尤其是使用像 libgcrypt 这样大的库,所以如果它非常简单,请理解。

所以,我一直在尝试加密一些文本,只是为了有点像启动板,这样我就可以在加密字节之前获得更好的信心,这是这个小应用程序的最终目标之一,如果你想打电话它那个。我一直在关注this ,但仍在努力让它发挥作用。真正让我感到困惑的部分是我将其大小与输入文本相匹配的输出缓冲区传递给它。我确实觉得奇怪,这个站点告诉我使输出缓冲区的大小与输入相同,即使通常在使用 AES 加密时输出的字节数很大。也许我错过了一些更有经验的人可以帮助我的东西,我现在至少阅读了他们的代码 3-4 次,我觉得它一定是我在这里错过的非常简单的东西。

编辑

客户端.c

#include "client.h"

void printGCRYPTError(gcry_error_t err) {
if (err) {
{
fprintf (stderr, "Failure: %s/%s\n",
gcry_strsource (err),
gcry_strerror (err));
fprintf (stdout, "Failure: %s/%s\n",
gcry_strsource (err),
gcry_strerror (err));
}
}
}

char* Encrypt(char* in, char* key, char* iv){
size_t keyLength = gcry_cipher_get_algo_keylen(GCRY_CIPHER);
size_t blkLength = gcry_cipher_get_algo_blklen(GCRY_CIPHER);

if(strlen(key) != keyLength) {
printf("Invalid Key Size. Expected %zu got %lu\n", keyLength, strlen(key));
return NULL;
}
if(strlen(iv) != blkLength) {
printf("Invalid BLK/IV Size. Expected %zu got %lu\n", blkLength, strlen(iv));
return NULL;
}

gcry_cipher_hd_t handle;
gcry_error_t err = 0;

size_t inLen = strlen(in)+1;
char * out = malloc(inLen);

// Open
err = gcry_cipher_open(&handle, GCRY_CIPHER, GCRY_C_MODE, 0);
printGCRYPTError(err);

// Set Key
err = gcry_cipher_setkey(handle, key, keyLength);
printGCRYPTError(err);

// Set IV
err = gcry_cipher_setiv(handle, iv, blkLength);
printGCRYPTError(err);

// Enc
printf("Encrypting...\n");
err = gcry_cipher_encrypt(handle, out, inLen, in, inLen);
printGCRYPTError(err);
printf("Encrypted.\n");
gcry_cipher_final(handle);


//Out
printf("Plaintext Message: %s\n", in);
printf("Encrypted Message: ");
int index = 0;
for (index = 0; index<inLen; index++) {
printf("%02X", (unsigned char)out[index]);
}
printf("\n");

// Cleanup!
gcry_cipher_close(handle);

// Return
return out;
}

char* Decrypt(char* in, char* key, char* iv){
size_t keyLength = gcry_cipher_get_algo_keylen(GCRY_CIPHER);
size_t blkLength = gcry_cipher_get_algo_blklen(GCRY_CIPHER);

if(strlen(key) != keyLength) {
printf("Invalid Key Size. Expected %zu got %lu\n", keyLength, strlen(key));
return NULL;
}
if(strlen(iv) != blkLength) {
printf("Invalid BLK/IV Size. Expected %zu got %lu\n", blkLength, strlen(iv));
return NULL;
}

gcry_cipher_hd_t handle;
gcry_error_t err = 0;

size_t inLen = strlen(in);
char * out = malloc(inLen);

// Open
err = gcry_cipher_open(&handle, GCRY_CIPHER, GCRY_C_MODE, 0);
printGCRYPTError(err);

// Set Key
err = gcry_cipher_setkey(handle, key, keyLength);
printGCRYPTError(err);

// Set IV
err = gcry_cipher_setiv(handle, iv, blkLength);
printGCRYPTError(err);

// Dec
printf("Decrypting...\n");
err = gcry_cipher_decrypt(handle, out, inLen, in, inLen);
printGCRYPTError(err);
printf("Decrypted.\n");
gcry_cipher_final(handle);

//Out
printf("Encrypted Message: ");
int index = 0;
for (index = 0; index<inLen; index++) {
printf("%02X", (unsigned char)out[index]);
}
printf("\n");
printf("Decrypted Message: %s\n", out);

// Cleanup!
gcry_cipher_close(handle);

// Return
return out;
}

int main() {
//aesTest();
char* encrypted = Encrypt("My test message", "sBK0hcXddz6mIKEsBK0hcXddz6mIKEAS", "sBK0uhSAUSUSHKES");
char* decrypted = Decrypt(encrypted, "sBK0hcXddz6mIKEsBK0hcXddz6mIKEAS", "sBK0uhSAUSUSHKES");
}

控制台输出

Encrypting...
Encrypted.
Plaintext Message: My test message
Encrypted Message: 153C01DD7821E5113C25D2038F3644F7
Decrypting...
Failure: gcrypt/Invalid length
Failure: gcrypt/Invalid length
Decrypted.
Encrypted Message: 0000000000001000000000000000101400
Decrypted Message:

感谢您的关注!

最佳答案

根据 libgcrypt 的文档:

Some encryption modes require that gcry_cipher_final is used before the final data chunk is passed to this function.

所以我怀疑你需要做两件事:

  1. 检查 gcry_cipher_encrypt 的错误代码结果是否与您执行先前函数调用的方式一致。

  2. 调用加密后调用 gcry_cipher_final(handle) - 假设您正在对小于密码 block 长度的缓冲区进行一次性加密。

关于c - C 中的 Libgcrypt 返回 42,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56266972/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com