libgcrypt中的AES128不加密

时间:2014-10-04 05:27:13

标签: c++ encryption libgcrypt

我一直在尝试将libgcrypt用于我的小型加密项目,但我似乎无法正确实现en / decryption。以下是课程及其用法。

#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;
}

1 个答案:

答案 0 :(得分:2)

当您使用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;
}