c++ - 使用 Crypto++ 库的 CBC 模式实现中的 AES128

标签 c++ encryption aes crypto++

在我的输入文件中:
第一行是一个十六进制编码的 key ,长度为 16 字节;
在第二行加密消息(CBC 模式下的 AES128,在加密消息前添加随机 iv)。

这就是我尝试解密的方式:

#include<iostream>
using namespace std;

#include <fstream>
#include <string.h>
#include <cryptopp/aes.h>
#include <cryptopp/modes.h>
#include <cryptopp/filters.h>

using namespace CryptoPP;

int main(void) {
    ifstream in("input0.txt");
    ofstream out("output0.txt");

    string hex_key = "", hex_ct = "";
    in >> hex_key >> hex_ct;

    byte key[ AES::DEFAULT_KEYLENGTH ], iv[ AES::BLOCKSIZE ];
    string ciphertext = "", recoveredtext = "";

    for(int i = 0; i < hex_key.size(); i+=2) {
        key[i/2] = (char) strtol((hex_key.substr(i, 2)).c_str(), 0, 16);
    }

    //then I divide iv from the text
    for(int i = 0; i < AES::BLOCKSIZE*2; i+=2) {
        iv[i/2] = (char) strtol((hex_ct.substr(i, 2)).c_str(), 0, 16);
    }

    for(int i = AES::BLOCKSIZE*2; i < hex_ct.size(); i++) {
        ciphertext.push_back(hex_ct[i]);
    }

    //decryption
    CBC_Mode< AES >::Decryption d;
    d.SetKeyWithIV(key, AES::DEFAULT_KEYLENGTH, iv);

    StringSink sink( recoveredtext );

    StreamTransformationFilter stf (
        d,
        &sink
    );

    StringSource ss (
        ciphertext,
        true,
        &stf
    );

    out << recoveredtext;

    return 0;
}

我在 Wiki 之后使用了这个实现.
我也试过 this 它起作用了,但没有用我的替换 key 和密文。
好吧,使用上面的代码我得到了这个输出:

AES128CBC: /usr/local/include/cryptopp/misc.h:304: void CryptoPP::memcpy_s(void*, size_t, const void*, size_t): Assertion `dest != __null' failed.
Aborted (core dumped)

使用这段代码时:

//decryption

 AES::Decryption aesDecryption(key, AES::DEFAULT_KEYLENGTH);
CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );

StreamTransformationFilter stfDecryptor(
    cbcDecryption,
    new StringSink( recoveredtext ),
    BlockPaddingSchemeDef::NO_PADDING
);

stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext.c_str() ), ciphertext.size() );
stfDecryptor.MessageEnd();

它有效,但输出不是有效的字符序列。

我安装了库:

sudo apt-get install libcrypto++-dev libcrypto++-doc libcrypto++-utils

我编译它:

g++ -o AESCBC128 AESCBC128.cpp -lcryptopp

我找不到问题所在。
预先感谢您的帮助。

输入样本:

140b41b22a29beb4061bda66b6747e14
4ca00ff4c898d61e1edbf1800618fb2828a226d160dad07883d04e008a7897ee2e4b7465d5290d0c0e6c6822236e1daafb94ffe0c5da05d9476be028ad7c1d81


我不知道输出样本,因为这是一个练习,我的目标是发现 secret 消息。
测试输入和转换为字节数组:

out << "KEY:\n" << hex_key << endl;

for(int i = 0; i < AES::DEFAULT_KEYLENGTH; i++) {
    out << setfill('0') << setw(2) << hex << (int)key[i];
}

out << endl << "Received message:\n" << hex_ct << endl;

out << "IV:\n";
for(int i = 0; i < AES::BLOCKSIZE; i++) {
    out << setfill('0') << setw(2) << hex << (int)iv[i];
}

out << endl << "CT:\n" << ciphertext << endl;

结果:

KEY:
140b41b22a29beb4061bda66b6747e14
140b41b22a29beb4061bda66b6747e14
Received message:
4ca00ff4c898d61e1edbf1800618fb2828a226d160dad07883d04e008a7897ee2e4b7465d5290d0c0e6c6822236e1daafb94ffe0c5da05d9476be028ad7c1d81
IV:
4ca00ff4c898d61e1edbf1800618fb28
CT:
28a226d160dad07883d04e008a7897ee2e4b7465d5290d0c0e6c6822236e1daafb94ffe0c5da05d9476be028ad7c1d81

它们符合预期。

最佳答案

出现填充错误的一个可能原因不是填充而是解密错误,提供参数的方式可能不正确。

我在提供的 KEY、IV 和 CT 上编写了解密代码。结果具有 8 字节 0x08 的 PKCS#7 填充。

(正确删除 iv)

删除填充的解密:

42617369 63204342 43206d6f 64652065 6e637279 7074696f 6e206e65 65647320 70616464 696e672e

或在文本中:

Basic CBC mode encryption needs padding.

完整填充的解密(注意填充的尾随 8 个字符):

42617369 63204342 43206d6f 64652065 6e637279 7074696f 6e206e65 65647320 70616464 696e672e 08080808 08080808

输出主要是 ASCII,只有少数异常(exception),例如第 3 个字节 0xfc。

因为填充是正确的,所以我相信这是加密的真实数据。

对于过度好奇的人,这里是我的测试代码:

NSData *key    = [Utilities dataFromHexString:@"140b41b22a29beb4061bda66b6747e14"];
NSData *iv     = [Utilities dataFromHexString:@"4ca00ff4c898d61e1edbf1800618fb28"];
NSData *dataIn = [Utilities dataFromHexString:@"28a226d160dad07883d04e008a7897ee2e4b7465d5290d0c0e6c6822236e1daafb94ffe0c5da05d9476be028ad7c1d81"];

size_t         cryptBytes = 0;
NSMutableData *dataOut    = [NSMutableData dataWithLength:dataIn.length + kCCBlockSizeAES128];

CCCrypt(kCCDecrypt,
        kCCAlgorithmAES,
        kCCOptionPKCS7Padding,
        key.bytes, key.length,
        iv.bytes,
        dataIn.bytes, dataIn.length,
        dataOut.mutableBytes, dataOut.length,
        &cryptBytes);
dataOut.length = cryptBytes;

NSLog(@"dataOut: %@", dataOut);
NSLog(@"dataOut: %@", [[NSString alloc] initWithData:dataOut encoding:NSUTF8StringEncoding]);

关于c++ - 使用 Crypto++ 库的 CBC 模式实现中的 AES128,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36239273/

相关文章:

python 密码库 : what is the best value for "rounds"

php - 如何快速保护身份验证

java - 如何使用已知 IV 解密 AES/CBC

java - AES 与 PBEWithSHA256And256BitAES

python - 如何在 Python(不使用 MedPy)或 C 中将 *.mha 文件转换为 *.nii 文件?

c++ - 如何使用自动变量选择迭代器类型?

c++ - C++类: Where should it be destroyed?的成员对象

php - 保证正确的 Ajax 调用参数值的最佳方式

Java 错误 : Input length must be multiple of 16 when decrypting with padded cipher

c++ - 如何调试.so源?