1

我正在使用 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;

我加密和解密后的消息完全是垃圾,然后我无法阅读。简而言之,没有解密回“你好”。

那么如何使用上述密钥加密和解密消息?

4

1 回答 1

1

两个问题。首先,您需要使用字符串的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;
于 2014-11-17T01:45:46.857 回答