【发布时间】:2022-01-02 16:27:31
【问题描述】:
我有一个 python 端点,它使用 AES cbc 模式加密字符串并将其返回给用 c++ 编写的客户端软件(以十六进制空格分隔格式) link 用于 c++ 存储库
std::vector<unsigned char> cipher_as_chars(std::string cipher)
{
std::istringstream strm{cipher};
strm >> std::hex;
std::vector<unsigned char> res;
res.reserve(cipher.size() / 3 + 1);
int h;
while (strm >> h) {
res.push_back(static_cast<unsigned char>(h));
}
return res;
}
namespace client{
std::string decrypt_cipher(std::string cipher, std::string usr_key)
{
std::string original_text = "";
const std::vector<unsigned char> key = key_from_string(usr_key); // 16-char = 128-bit
const unsigned char iv[16] = {
0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x6e, 0x20, 0x49, 0x56, 0x34, 0x35, 0x36
};
std::vector<unsigned char> encrypted = cipher_as_chars(cipher);
unsigned long padded_size = 0;
std::vector<unsigned char> decrypted(encrypted.size());
plusaes::decrypt_cbc(&encrypted[0], encrypted.size(), &key[0], key.size(), &iv, &decrypted[0], decrypted.size(), &padded_size);
for (int i =0 ; i < decrypted.size(); i++)
{
//cout << decrypted[i] << endl;
std::stringstream stream;
stream << decrypted[i];
original_text = original_text + stream.str();
}
return original_text;
}
}
def encrypt_string(key,text):
result = ''
while len(text)% 16 != 0 :
text = text+" "
string_as_bytes = text.encode('utf8')
obj = AES.new(key.encode("utf8"), AES.MODE_CBC, 'This is an IV456'.encode("utf8"))
cipher_text = obj.encrypt(string_as_bytes)
for item in bytearray(cipher_text):
result += f"{hex(item).replace('0x','')} "
return result
@api.route('/test')
def test_route():
return encrypt_string("Encryptionkey123", "Happy new year people")
服务器具有与客户端软件相同的加密和解密功能,如果我使用 c++ 代码加密字符串并使用用 c++ 编写的解密函数解密它,它工作正常并且我得到相同的字符串,但是当客户端读取/test 的响应并对其进行加密,c++ 客户端尝试对其进行解密,最后输出缺少 n 个字母的字符串
python服务器中的原文Happy new year people
c++客户端中的输出Happy new year p
【问题讨论】:
-
你做了什么来调试问题?您是否将所有输入/输出都打印为十六进制?您正在谈论获取字符串的一部分,您在发送之前和接收之后查看密文大小吗?您正在使用哪些库?请提供minimal reproducible example。
-
@MaartenBodewes 对于 c++ 方面我提供了用于 python 的 AES 存储库的链接我正在使用
pycrypto对于 c++ 方面的解密大小它是16字母长小于原始5 -
好吧,我对 C++ 库的看法是正确的——尽管你为什么要使用这样的库超出了我的理解(我的意思是,我不是 C++ 开发人员,而是一个更改 @987654330 的推送请求@ 到
t_size不明白。真的吗?你为什么忽略返回的Error?你不认为这是你可以做的非常基本的调试和验证吗? -
@MaartenBodewes 我正在使用该存储库,因为它是一个支持 AES 加密的单个头库,因为
int到t_size我已经修复了这个问题,对于调试方面我不是专家这就是我在这里问的原因 -
好,所以我假设您现在正在输入并试图找出返回值,比较两边的 ciphertext 大小并以十六进制打印输入/输出?跨度>