gpt4 book ai didi

c++ - 我的 AES 加密/解密函数不适用于随机 ivecs

转载 作者:行者123 更新时间:2023-11-30 01:00:31 25 4
gpt4 key购买 nike

我很无聊,写了一个围绕 openSSL 的包装器来用更少的工作进行 AES 加密。如果我这样做: http://pastebin.com/V1eqz4jp (ivec = 0)
一切正常,但默认的ivec全为0,存在一些安全问题。由于无论如何我都将数据作为字符串传回,我想,为什么不生成一个随机 ivec 并将其粘贴到前面,当我解密字符串时将其取回?但出于某种原因,它不起作用。

实际上,它几乎可以工作。它似乎解密了字符串的中间部分,但不是开头的 或结尾 :

String is: 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF
Encrypting..
���l%%1u���B!
�����`pN)�ɶ���[l�ӏ��{�Q�?�2�/�HԵ�y"�=Z�Cu����l%%1u���B!

Decrypting..
String is: �%���G*�5J�0��0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF

老实说,我不知道出了什么问题。也许是一些愚蠢的错误,或者我可能遗漏了一些关于 AES 的信息?

这是代码:(经过编辑以合并 Steve Jessop 对我的第一个问题的解决方案)

/*!
* Simple AES
* Brendan Long
* March 29, 2010
*
* Simplified encryption and decryption using OpenSSL's AES library.
* Remember to compile with -lcrypto and link against the library
* g++ (your stuff) -lcrypto simpleAes.cpp (or simpleAes.o)
*
* Implementation note: Using the default ivec (0) is not secure. For
* the full security that AES offers, use a different
* ivec each time (it does not need to be secret,
* just different.
*
* This code is released into the public domain. Yada yada..
* Read this for details: http://creativecommons.org/licenses/publicdomain/
*
* If for some reason public domain isn't good enough, you may use, alter,
* distribute or do anything else you want with this code with no restrictions.
*/

#include <openssl/aes.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>

bool seed = true;

/*!
* Encrypts a string using AES with a 256 bit key
* Note: If the key is less than 32 bytes, it will be null padded.
* If the key is greater than 32 bytes, it will be truncated
* \param in The string to encrypt
* \param key The key to encrypt with
* \return The encrypted data
*/
std::string aes_encrypt(std::string in, std::string key){

// Seed the random number generator once
if(seed){
srand( (unsigned int) time(NULL));
seed = false;
}

// Generate a random ivec
unsigned char ivec[16];
for(int i=0; i<16; i++){
ivec[i] = (unsigned char) rand();
}

// Round up to AES_BLOCK_SIZE
size_t textLength = ((in.length() / AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE;

// Always pad the key to 32 bits.. because we can
if(key.length() < 32){
key.append(32 - key.length(), '\0');
}

// Get some space ready for the output
unsigned char *output = new unsigned char[textLength];

// Generate a key
AES_KEY *aesKey = new AES_KEY;
AES_set_encrypt_key((unsigned char*)key.c_str(), 256, aesKey);

// Encrypt the data
AES_cbc_encrypt((unsigned char*)in.c_str(), output, in.length() + 1, aesKey, ivec, AES_ENCRYPT);

// Make the data into a string
std::string ret((char*) output, textLength);

// Add the ivec to the front
ret = std::string((char*)ivec, 16) + ret;

// Clean up
delete output;
delete aesKey;

return ret;
}

/*!
* Decrypts a string using AES with a 256 bit key
* Note: If the key is less than 32 bytes, it will be null padded.
* If the key is greater than 32 bytes, it will be truncated
* \param in The string to decrypt
* \param key The key to decrypt with
* \return The decrypted data
*/
std::string aes_decrypt(std::string in, std::string key){

// Get the ivec from the front
unsigned char ivec[16];
for(int i=0;i<16; i++){
ivec[i] = in[i];
}

in = in.substr(16);

// Always pad the key to 32 bits.. because we can
if(key.length() < 32){
key.append(32 - key.length(), '\0');
}

// Create some space for output
unsigned char *output = new unsigned char[in.length()];

// Generate a key
AES_KEY *aesKey = new AES_KEY;
AES_set_decrypt_key((unsigned char*)key.c_str(), 256, aesKey); // key length is in bits, so 32 * 8 = 256

// Decrypt the data
AES_cbc_encrypt((unsigned char*)in.c_str(), output, in.length(), aesKey, ivec, AES_DECRYPT);

// Make the output into a string
std::string ret((char*) output);

// Clean up
delete output;
delete aesKey;

return ret;
}

最佳答案

您应该在加密之前将 ivec[16] 保存到“输出”中。就是这样...

我还想补充一点,使用 char* 而不是 string 会简单得多。

关于c++ - 我的 AES 加密/解密函数不适用于随机 ivecs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2541971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com