AES128在CBC模式下实现使用Crypto ++库 [英] AES128 in CBC mode implementation using Crypto++ library

查看:420
本文介绍了AES128在CBC模式下实现使用Crypto ++库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在输入文件中,我有:

在第一行一个以十六进制编码,长度为16字节的密钥;

在第二行加密消息(AES128在CBC模式下,随机添加一个加密消息)。

b
$ b

这是我如何解密:

 #include< iostream> 
使用命名空间std;

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

使用命名空间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 =,restoredtext =; (int i = 0; i< hex_key.size(); i + = 2){
key [i / 2] =(char)strtol((hex_key.substr i,2))。c_str(),0,16);
}

//然后我将文本从
中分开(int i = 0; i< AES :: BLOCKSIZE * 2; i + = 2){
iv [i / 2] =(char)strtol((hex_ct.substr(i,2))。c_str(),0,16); (int i = AES :: BLOCKSIZE * 2; i< hex_ct.size(); i ++){
ciphertext.push_back(hex_ct [i])
}

;
}

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

StringSink sink(deletedtext);

StreamTransformationFilter stf(
d,
& sink
);

StringSource ss(
ciphertext,
true,
& stf
);

out<< recoveredtext;

return 0;
}

我在维基

我也尝试过,而且它有效,但是没有用我的替换密钥和密文。

嗯,使用上面的代码我有这个输出:

  AES128CBC:/usr/local/include/cryptopp/misc.h:304:void CryptoPP :: memcpy_s(void *, size_t,const void *,size_t):断言`dest!= __null'失败。 
中止(核心转储)

使用此代码时:

  //解密

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

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

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

它的工作原理,但输出不是有效的字符序列。



我安装了lib:

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

我编译它:

  g ++ -o AESCBC128 AESCBC128.cpp -lcryptopp 

我找不到什么问题。

提前感谢帮助。


输入示例:

  140b41b22a29beb4061bda66b6747e14 
4ca00ff4c898d61e1edbf1800618fb2828a226d160dad07883d04e008a7897ee2e4b7465d5290d0c0e6c6822236e1daafb94ffe0c5da05d9476be028ad7c1d81



我不知道输出示例,因为这是一个练习,我的目标是发现秘密消息。

测试输入和转换为字节数组:

  out<< KEY:\\\
<< hex_key<< ENDL; (int i = 0; i< AES :: DEFAULT_KEYLENGTH; i ++)

{
out< setfill('0')<< setw(2)< hex< (INT)键[I];
}

out<< endl< Received message:\\\
<< hex_ct<< ENDL;

out<< IV:\\\
; (int i = 0; i< AES :: BLOCKSIZE; i ++)
{
out< setfill('0')<< setw(2)< hex< (int)的四[I];
}

out<< endl< CT:\\\
<<密文<< ENDL;

结果:

 code> KEY:
140b41b22a29beb4061bda66b6747e14
140b41b22a29beb4061bda66b6747e14
接收消息:
4ca00ff4c898d61e1edbf1800618fb2828a226d160dad07883d04e008a7897ee2e4b7465d5290d0c0e6c6822236e1daafb94ffe0c5da05d9476be028ad7c1d81
IV:
4ca00ff4c898d61e1edbf1800618fb28
CT:
28a226d160dad07883d04e008a7897ee2e4b7465d5290d0c0e6c6822236e1daafb94ffe0c5da05d9476be028ad7c1d81

正如预期的那样。

解决方案

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



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



(正确删除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 dataFro mHexString:@ 140b41b22a29beb4061bda66b6747e14]; 
NSData * iv = [实用程序dataFromHexString:@4ca00ff4c898d61e1edbf1800618fb28];
NSData * dataIn = [实用程序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]);


In the input file I have:
on the first line a key which is encoded in hex and with length of 16 bytes;
on the second line encrypted message ( AES128 in CBC mode , with a random iv prepended to the encrypted message).

This is how I tried to decrypt:

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

I used this implementation following the Wiki.
I also tried with this and it worked, but not replacing key and ciphertext with my.
Well, using the code above I have this output:

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)

While using this code:

//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();

It works but the output isn't a valid sequence of characters.

I installed the lib with:

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

and I compiled it with:

g++ -o AESCBC128 AESCBC128.cpp -lcryptopp

I can't find what's wrong.
Thanks in advance for the help.

Sample of input:

140b41b22a29beb4061bda66b6747e14
4ca00ff4c898d61e1edbf1800618fb2828a226d160dad07883d04e008a7897ee2e4b7465d5290d0c0e6c6822236e1daafb94ffe0c5da05d9476be028ad7c1d81


I don't know a sample of output because this is an exercise and my goal is to discover the secret message.
Test on the inputs and on conversion to byte arrays:

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;

Result:

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

They are as expected.

解决方案

A probable reason you are getting a padding error is not the padding but that the decryption is wrong, the way the parameters are supplied is probably incorrect.

I coded up decryption on the supplied KEY, IV and CT. The result has PKCS#7 padding of 8 bytes of 0x08.

(correct to remove iv)

Decryption with padding removed:

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

or in text:

Basic CBC mode encryption needs padding.

Decryption with padding intact (note the trailing 8 characters of padding):

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

The output is mostly ASCII with a few exceptions such as the 3rd byte 0xfc.

Because the padding is correct I believe this is the true data that was encrypted.

For the overly curious here is my test code:

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]);

这篇关于AES128在CBC模式下实现使用Crypto ++库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆