libgcrypt中的AES128不加密 [英] AES128 in libgcrypt not encrypting

查看:133
本文介绍了libgcrypt中的AES128不加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为我的一个小型密码学项目尝试libgcrypt,但是我似乎无法正确实现en/decrypt.下面是该类及其用法.

#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',您可能会丢失一些数据./p>

尝试改用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;
}

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

解决方案

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

这篇关于libgcrypt中的AES128不加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆