将AES密文作为参数传递
Pass AES ciphertext as parameter
我正在尝试编写两个不同的函数,以在cryptopp中使用AES加密和解密数据。我想把密文作为参数传递给解密函数。然而在解密函数上,它接收到一些特殊的符号作为密文&因此不能正确解密。请帮忙。
// -- AES encryption function ----------
void Security_packetAgent::encryption(char out[])
{
std::string plaintext = out;
std::string ciphertext = "";
CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv );
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext ) );
stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length() + 1 );
stfEncryptor.MessageEnd();
std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;
for( int i = 0; i < ciphertext.size(); i++ )
{
std::cout << "0x" << std::hex << (0xFF & static_cast<byte>(ciphertext[i])) << " ";
}
std::cout<<"nMessage encrypted ...";
std::cout << std::endl << std::endl;
sprintf(out, "%s", ciphertext.c_str());
printf("Final Data: %s : %s ", out, reinterpret_cast<const unsigned char*>(ciphertext.c_str()));
}
// ---- AES decryption ------------------
void Security_packetAgent::decryption(char out[])
{
cout<<"nCipher recieved: "<<out;
std::cout<<"nEntered decryption ..";
std::string ciphertext = out;
std::string decryptedtext;
cout<<"nCipher recieved: "<<reinterpret_cast<const unsigned char*>( ciphertext.c_str());
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext.c_str() ), ciphertext.size() );
stfDecryptor.MessageEnd();
std::cout << "Decrypted Text: " << std::endl;
std::cout << decryptedtext;
std::cout << std::endl << std::endl;
}
如果char*
指向的不是文本字符串,例如加密数据,则不应该从char*
构造std::string
。
std::string ciphertext = out;
一旦ciphertext
的结构达到0字节,它就会停止从out
进行复制。相反,您还需要传入加密数据的长度,并使用std::string(const char* data, size_t size)
构造函数。
std::string ciphertext(out, outSize);
或者,您也可以使用带有开始和结束迭代器的构造函数。
相关文章:
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用指向成员的指针将成员函数作为参数传递
- 如何将参数传递给正在使用模板的类
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 修改函数中的指针(将另一个指针作为参数传递)
- 如何将部分流作为参数传递
- 我正在开发服务器,ip作为参数传递不起作用
- 将成员函数指针作为参数传递给模板方法
- 如何在C++中将迭代器作为函数参数传递
- 将附加参数传递给使用 beast::bind_front_handler 调用的函数
- 如何将一个类的函数作为另一个类的另一个函数的参数传递
- 将参数传递为"const"的奇怪效果
- 如何在 c++ 中将函数作为参数传递?
- 在 C++ 中将非指定类型作为参数传递的最佳方法?
- 使用引用与指针将数组作为参数传递
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 将参数打包的参数传递到 std::queue 中,以便稍后使用不同的函数调用
- 为什么我不能将引用作为 std::async 的函数参数传递
- 如何在不使用指针的情况下将派生类的对象作为参数传递给基类中的函数?
- 将AES密文作为参数传递