gpt4 book ai didi

c++ - libgcrypt 中的 AES128 未加密

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:42:41 27 4
gpt4 key购买 nike

我一直在尝试为我的一个小型密码学项目使用 libgcrypt,但我似乎无法正确实现加密/解密。以下类及其用法。

#include <iostream>
#include <string>
#include <cstdlib>
#include <gcrypt.h>
#include "aes.h"

#define GCRY_CIPHER GCRY_CIPHER_AES128
#define GCRY_MODE GCRY_CIPHER_MODE_ECB

using namespace std;

aes::aes(string a) {
key = a;
keyLength = gcry_cipher_get_algo_keylen(GCRY_CIPHER);

gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
gcry_cipher_open(&handle, GCRY_CIPHER, GCRY_MODE, 0);
gcry_cipher_setkey(handle, key.c_str(), keyLength);
}

string aes::encrypt(string text) {
size_t textLength = text.size() + 1;
char * encBuffer = (char *)malloc(textLength);
gcry_cipher_encrypt(handle, encBuffer, textLength, text.c_str(), textLength);
string ret (encBuffer);
return ret;
}

string aes::decrypt(string text) {
size_t textLength = text.size() + 1;
char * decBuffer = (char * )malloc(textLength);
gcry_cipher_decrypt(handle, decBuffer, textLength, text.c_str(), textLength);
string ret (decBuffer);
return ret;
}

我在主函数中使用它,如下所示:

...
aes bb = aes("one test AES key");
string test = "Some Message";
string enc = bb.encrypt(test);
string dec = bb.decrypt(enc);

for (size_t index = 0; index<enc.size(); index++)
printf("%c", enc[index]);
printf("\n");

cout << dec << endl;
...

输出是

BBBBBBBBBBBBB
�n�S[

奇怪的是,我用几乎完全相同的语句制作了一个运行完美的测试程序。当我试图将它打包成一个类时,它开始分崩离析。下面是这个程序的代码,有需要的 friend 可以看看。

#include <cstdlib>
#include <iostream>
#include <string>
#include <gcrypt.h>

using namespace std;

#define GCRY_CIPHER GCRY_CIPHER_AES128 // Pick the cipher here
#define GCRY_MODE GCRY_CIPHER_MODE_ECB // Pick the cipher mode here

void aesTest(void)
{
gcry_cipher_hd_t handle;
size_t keyLength = gcry_cipher_get_algo_keylen(GCRY_CIPHER);

string txtBuffer ("123456789 abcdefghijklmnopqrstuvwzyz ABCDEFGHIJKLMNOPQRSTUVWZYZ");
size_t txtLength = txtBuffer.size() +1; // string plus termination
char * encBuffer = (char *)malloc(txtLength);
char * outBuffer = (char *)malloc(txtLength);

char * key = "one test AES key"; // 16 bytes

gcry_control (GCRYCTL_DISABLE_SECMEM, 0);

gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);

gcry_cipher_open(&handle, GCRY_CIPHER, GCRY_MODE, 0);

gcry_cipher_setkey(handle, key, keyLength);

gcry_cipher_encrypt(handle, encBuffer, txtLength, txtBuffer.c_str(), txtLength);

gcry_cipher_decrypt(handle, outBuffer, txtLength, encBuffer, txtLength);

size_t index;
printf("encBuffer = ");
for (index = 0; index<txtLength; index++)
printf("%c", encBuffer[index]);
printf("\n");

printf("outBuffer = %s\n", outBuffer);

gcry_cipher_close(handle);
free(encBuffer);
free(outBuffer);
}


int main() {
aesTest();
return 0;
}

最佳答案

当您使用 std::string要捕获加密数据,由于 '\0' 的存在,您可能会丢失一些数据在加密的字符串中。

尝试使用 std::vector<char>相反。

void aes::encrypt(string text, std::vector<char>& ret) {
size_t textLength = text.size() + 1;
ret.resize(textLength);
gcry_cipher_encrypt(handle, ret.data(), textLength, text.c_str(), textLength);
}

string aes::decrypt(std::vector<char> const& text) {
size_t textLength = text.size() + 1;

// Since you are in C++ land, use new and delete
// instead of malloc and free.

char * decBuffer = new char[textLength];
gcry_cipher_decrypt(handle, decBuffer, textLength, text.data(), textLength);
string ret (decBuffer);
delete [] decBuffer;
return ret;
}

关于c++ - libgcrypt 中的 AES128 未加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26189967/

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