- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 CBC 模式和 Crypto++ 库中使用 AES 加密(并在之后解密)
这是我已经做过的:
using namespace CryptoPP;
AutoSeededRandomPool rnd;
//generating the key and iv
SecByteBlock key(AES::MAX_KEYLENGTH);
rnd.GenerateBlock(key, key.size());
byte iv[AES::BLOCKSIZE];
rnd.GenerateBlock(iv, AES::BLOCKSIZE);
为了加密文件,我以二进制模式打开它,并将内容转储为字符串:
std::ifstream fin(file_path, std::ios::binary);
if (!fin)
{
std::cout << "error";
}
std::ostringstream ostrm;
ostrm << fin.rdbuf();
std::string plaintext(ostrm.str());
fin.close();
然后,我使用之前生成的 key 和 iv 加密此字符串:
std::string ciphertext;
AES::Encryption aesEncryption(key, CryptoPP::AES::MAX_KEYLENGTH);
CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv);
StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink(ciphertext));
stfEncryptor.Put(reinterpret_cast<const unsigned char*>(plaintext.c_str()), plaintext.length() + 1);
stfEncryptor.MessageEnd();
现在,我想将加密后的字符串写入一个文件,并用它存储 IV,因为 iv 不需要保密,最好在密文的开头或结尾
问题来了:IV是一个字节数组,密文是一个字符串,我需要将两者之一转换为另一种类型,还是我可以这样做:
std::ofstream fdout(file_path2, std::ios::binary);
if (!fdout)
{
std::cout << "error";
}
fdout << iv;
fdout << ciphertext;
fdout.close();
当我尝试解密这个文件时,如何分别提取 iv 和密文? IV 有 16 个字节长,但在这里我完全迷路了,我不知道该怎么做。
最佳答案
Storing the IV with the ciphertext Crypto++ CBC AES encryption
您使用的某些代码对我来说有点不寻常。我将挑出一些内容并向您展示一些 Crypto++ 方法。
开始之前,请先查看 Pipelines和 Pumping Data在 Crypto++ 维基上。请记住,数据从源流向接收器。在数据之间遇到转换数据的过滤器。
std::ifstream fin(file_path, std::ios::binary);
if (!fin)
{
std::cout << "error";
}
std::ostringstream ostrm;
ostrm << fin.rdbuf();
std::string plaintext(ostrm.str());
fin.close();
Crypto++ FileSource
有一个采用 std::istream
的构造函数。您可以执行以下操作。另见 FileSource
在 Crypto++ wiki 上。
std::ifstream fin(file_path, std::ios::binary);
FileSource source(fin, true /*pump all*/, NULLPTR);
...
AES::Encryption aesEncryption(key, CryptoPP::AES::MAX_KEYLENGTH);
CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv);
ExternalCipher
用于 FIPS DLL。您可以在没有 DLL 的情况下使用它们,但它们存在于 DLL 中。通常你使用:
CBC_Mode<AES>::Encryption encryptor;
此外,您通常希望避免仅保密 模式。通常你想使用 Authenticated Encryption操作模式。它提供 secret 性和真实性。
Crypto++ 提供 CCM、EAX 和 GCM 认证加密操作模式。 OCB和EAX都是非常好的选择。 EAX 模式记录在 EAX Mode在 Crypto++ 维基上。 OCB 目前不可用。我们正在准备入住 OCB 模式。
Now,i want to write the encrypted string to a file,and store the IV with it,since the iv doesn't need to be kept secret,ideally at the beginning or the end of the ciphertext
使用类似下面的内容。我没有编译它,所以你需要修正拼写错误。
AutoSeededRandomPool prng;
SecByteBlock key(AES::MAXIMUM_KEYLENGTH), iv(AES::BLOCKSIZE);
RandomNumberSource rs1(prng, AES::MAXIMUM_KEYLENGTH, new ArraySink(key, key.size()));
RandomNumberSource rs2(prng, AES::BLOCKSIZE, new ArraySink(iv, iv.size()));
HexEncoder encoder(new FileSink(std::cout));
std::cout << "Key: ";
encoder.Put(key, key.size());
encoder.MessageEnd();
std::cout << std::endl;
std::cout << "IV: ";
encoder.Put(iv, iv.size());
encoder.MessageEnd();
std::cout << std::endl;
EAX<AES>::Encryption encryptor;
encryptor.SetKeyWithIV(key, key.size(), iv, iv.size());
// Plaintext message
std::string message;
// Output file
FileSink file("message.enc");
// Source wrappers
ArraySource as(iv, iv.size(), true,
new Redirector(file));
// Source wrapper
StringSource ss(message, true,
new StreamTransformationFilter(encryptor,
new Redirector(file)));
When i will try to decrypt this file,how can i extract the iv and ciphertext separately ?
使用如下内容。
// Key is from previous example. It cannot change
SecByteBlock key(AES::MAXIMUM_KEYLENGTH), iv(AES::BLOCKSIZE);
FileSource fs("message.enc", false /* DO NOT Pump All */);
// Attach new filter
ArraySink as(iv, iv.size());
fs.Attach(new Redirector(as));
fs.Pump(AES::BLOCKSIZE); // Pump first 16 bytes
EAX<AES>::Decryption decryptor;
decryptor.SetKeyWithIV(key, key.size(), iv, iv.size());
// Detach previously attached filter, attach new filter
ByteQueue queue;
fs.Detach(new StreamTransformationFilter(decryptor, new Redirector(queue)));
fs.PumpAll(); // Pump remainder of bytes
加密后的数据将在 ByteQueue
中。它不提供类似 C++ 迭代器的功能,如指针和大小。要从 ByteQueue
中获取数据,您可以将其传输或复制到另一个过滤器或接收器:
SecByteBlock block(queue.MaxRetrievable());
ArraySink sink(block, block.size());
queue.TransferTo(sink);
您可以从 ByteQueue
中获取数据并将其放入 std::string
中:
std::string recovered;
StringSink sink(recovered);
queue.TransferTo(sink);
您可以打印从文件中恢复的 IV:
HexEncoder encoder(new FileSink(std::cout));
std::cout << "IV: ";
encoder.Put(iv, iv.size());
encoder.MessageEnd();
std::cout << std::endl;
关于c++ - 使用密文 Crypto++ CBC AES 加密存储 IV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45461770/
题目地址:https://leetcode-cn.com/problems/course-schedule-iv/ 题目描述 你总共需要上 n 门课,课程编号依次为 0 到 n-1 。 有的课会
我即将使用以下脚本来加密和解密一些数据。我正在使用它,因为我当前的加密在我们的新服务器上不起作用。我们目前正在使用 mcrypt,所以我想更改为 openssl。 在我们的数据库中,我们使用 aes
有人可以告诉我为什么会收到此错误消息吗?显然这是我缺少的某种转换 预期 IV 长度为 16 但实际为 24 我用它来调用它 String encrypted = "E5ADDEB05D9D7B3925
这是我用来加密/解密数据的代码: // Set the method $method = 'AES-128-CBC'; // Set the encryption key $encryption_ke
1.题目 给定一个整数数组 prices ,它的第 i 个元素 prices[i] 是一支给定的股票在第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。 注意:你
已关闭。这个问题是 not about programming or software development 。目前不接受答案。 这个问题似乎不是关于 a specific programming
在 AES 中,我的理解是盐是使密码更安全的东西,它不会被添加到加密文本中。但是IV是用来加密第一个消息 block 的东西,将被添加到加密文本中。 我有什么问题吗? 最佳答案 AES 本身并不直接使
我需要编写一个简化的加密 API,它可以通过使用随机生成的 key 或密码派生的 key 轻松处理对称加密。 密码生成是使用 OpenSSL 库中的 PKCS5_PBKDF2_HMAC() 函数并使用
我正在 Java 中使用 AES/CTR/NoPadding 使用恒定 key 和随机 IV 来加密我的消息。 我计划将 IV 添加到密文前面,然后对其进行 Base64 编码。 现在有一个问题困扰着
如果在加密明文时使用随机IV,那么密文的接收者如何知道IV是什么以对其进行解密? 这是对上一个关于 IVs here. 的 stackoverflow 问题的回复的后续问题 The IV allows
我“到处”都找过了,但找不到。是否有示例说明如何使用 c++ Quantlib 来对具有合成行使价/到期日的期权价格进行插值? 例如,如果“今天”是 2017 年 2 月 6 日,我得到了 2017
当我尝试为 TripleDES 加密器创建 IV 初始化向量时出现以下错误。 请看代码示例: TripleDESCryptoServiceProvider tripDES = new TripleDE
我无法理解以下句子:“初始化向量 (IV) 中的数字全为零(不是 ASCII 字符‘0’)。 我的目标是使用 openssl enc 命令使用 aes-128-cbc 和 key K(比如 12345
我正在阅读有关如何对 key 加盐以确保加密安全的教程,但没能充分理解它。我对密码学了解不多,需要一些帮助。我正在使用 commoncrypto 来加密文件,并且已经完成,除了它不安全这一事实....
我正在尝试创建dBase文件,因为似乎我的Export vba代码中的.TransferDatabase()方法要求该文件存在(请继续获取“字段将不适合记录”。假设由于文件不存在)。 我在网上使用vb
是否建议我使用 initialization vector加密/解密我的数据?它会让事情变得更安全吗?这是需要根据具体情况进行评估的事情之一吗? 为了将其放入实际上下文中,Win32 加密函数 Cry
我可以知道一个 R 包和代码,用于通过可用的诊断测试(例如,弱仪器测试、外生性测试(使用 Wu-Hausman)、Sargan 测试)运行固定效应工具变量 (IV) 回归吗? 我知道 plm 代码提供
我可以知道一个 R 包和代码,用于通过可用的诊断测试(例如,弱仪器测试、外生性测试(使用 Wu-Hausman)、Sargan 测试)运行固定效应工具变量 (IV) 回归吗? 我知道 plm 代码提供
我试图在加密后但写入之前将一个 16 字节安全生成的 IV 附加到每个文件上。这样我以后就可以通过从文件中拉出 IV 来解密它。这是我到目前为止的代码: public static void encr
了解 PKCS5_PBKDF2_HMAC()需要一个salt 并返回一个derivedKey 还有那个GCM::Encryption.SetKeyWithIV()需要 iv(以及 derivedKey
我是一名优秀的程序员,十分优秀!