我使用 Crypto++ 库进行 arc4 加密。来自此处但未完全解释的引用:http://www.cryptopp.com/wiki/Stream_Cipher .
以下是我的代码:
string key = "key";
string msg = "hello";
ARC4 arc4((byte*)key.c_str(), sizeof((byte*)key.c_str()));
arc4.ProcessData((byte*)msg.c_str(), (byte*)msg.c_str(), sizeof((byte*)msg.c_str()));
arc4.ProcessData((byte*)msg.c_str(), (byte*)msg.c_str(), sizeof((byte*)msg.c_str()));
cout << msg << endl;
加密和解密后我的消息完全是垃圾,我无法阅读。 简而言之,没有解密回“你好”。
那么如何使用上面的 key 加密和解密消息呢?
最佳答案
两个问题。首先,您需要使用字符串的 size()
,而不是 sizeof()
。其次,解密时需要重新设置arc4
对象。否则,密码的状态从之前的加密继续。
string key = "key";
string msg = "hello";
ARC4 arc4((byte*)key.data(), key.size());
arc4.ProcessData((byte*)msg.data(), (byte*)msg.data(), msg.size());
// Reset state
arc4.SetKey((byte*)key.data(), key.size());
arc4.ProcessData((byte*)msg.data(), (byte*)msg.data(), msg.size());
cout << msg << endl;
关于c++ - 如何使用arc4输入字符串进行加密?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26957973/