c++ - 如何使用arc4输入字符串进行加密?

标签 c++ cryptography encryption-symmetric crypto++ rc4-cipher

我使用 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/

相关文章:

c++ - Qt - 清理和重建后的 "This does not seem to be a debug build"

php - 是否可以在 ECB 模式下将 AES 与 IV 一起使用?

java - Java对称加密程序中 key 大小无效

.net - 解密字符串 : System. Security.Cryptography.CryptographicException 时偶尔出现错误数据错误

ios - CCCrypt 处理文件片段 AES CBC : garbage to end of files on encryption-decryption cycle

C++,三元运算符操作数评估规则

c++ - 奇怪 - mysql 的 sql::SQLException 未被其类型捕获,而是被捕获为 std::exception 并成功投回

c++ - sizeof(*p) 我的结果是否未定义?

java - 将 Jython 与 Java 集成(导入错误 : No module named Crypto)

security - 提高 MD5 哈希安全性的有效方法是什么?