c++ - AES128 加密 libgcrypt 输出垃圾

标签 c++ aes libgcrypt

我正在使用这个已解决的帖子作为引用,但似乎无法获得相同的结果:AES128 in libgcrypt not encrypting

当我将字符串打印到控制台时,我得到了垃圾字符。我认为这可能是由于 '\0' 在字符串中,但我不确定如何去做。

//aes.cpp

#include "aes.h"
#include <iostream>
#include <stdio.h>

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

aes::aes(std::string k)
{
    key_ = k;
    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_);
}

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;


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

//aes.h

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

#ifndef AES_H
#define AES_H

class aes
{
public:
    aes(std::string);
    ~aes();

    gcry_cipher_hd_t handle;
    void encrypt(std::string, std::vector<char>&);
    std::string decrypt(std::vector<char> const&); 

private:
    std::string key_;
    size_t keyLength_; 
};

#endif // AES_H

// main.cpp

#include "aes.h"
#include <iostream>

int main()
{
    std::vector<char> data;

    aes bb = aes("one test AES key");
    bb.encrypt("Some message", data);

    std::string dec = bb.decrypt(data);
    std::cout << "decrypted string " << dec << std::endl;
    return 0;
    //output gives me: decrypted string �

}

最佳答案

对于ECB模式下的AES128,要加密的数据大小必须是128位的倍数。明文和密文大小必须完全相同。

以下是工作代码,可让您了解需要执行哪些操作才能使其正常工作。但我强烈建议您重新考虑使用 ECB。

#include <gcrypt.h>
#include <iostream>
#include <vector>
#include <stdio.h>

class aes
{
public:
    aes(std::string);
    ~aes() { ; }

    gcry_cipher_hd_t handle;
    void encrypt(std::string, std::vector<char>&);
    std::string decrypt(std::vector<char> const&); 

private:
    std::string key_;
    size_t keyLength_; 
};

#define GCRY_CIPHER GCRY_CIPHER_AES128
#define GCRY_MODE GCRY_CIPHER_MODE_ECB

aes::aes(std::string k)
{
    key_ = k;
    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_);
}

void aes::encrypt(std::string text, std::vector<char>& ret)
{

    // round up to the nearest multiple of the AES block size
    while ((text.size() & 0xf) != 0xf)
        text += " ";

    size_t textLength = text.size() + 1;
    ret.resize(textLength);
    int err = gcry_cipher_encrypt(handle, ret.data(), textLength, text.c_str(), textLength);
    if (err != 0)
    {
       std::cout << "encrypt fail" <<
           gcry_strsource(err) << " " <<
           gcry_strerror(err) << std::endl;
    }
}

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

    char * decBuffer = new char[textLength];

    int err=gcry_cipher_decrypt(handle, decBuffer, textLength, text.data(), textLength);
    if (err != 0)
    {
       std::cout << "decrypt fail: " <<
           gcry_strsource(err) <<  " " <<
           gcry_strerror(err) << std::endl;
     }

    std::string ret (decBuffer);
    delete [] decBuffer;
    return ret;
}

int main()
{
    std::vector<char> data;

    aes bb = aes("one test AES key");
    bb.encrypt("Some message", data);


    std::string dec = bb.decrypt(data);
    std::cout << "decrypted string " << dec << std::endl;
    return 0;
}

decrypted string Some message

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

相关文章:

c++ - 当我使用 "' 时,为什么会出现错误 'std' cout' in namespace "using cout = std::cout;"does not name a type"

c++ - <chrono> 错误太多 (std::chrono::timepoint) (VS2015)

C++ Eigen 库 : Performance overhead of Ref<>

encryption - 关于在 J2ME 中使用 AES 加密的任何好的示例或建议?

linux - 如何修复此 libgcrypt 交叉编译错误?

c - Libgcrypt 中的 AES CCM 加密和解密

c++ - unordered_set 的迭代器减法不起作用

ios - AES 解密在 iOS 7 中的行为与 iOS 8/9 不同

Java - 如何中断繁忙的线程