我有以下代碼來加密的密文轉換成可讀的十六進制格式:C++轉換十六進制字符串用「:」,以原來的「二進制」串
std::string convertToReadable(std::string ciphertext)
{
std::stringstream outText;
for(unsigned int i = 0; i < ciphertext.size(); i++)
outText << std::hex << std::setw(2) << std::setfill('0') << (0xFF & static_cast<byte>(ciphertext[i])) << ":";
return outText.str();
}
此函數的可讀的結果是一些爲:
56:5e:8b:a8:04:93:e2:f1:5c:20:8b:fd:f5:b7:22:0b:82:42:46:58:9b:d4:c1:8e:ac:62:85:04:ff:7f:c6:d3:
現在我需要做回來的路上,爲了可讀格式轉換爲原ciphertext
進行解密:
std::string convertFromReadable(std::string text)
{
std::istringstream cipherStream;
for(unsigned int i = 0; i < text.size(); i++)
{
if (text.substr(i, 1) == ":")
continue;
std::string str = text.substr(i, 2);
std::istringstream buffer(str);
int value;
buffer >> std::hex >> value;
cipherStream << value;
}
return cipherStream.str();
}
這不是絕對的工作,因爲我錯了字符串回來。
我該如何修復convertFromReadable()
,以便我可以回到原來的ciphertext
?
感謝您的幫助
你聽說過'const'和引用 –
值得擁有的LOOKAT「位集」 – 911
ED,請明確闡述你的觀點.. – Mendes