在C 示例中解密AE
Decrypt AES in C++ Example
我需要使用开放式SSL库中的AES解密在C 中解密A char数组时需要一些帮助。我已经完成了加密模式并且工作正常,但是解密行不通。
这是加密函数:
string Encrypt(char *Key, char *Msg, int size)
{
static char* Res;
static const char* const lut = "0123456789ABCDEF";
string output;
AES_KEY enc_key;
Res = (char *)malloc(size);
AES_set_encrypt_key((unsigned char *)Key, 128, &enc_key);
for(int vuelta = 0; vuelta <= size; vuelta += 16)
{
AES_ecb_encrypt((unsigned char *)Msg + vuelta, (unsigned char *)Res + vuelta, &enc_key, AES_ENCRYPT);
}
output.reserve(2 * size);
for (size_t i = 0; i < size; ++i)
{
const unsigned char c = Res[i];
output.push_back(lut[c >> 4]);
output.push_back(lut[c & 15]);
}
free(Res);
return output;
}
这是解密函数(不工作(:
char * Decrypt( char *Key, char *Msg, int size)
{
static char* Res;
AES_KEY dec_key;
Res = ( char * ) malloc( size );
AES_set_decrypt_key(( unsigned char * ) Key, 128, &dec_key);
for(int vuelta= 0; vuelta<=size; vuelta+=16)
{
AES_ecb_encrypt(( unsigned char * ) Msg+vuelta, ( unsigned char * ) Res+vuelta, &dec_key, AES_DECRYPT);
}
return (Res);
}
这是调用该方法的主要函数的一个示例,问题是我如何在解密函数中打印" res"变量,它始终显示随机的ascii值,我喜欢在像Encrypt函数之类的字符串:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "openSSL/aes.h"
using namespace std;
int main(int argc, char const *argv[])
{
char key[16];
char message[128];
char enc_message[128];
string s_key = "THIS_IS_THE_KEY_";
string s_message = "Hello World !!!";
memset(key, 0, sizeof(key));
strcpy(key, s_key.c_str());
memset(message, 0, sizeof(message));
strcpy(message, s_message.c_str());
string response = Encrypt(key, message, sizeof(message));
cout<<"This is the Encrypted Message: "<<response<<endl;
memset(enc_message, 0, sizeof(enc_message));
strcpy(enc_message, response.c_str());
Decrypt(key, enc_message, sizeof(enc_message));
return 0;
}
在这种方法中有任何改进吗?
我想对我的解决方法提出答案:示例的问题是我试图用十六进制字符串使用解密功能,应该使用ASCII完成带有加密函数传递的值的字符串。
也就是说,而不是试图解密这样的字符串: 461D019896EFA3
必须用这样的字符串解密:@(%_!#$
之后,解密将以ASCII值传递。它们必须传递给十六进制,最后是一根弦。
这是对我有用的示例:
string Decrypt_string(char *Key, string HEX_Message, int size)
{
static const char* const lut = "0123456789ABCDEF";
int i = 0;
char* Res;
AES_KEY dec_key;
string auxString, output, newString;
for(i = 0; i < size; i += 2)
{
string byte = HEX_Message.substr(i, 2);
char chr = (char) (int)strtol(byte.c_str(), NULL, 16);
auxString.push_back(chr);
}
const char *Msg = auxString.c_str();
Res = (char *)malloc(size);
AES_set_decrypt_key((unsigned char *)Key, 128, &dec_key);
for(i = 0; i <= size; i += 16)
{
AES_ecb_encrypt((unsigned char *)Msg + i, (unsigned char *)Res + i, &dec_key, AES_DECRYPT);
}
output.reserve(2 * size);
for (size_t i = 0; i < size; ++i)
{
const unsigned char c = Res[i];
output.push_back(lut[c >> 4]);
output.push_back(lut[c & 15]);
}
int len = output.length();
for(int i = 0; i < len; i += 2)
{
string byte = output.substr(i, 2);
char chr = (char) (int)strtol(byte.c_str(), NULL, 16);
newString.push_back(chr);
}
free(Res);
return newString;
}
相关文章:
- AES加密到解密未正确输出
- 为什么这不是解密
- 通过 Openssl 命令行加密,通过 c++ 解密
- OpenSSL 解密功能无法正常工作
- 仿射密码解密,输出大小写不同
- 将加密消息从 php 发送到 C++ 应用程序,然后使用 CryptoPP 进行解密
- 私钥解密代码使用公共接口说明符到 BSAFE 库 v6.0?
- POCO C++加密/解密
- 从加密项目向量中解密任意选择的元素会导致无效的 PKCS #7 块错误
- 尝试从 Visual Studios C++ 中的加密字符串输出解密字符串时出现逻辑错误
- OpenSSL RSA 解密随机失败 C/C++
- 压缩天然气.AES.错误解密(可能加密)的文件
- C++ - 从文件解密字符串
- C++ - 如何从上到下制作 4x4 矩阵和 cout 列(需要解密字符串)
- 运行密钥密码解密知道密钥?
- 如何在c ++中使用XOR解密文件?
- 如何制作一个程序,它使用特定单词加密和解密消息?
- 基本加密和解密方法不起作用 C++ 11.
- 凯撒密码在C++中无法正确解密的问题
- 在C 示例中解密AE