Implementing AES encryption with OpenSSL and C++.

huangapple go评论66阅读模式
英文:

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&lt;std::uint8_t&gt; HexToBytes(const std::string&amp; hexString) {
	std::vector&lt;std::uint8_t&gt; byteArray;

	for (std::size_t i = 0; i &lt; 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&lt;std::uint8_t&gt;&amp; data)
{
	std::string keyString = &quot;FE7F64F9B5592EDFC84CA5B07DE0901F0671EDB6105FDD5D7C5006C2C10F4ADB&quot;;
	std::string ivString = &quot;95E060482AD77FB9714DF74150753A37&quot;;

	printf(&quot;noncrypted data: %s\n&quot;, data.data());
	std::vector&lt;std::uint8_t&gt; encOut(data.size());

	auto keyBytes = HexToBytes(keyString);
	auto ivBytes = HexToBytes(ivString);

	// Zero out memory for iv &amp; 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, &amp;encKey);
	AES_cbc_encrypt(data.data(), encOut.data(), data.size(), &amp;encKey, iv, AES_ENCRYPT);
	printf(&quot;encrypted data: %s\n&quot;, encOut.data());

	SendPacket(encOut);
}

huangapple
  • 本文由 发表于 2023年2月16日 10:27:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定