Implementing AES encryption with OpenSSL and C++.

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

Implementing AES encryption with OpenSSL and C++

问题

我正在尝试在我的应用程序中使用CBC来实现AES加密。但是,每次运行它时,我得到的数据都不同。我一直在尝试使用调试器来识别问题,但我不太明白我应该寻找什么。

我的实现如下;

  1. #define AES_KEYLENGTH 256
  2. void NetworkHandler::SendEncryptedPacket(std::vector<std::uint8_t>& data)
  3. {
  4. std::string keyString = "AC3CF1B84D7C946640447DE9670E18BE8A45F49A286FC4D8404DD729491064E4";
  5. std::string ivString = "5D4FB5A040DE76B316794BAC89FC3A48";
  6. printf("未加密数据: %s\n", data.data());
  7. size_t inputLen = data.size();
  8. size_t encLen = ((inputLen + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
  9. std::vector<std::uint8_t> encOut(encLen);
  10. // 将iv和key的内存清零
  11. std::uint8_t iv[AES_BLOCK_SIZE];
  12. std::uint8_t key[AES_KEYLENGTH];
  13. memset(iv, 0x00, AES_BLOCK_SIZE);
  14. memset(key, 0x00, AES_KEYLENGTH);
  15. std::copy(keyString.begin(), keyString.end(), key);
  16. std::copy(ivString.begin(), ivString.end(), iv);
  17. AES_KEY encKey;
  18. AES_set_encrypt_key(key, AES_KEYLENGTH, &encKey);
  19. AES_cbc_encrypt(data.data(), encOut.data(), encLen, &encKey, iv, AES_ENCRYPT);
  20. printf("加密数据: %s\n", encOut.data());
  21. SendPacket(encOut);
  22. }

我最初尝试遵循在这里找到的一个已实现的示例: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;

  1. #define AES_KEYLENGTH 256
  2. void NetworkHandler::SendEncryptedPacket(std::vector<std::uint8_t>& data)
  3. {
  4. std::string keyString = "AC3CF1B84D7C946640447DE9670E18BE8A45F49A286FC4D8404DD729491064E4";
  5. std::string ivString = "5D4FB5A040DE76B316794BAC89FC3A48";
  6. printf("noncrypted data: %s\n", data.data());
  7. size_t inputLen = data.size();
  8. size_t encLen = ((inputLen + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
  9. std::vector<std::uint8_t> encOut(encLen);
  10. // Zero out memory for iv & key
  11. std::uint8_t iv[AES_BLOCK_SIZE];
  12. std::uint8_t key[AES_KEYLENGTH];
  13. memset(iv, 0x00, AES_BLOCK_SIZE);
  14. memset(key, 0x00, AES_KEYLENGTH);
  15. std::copy(keyString.begin(), keyString.end(), key);
  16. std::copy(ivString.begin(), ivString.end(), iv);
  17. AES_KEY encKey;
  18. AES_set_encrypt_key(key, AES_KEYLENGTH, &encKey);
  19. AES_cbc_encrypt(data.data(), encOut.data(), encLen, &encKey, iv, AES_ENCRYPT);
  20. printf("encrypted data: %s\n", encOut.data());
  21. SendPacket(encOut);
  22. }

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.

  1. std::vector<uint8_t> HexToBytes(const std::string& hexString) {
  2. std::vector<uint8_t> byteArray;
  3. for (std::size_t i = 0; i < hexString.size(); i += 2) {
  4. std::string hexByte = hexString.substr(i, 2);
  5. std::uint8_t byte = std::stoi(hexByte, nullptr, 16);
  6. byteArray.push_back(byte);
  7. }
  8. return byteArray;
  9. }
  10. #define AES_KEYLENGTH 256
  11. void NetworkHandler::SendEncryptedPacket(std::vector<uint8_t>& data)
  12. {
  13. std::string keyString = "FE7F64F9B5592EDFC84CA5B07DE0901F0671EDB6105FDD5D7C5006C2C10F4ADB";
  14. std::string ivString = "95E060482AD77FB9714DF74150753A37";
  15. printf("noncrypted data: %s\n", data.data());
  16. std::vector<uint8_t> encOut(data.size());
  17. auto keyBytes = HexToBytes(keyString);
  18. auto ivBytes = HexToBytes(ivString);
  19. // Zero out memory for iv & key
  20. std::uint8_t iv[AES_BLOCK_SIZE];
  21. std::uint8_t key[AES_KEYLENGTH];
  22. memset(iv, 0x00, AES_BLOCK_SIZE);
  23. memset(key, 0x00, AES_KEYLENGTH);
  24. std::copy(keyBytes.begin(), keyBytes.end(), key);
  25. std::copy(ivBytes.begin(), ivBytes.end(), iv);
  26. AES_KEY encKey;
  27. AES_set_encrypt_key(key, AES_KEYLENGTH, &encKey);
  28. AES_cbc_encrypt(data.data(), encOut.data(), data.size(), &encKey, iv, AES_ENCRYPT);
  29. printf("encrypted data: %s\n", encOut.data());
  30. SendPacket(encOut);
  31. }
英文:

I came up with this solution.
I'm not sure if it's 100% correct, but I'm getting the same ciphertext each time.

  1. std::vector&lt;std::uint8_t&gt; HexToBytes(const std::string&amp; hexString) {
  2. std::vector&lt;std::uint8_t&gt; byteArray;
  3. for (std::size_t i = 0; i &lt; hexString.size(); i += 2) {
  4. std::string hexByte = hexString.substr(i, 2);
  5. std::uint8_t byte = std::stoi(hexByte, nullptr, 16);
  6. byteArray.push_back(byte);
  7. }
  8. return byteArray;
  9. }
  10. #define AES_KEYLENGTH 256
  11. void NetworkHandler::SendEncryptedPacket(std::vector&lt;std::uint8_t&gt;&amp; data)
  12. {
  13. std::string keyString = &quot;FE7F64F9B5592EDFC84CA5B07DE0901F0671EDB6105FDD5D7C5006C2C10F4ADB&quot;;
  14. std::string ivString = &quot;95E060482AD77FB9714DF74150753A37&quot;;
  15. printf(&quot;noncrypted data: %s\n&quot;, data.data());
  16. std::vector&lt;std::uint8_t&gt; encOut(data.size());
  17. auto keyBytes = HexToBytes(keyString);
  18. auto ivBytes = HexToBytes(ivString);
  19. // Zero out memory for iv &amp; key
  20. std::uint8_t iv[AES_BLOCK_SIZE];
  21. std::uint8_t key[AES_KEYLENGTH];
  22. memset(iv, 0x00, AES_BLOCK_SIZE);
  23. memset(key, 0x00, AES_KEYLENGTH);
  24. std::copy(keyBytes.begin(), keyBytes.end(), key);
  25. std::copy(ivBytes.begin(), ivBytes.end(), iv);
  26. AES_KEY encKey;
  27. AES_set_encrypt_key(key, AES_KEYLENGTH, &amp;encKey);
  28. AES_cbc_encrypt(data.data(), encOut.data(), data.size(), &amp;encKey, iv, AES_ENCRYPT);
  29. printf(&quot;encrypted data: %s\n&quot;, encOut.data());
  30. SendPacket(encOut);
  31. }

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:

确定