英文:
Implementing AES encryption with OpenSSL and C++
问题
我正在尝试在我的应用程序中使用CBC来实现AES加密。但是,每次运行它时,我得到的数据都不同。我一直在尝试使用调试器来识别问题,但我不太明白我应该寻找什么。
我的实现如下;
#define AES_KEYLENGTH 256
void NetworkHandler::SendEncryptedPacket(std::vector<std::uint8_t>& data)
{
std::string keyString = "AC3CF1B84D7C946640447DE9670E18BE8A45F49A286FC4D8404DD729491064E4";
std::string ivString = "5D4FB5A040DE76B316794BAC89FC3A48";
printf("未加密数据: %s\n", data.data());
size_t inputLen = data.size();
size_t encLen = ((inputLen + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
std::vector<std::uint8_t> encOut(encLen);
// 将iv和key的内存清零
std::uint8_t iv[AES_BLOCK_SIZE];
std::uint8_t key[AES_KEYLENGTH];
memset(iv, 0x00, AES_BLOCK_SIZE);
memset(key, 0x00, AES_KEYLENGTH);
std::copy(keyString.begin(), keyString.end(), key);
std::copy(ivString.begin(), ivString.end(), iv);
AES_KEY encKey;
AES_set_encrypt_key(key, AES_KEYLENGTH, &encKey);
AES_cbc_encrypt(data.data(), encOut.data(), encLen, &encKey, iv, AES_ENCRYPT);
printf("加密数据: %s\n", encOut.data());
SendPacket(encOut);
}
我最初尝试遵循在这里找到的一个已实现的示例:https://stackoverflow.com/questions/43192493/aes-256-cbc-encryption-c-using-openssl。
但似乎我做得不够。
英文:
I'm trying to implement AES using CBC for my application. However the data I'm getting out is different every time I run it. I have been trying to identify the problems using a debugger, but I don't really understand what exactly I'm looking for.
My implementation looks like;
#define AES_KEYLENGTH 256
void NetworkHandler::SendEncryptedPacket(std::vector<std::uint8_t>& data)
{
std::string keyString = "AC3CF1B84D7C946640447DE9670E18BE8A45F49A286FC4D8404DD729491064E4";
std::string ivString = "5D4FB5A040DE76B316794BAC89FC3A48";
printf("noncrypted data: %s\n", data.data());
size_t inputLen = data.size();
size_t encLen = ((inputLen + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
std::vector<std::uint8_t> encOut(encLen);
// Zero out memory for iv & key
std::uint8_t iv[AES_BLOCK_SIZE];
std::uint8_t key[AES_KEYLENGTH];
memset(iv, 0x00, AES_BLOCK_SIZE);
memset(key, 0x00, AES_KEYLENGTH);
std::copy(keyString.begin(), keyString.end(), key);
std::copy(ivString.begin(), ivString.end(), iv);
AES_KEY encKey;
AES_set_encrypt_key(key, AES_KEYLENGTH, &encKey);
AES_cbc_encrypt(data.data(), encOut.data(), encLen, &encKey, iv, AES_ENCRYPT);
printf("encrypted data: %s\n", encOut.data());
SendPacket(encOut);
}
I initially tried to follow the an implemented example found here; https://stackoverflow.com/questions/43192493/aes-256-cbc-encryption-c-using-openssl.
But looks like I came short.
答案1
得分: 1
I came up with this solution.
I'm not sure if it's 100% correct, but I'm getting the same ciphertext each time.
std::vector<uint8_t> HexToBytes(const std::string& hexString) {
std::vector<uint8_t> byteArray;
for (std::size_t i = 0; i < hexString.size(); i += 2) {
std::string hexByte = hexString.substr(i, 2);
std::uint8_t byte = std::stoi(hexByte, nullptr, 16);
byteArray.push_back(byte);
}
return byteArray;
}
#define AES_KEYLENGTH 256
void NetworkHandler::SendEncryptedPacket(std::vector<uint8_t>& data)
{
std::string keyString = "FE7F64F9B5592EDFC84CA5B07DE0901F0671EDB6105FDD5D7C5006C2C10F4ADB";
std::string ivString = "95E060482AD77FB9714DF74150753A37";
printf("noncrypted data: %s\n", data.data());
std::vector<uint8_t> encOut(data.size());
auto keyBytes = HexToBytes(keyString);
auto ivBytes = HexToBytes(ivString);
// Zero out memory for iv & key
std::uint8_t iv[AES_BLOCK_SIZE];
std::uint8_t key[AES_KEYLENGTH];
memset(iv, 0x00, AES_BLOCK_SIZE);
memset(key, 0x00, AES_KEYLENGTH);
std::copy(keyBytes.begin(), keyBytes.end(), key);
std::copy(ivBytes.begin(), ivBytes.end(), iv);
AES_KEY encKey;
AES_set_encrypt_key(key, AES_KEYLENGTH, &encKey);
AES_cbc_encrypt(data.data(), encOut.data(), data.size(), &encKey, iv, AES_ENCRYPT);
printf("encrypted data: %s\n", encOut.data());
SendPacket(encOut);
}
英文:
I came up with this solution.
I'm not sure if it's 100% correct, but I'm getting the same ciphertext each time.
std::vector<std::uint8_t> HexToBytes(const std::string& hexString) {
std::vector<std::uint8_t> byteArray;
for (std::size_t i = 0; i < hexString.size(); i += 2) {
std::string hexByte = hexString.substr(i, 2);
std::uint8_t byte = std::stoi(hexByte, nullptr, 16);
byteArray.push_back(byte);
}
return byteArray;
}
#define AES_KEYLENGTH 256
void NetworkHandler::SendEncryptedPacket(std::vector<std::uint8_t>& data)
{
std::string keyString = "FE7F64F9B5592EDFC84CA5B07DE0901F0671EDB6105FDD5D7C5006C2C10F4ADB";
std::string ivString = "95E060482AD77FB9714DF74150753A37";
printf("noncrypted data: %s\n", data.data());
std::vector<std::uint8_t> encOut(data.size());
auto keyBytes = HexToBytes(keyString);
auto ivBytes = HexToBytes(ivString);
// Zero out memory for iv & key
std::uint8_t iv[AES_BLOCK_SIZE];
std::uint8_t key[AES_KEYLENGTH];
memset(iv, 0x00, AES_BLOCK_SIZE);
memset(key, 0x00, AES_KEYLENGTH);
std::copy(keyBytes.begin(), keyBytes.end(), key);
std::copy(ivBytes.begin(), ivBytes.end(), iv);
AES_KEY encKey;
AES_set_encrypt_key(key, AES_KEYLENGTH, &encKey);
AES_cbc_encrypt(data.data(), encOut.data(), data.size(), &encKey, iv, AES_ENCRYPT);
printf("encrypted data: %s\n", encOut.data());
SendPacket(encOut);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论