C++ libgcrypt中的AES128未加密
我一直在为我的一个小型加密项目尝试libgcrypt,但我似乎无法正确实现en/解密。下面介绍了类及其用法C++ libgcrypt中的AES128未加密,c++,encryption,libgcrypt,C++,Encryption,Libgcrypt,我一直在为我的一个小型加密项目尝试libgcrypt,但我似乎无法正确实现en/解密。下面介绍了类及其用法 #include <iostream> #include <string> #include <cstdlib> #include <gcrypt.h> #include "aes.h" #define GCRY_CIPHER GCRY_CIPHER_AES128 #define GCRY_MODE GCRY_CIPHER_MODE_
#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;
}
奇怪的是,我用几乎完全相同的语句编写了一个测试程序,它可以完美地工作。当我试图将它打包成一个类时,它开始崩溃。下面是这个程序的代码,如果有人想看的话
#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;
#定义GCRY\u密码GCRY\u密码AES128//在此处选择密码
#定义GCRY\u MODE GCRY\u CIPHER\u MODE\u ECB//在此处选择密码模式
无效测试(无效)
{
gcry\u密码\u hd\u t句柄;
size\u t keyLength=gcry\u cipher\u get\u algo\u keylen(gcry\u cipher);
字符串txtBuffer(“123456789 abcdefghijklmnopqrstuvzyz abcdefghijklmnopqrstuvzyz”);
size\u t txtLength=txtBuffer.size()+1;//字符串加终止符
char*encBuffer=(char*)malloc(txtLength);
char*exputffer=(char*)malloc(txtLength);
char*key=“一个测试AES密钥”;//16字节
gcry_控制(GCRYCTL_禁用_SECMEM,0);
gcry\u控制(GCRYCTL\u初始化完成,0);
gcry\u密码打开(&handle,gcry\u密码,gcry\u模式,0);
gcry_cipher_setkey(句柄、密钥、密钥长度);
gcry_cipher_encrypt(句柄,encBuffer,txtLength,txtBuffer.c_str(),txtLength);
gcry_cipher_decrypt(句柄、突发、txtLength、encBuffer、txtLength);
尺寸指数;
printf(“encBuffer=”);
对于(index=0;index当您使用std::string
捕获加密数据时,由于加密字符串中存在'\0'
,可能会丢失一些数据
尝试改用std::vector
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;
}
void aes::encrypt(字符串文本、std::vector和ret){
size\u t textLength=text.size()+1;
调整大小(文本长度);
gcry_cipher_encrypt(句柄,ret.data(),textLength,text.c_str(),textLength);
}
字符串aes::解密(std::向量常量和文本){
size\u t textLength=text.size()+1;
//因为您在C++域中,使用新的和删除
//而不是malloc和free。
char*decBuffer=新字符[textLength];
gcry_cipher_decrypt(句柄、decBuffer、textLength、text.data()、textLength);
字符串ret(decBuffer);
删除[]数据缓冲区;
返回ret;
}
您能提供您的aes.h头文件吗?您好,我解密时打印垃圾字符时遇到了同样的问题。此解决方案似乎无法解决此问题。@MrDiggles当您说它有效时,您可以将字符串打印到控制台并查看解密的消息吗?@KS7X,我鼓励您创建一篇新文章,并添加一个链接to这是你帖子中的答案。@RSahu我已经创建了一个新帖子。希望你能给我一些建议,谢谢
#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;
}
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;
}