【问题标题】:AES128 in CBC mode implementation using Crypto++ library使用 Crypto++ 库在 CBC 模式下实现 AES128
【发布时间】:2016-07-14 08:25:00
【问题描述】:

在我的输入文件中:
第一行是一个以十六进制编码且长度为 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 并且它有效,但没有用我的替换密钥和密文。
好吧,使用上面的代码我有这个输出:

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


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

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

它们符合预期。

【问题讨论】:

  • 调试代码。您忘记了样本数据和输入/utpot。最好以十六进制打印密钥、iv、加密数据、预期的解密数据。他们是正确的吗?使用简短的输入数据进行测试。是否使用 PKCS#7 填充进行加密,解密需要 PKCS#7 填充。
  • 我用输入和一些测试编辑了问题,它们是正确的。关于padding,解密是否受pad影响?抱歉,这是我第一次使用cryptopp,这也是我在stackoverflow 中的第一个问题。
  • 好的,在 StreamTransformationFilter 构造函数中添加块填充方案参数(NO_PADDING),第二个代码有效,但输出是无意义的字符序列。我确定输入,出了什么问题?
  • 如果解密指定了填充,则会注意到填充不正确。见PKCS7。错误可能来自不正确的密钥、加密期间的不同填充(mcrypt 我在看你)或加密期间没有填充。一些实现不会报告填充错误,因为它真的没用,并且这样的错误不应以任何形式传播回调用者以避免填充 oracle 附加。
  • 假设消息的长度是 40:那么我将在原始明文后面附加一个 8 字节的填充“8”。那么填充作为明文的一部分被解密,对吗?然后最后我必须从恢复的文本中删除一些字节,这些字节在恢复的文本的最后一个字节中描述。我对吗?如果是,那么使用“无填充”作为填充方案,我应该正确解密附加到明文的填充,这样我就可以删除它并只获得秘密消息。正确的?如果是,可能是输入不正确吗?或者我该如何解决这个填充问题?

标签: c++ encryption aes crypto++


【解决方案1】:

您收到填充错误的可能原因不是填充,而是解密错误,提供参数的方式可能不正确。

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

(正确删除iv)

删除填充的解密:

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

或在文本中:

基本 CBC 模式加密需要填充。

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

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

输出主要是 ASCII,有少数例外,例如第 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]);

【讨论】:

  • 我将您的结果从十六进制解码为 ASCII,删除了 IV(16 个字节),结果是正确答案,请问您是如何对解密进行编码的?或者更好的是我的代码哪里出错了?
  • 更正答案,我选择了包含 iv 的版本。
  • 我使用不同的语言和库(Common Crypto),所以我的代码无济于事。基本上提供正确的输入,你就会得到正确的输出,它只是一个函数。我的猜测是您没有以正确的格式提供输入。我使用的库只是基于数据的,所以几乎没有猜测。您收到填充错误的原因不是填充,而是解密错误,提供参数的方式。
  • 调试提示(仅限):在解密时设置无填充,您将能够看到填充,PKCS#7 填充很容易识别。
  • 是的,谢谢,问题出在 ct 输入中:我没有将它从 hex 转换为 ascii。现在一切正常。
【解决方案2】:

是的,问题出在输入中:我没有将密文从 hex 转换为 ascii。

所以解决方法是使用这个:

for(int i = AES::BLOCKSIZE*2; i < hex_ct.size(); i+=2) {
        ciphertext.push_back((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]);
    }

IV 和 PAD 会自动移除。

所以解决方案代码是:

#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);
    }

    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+=2) {
        ciphertext.push_back((char) strtol((hex_ct.substr(i, 2)).c_str(), 0, 16));
    }

    //decryption

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

    StreamTransformationFilter stfDecryptor(
        cbcDecryption,
        new StringSink( recoveredtext )
    );

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

    out << recoveredtext;

    return 0;
}

【讨论】:

  • 这个解决方案给了我:Basic CBC mode encryption needs padding.
猜你喜欢
  • 1970-01-01
  • 2013-04-06
  • 1970-01-01
  • 2011-12-10
  • 2018-04-01
  • 2014-01-27
  • 2020-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多