c++ - libgcrypt 中的 AES128 未加密

标签 c++ encryption libgcrypt

我一直在尝试使用 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[

奇怪的是,我用几乎完全相同的语句编写了一个测试程序,并且运行良好。当我试图将它打包到一个类中时,它开始崩溃。下面是这个程序的代码,如果有人想看的话。

#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/

相关文章:

C 加密 : Unable to check if number is prime using libgcrypt

c++ - 等待 DBus 服务在 Qt 中可用

java - 此 Java 和 C 代码之间的区别?

java - RSA 加密编码使用 Android 解密使用 apache 编解码器问题

为 Vala 编译 gcrypt

c - AES256 Libgcrypt 无效 key 长度

c++ - 获取CUDA纹理问题

c++ - 氧气警告 : documented function not declared or defined

C++ Pugixml通过属性id获取父级的子级

ruby - 加密很难 : AES encryption to Hex