AES128 in libgcrypt not encrypting

别来无恙 提交于 2019-12-11 03:03:51

问题


I've been trying to the libgcrypt for a small cryptography project of mine, but I can't seem to be able to implement the en/decryption correctly. The following the class and the usage of it.

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

And I use it in a main function like so:

...
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;
...

And the output is

BBBBBBBBBBBBB
�n�S[

The odd thing is, I made a test program with almost the exact same statements that works perfectly. It started falling apart when I tried to package it into a class. The following is the code for this program, if anyone wants to see it.

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


When you use a std::string to capture the encrypted data, you are possibly losing some data due to the presence of '\0' in the encrypted string.

Try using std::vector<char> instead.

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


来源:https://stackoverflow.com/questions/26189967/aes128-in-libgcrypt-not-encrypting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!