AES加密中的PKCS7填充C++代码

3
我需要一个使用aes-cbc256和填充方式PKCS7的字符串加密示例(在C++中,我正在使用linux-Ubuntu)。请帮忙。
以下代码如何将IV设置为0并将密钥值设置为字符串值? 我还想添加pkcs7 padding。我在Linux中使用crypto++库。
// Driver.cpp   
//      

#include "stdafx.h"    
#include "cryptopp/dll.h"    
#include "cryptopp/default.h"    
#include "crypto++/osrng.h"    
using CryptoPP::AutoSeededRandomPool;    

#include <iostream>    
using std::cout;    
using std::cerr;       

#include <string>    
using std::string;       

#include "crypto++/cryptlib.h"    
using CryptoPP::Exception;        

#include "crypto++/hex.h"    
using CryptoPP::HexEncoder;    
using CryptoPP::HexDecoder;        

#include "crypto++/filters.h"    
using CryptoPP::StringSink;    
using CryptoPP::StringSource;    
using CryptoPP::StreamTransformationFilter;        

#include "crypto++/aes.h"    
using CryptoPP::AES;       

#include "crypto++/ccm.h"    
using CryptoPP::CBC_Mode;       

#include "assert.h"        

int main(int argc, char* argv[])    
{    
    AutoSeededRandomPool prng;        

    byte key[ AES::DEFAULT_KEYLENGTH ];    
    prng.GenerateBlock( key, sizeof(key) );        

    byte iv[ AES::BLOCKSIZE];    
    iv[AES::BLOCKSIZE] = 0;    
    //prng.GenerateBlock(iv,  sizeof(iv) );        

    string plain = "CBC Mode Test";    
    string cipher, encoded, recovered;       

    // Pretty print key    
    encoded.clear();    
    StringSource( key, sizeof(key), true,    
                  new HexEncoder(new StringSink( encoded )) // HexEncoder    
    ); // StringSource

    cout << "key: " << encoded << endl;        

    // Pretty print iv    
    encoded.clear();

    StringSource( iv, sizeof(iv), true,    
        new HexEncoder(new StringSink( encoded )) // HexEncoder    
    ); // StringSource

    cout << "iv: " << encoded << endl;       

    /*********************************\
    \*********************************/

    try    
    {    
        cout << "plain text: " << plain << endl;            
        CBC_Mode< AES >::Encryption e;    
        e.SetKeyWithIV( key, sizeof(key), iv );     

        // The StreamTransformationFilter adds padding    
        //  as required. ECB and CBC Mode must be padded    
        //  to the block size of the cipher.    
        StringSource( plain, true,     
            new StreamTransformationFilter( e,    
                new StringSink( cipher )    
            ) // StreamTransformationFilter          
        ); // StringSource    
    }    
    catch( CryptoPP::Exception& e )    
    {    
        cerr << "Caught Exception..." << endl;    
        cerr << e.what() << endl;    
        cerr << endl;    
    }    

    /*********************************\    
    \*********************************/    

    // Pretty print    
    encoded.clear();    
    StringSource( cipher, true,    
        new HexEncoder(    
            new StringSink( encoded )    
        ) // HexEncoder    
    ); // StringSource    
    cout << "cipher text: " << encoded << endl;    

    /*********************************\    
    \*********************************/    

    try    
    {    
        CBC_Mode< AES >::Decryption d;    
        d.SetKeyWithIV( key, sizeof(key), iv );    

        // The StreamTransformationFilter removes    
        //  padding as required.    
        StringSource s( cipher, true,     
            new StreamTransformationFilter( d,    
                new StringSink( recovered )    
            ) // StreamTransformationFilter    
        ); // StringSource    

        cout << "recovered text: " << recovered << endl;    
    }    
    catch( CryptoPP::Exception& e )    
    {    
        cerr << "Caught Exception..." << endl;    
        cerr << e.what() << endl;    
        cerr << endl;    
    }    

    /*********************************\    
    \*********************************/    

    assert( plain == recovered );    

    return 0;    
}

“byte” 是 “unsigned char”的意思。 “AES :: DEFAULT_KEYLENGTH” 通常为16。因此,您有一个长度为16的无符号字符数组。只需将字符串复制到其中,将“char”转换为“unsigned char”,并使用已知值(例如0)填充其余的“key”数组即可。这是关于密钥的内容。 - davka
关于iv,你是指“全0”吗?如果是的话,只需用0或其他你喜欢的值来填充它。 - davka
那么AES是128位的。你的意思是我必须用key[aes::default_keylenght]='hello0000000000'替换prng.GenerateBlock(key, sizeof(key));吗?如果不是,你能否请写出正确的代码?我会非常感激:)那填充呢?它是PKCS7吗?我在哪里可以更改或查看该值?非常感谢! - she
还有一个问题。我如何打印键值?cout<<"key: "<<key<<endl; 打印出来的键值完全不同: - she
2个回答

10

OpenSSL默认使用PKCS7填充。这种填充意味着当您的数据不是块大小的倍数时,您需要用值n填充n个字节,其中n是您需要达到块大小所需的字节数。AES的块大小为16。

以下是使用OpenSSL和AES256-cbc加密字符串的示例。OpenSSL文档还有示例,尽管它们使用不同的密码。此示例不进行错误检查。

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <cassert>

#include <openssl/evp.h>

int main()
{
    // ctx holds the state of the encryption algorithm so that it doesn't
    // reset back to its initial state while encrypting more than 1 block.
    EVP_CIPHER_CTX ctx;
    EVP_CIPHER_CTX_init(&ctx);

    unsigned char key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
                   0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
                   0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f};
    unsigned char iv[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    assert(sizeof(key) == 32);  // AES256 key size
    assert(sizeof(iv) == 16);   // IV is always the AES block size

    // If data isn't a multiple of 16, the default behavior is to pad with
    // n bytes of value n, where n is the number of padding bytes required
    // to make data a multiple of the block size.  This is PKCS7 padding.
    // The output then will be a multiple of the block size.
    std::string plain("encrypt me");
    std::vector<unsigned char> encrypted;
    size_t max_output_len = plain.length() + 16 - (plain.length() % 16);
    encrypted.resize(max_output_len);

    // Enc is 1 to encrypt, 0 to decrypt, or -1 (see documentation).
    EVP_CipherInit_ex(&ctx, EVP_aes_256_cbc(), NULL, key, iv, 1);

    // EVP_CipherUpdate can encrypt all your data at once, or you can do
    // small chunks at a time.
    int actual_size = 0;
    EVP_CipherUpdate(&ctx,
             &encrypted[0], &actual_size,
             reinterpret_cast<unsigned char *>(&plain[0]), plain.size());

    // EVP_CipherFinal_ex is what applies the padding.  If your data is
    // a multiple of the block size, you'll get an extra AES block filled
    // with nothing but padding.
    int final_size;
    EVP_CipherFinal_ex(&ctx, &encrypted[actual_size], &final_size);
    actual_size += final_size;

    encrypted.resize(actual_size);

    for( size_t index = 0; index < encrypted.size(); ++index )
    {
        std::cout << std::hex << std::setw(2) << std::setfill('0') <<
            static_cast<unsigned int>(encrypted[index]);
    }
    std::cout << "\n";

    EVP_CIPHER_CTX_cleanup(&ctx);

    return 0;
}

将其命名为 encrypt.cpp 并使用以下命令进行编译:

g++ encrypt.cpp -o encrypt -lcrypto -lssl -Wall

您将会得到以下输出:
338d2a9e28208cad84c457eb9bd91c81

您可以通过从命令提示符运行OpenSSL命令行实用程序来验证正确性:

$ echo -n "encrypt me" > to_encrypt
$ openssl enc -in to_encrypt -out encrypted -e -aes-256-cbc \
-K 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f \
-iv 00000000000000000000000000000000
$ hexdump -C encrypted

这个十六进制转储将显示与C++程序相同的字节。

00000000  33 8d 2a 9e 28 20 8c ad  84 c4 57 eb 9b d9 1c 81  |3.*.( ....W.....|

我该如何解密编码字符串?同时,我还需要在加密后添加base64。我该如何使用openssl进行第二个选项:base64?谢谢:)...!很抱歉问这么愚蠢的问题,但对我来说,在这个领域里所有的东西都是新的:)...而且例子太少了:(。再次感谢!!:) - she
代码是一样的,我只需要更改这一行:EVP_CipherInit_ex(&ctx,EVP_aes_256_cbc(),NULL,key,iv,1); 改成0而不是1,对吧? - she
在我的代码中,密钥是一个名为hello的密码。我遇到了以下错误:int main():断言“sizeof(key)== 32”失败。已中止。 - she
@she: 是的,我认为你只需要在EVP_CipherInit_ex中将1改为0即可解密。至于断言,AES256密钥是32个字节。在此EVP_Cipher_* API中必须为32个字节。有一个OpenSSL API可以使用密码生成AES256密钥,但我不确定它是什么。我知道有这样的API,因为您可以使用命令行实用程序执行此操作:openssl aes-256-cbc -iv 00000000000000000000000000000000 -e -in plain_text_file -out encrypted_file,然后它会要求输入密码。也许可以发布另一个问题询问如何执行此操作。 - indiv
你的意思不是说 size_t max_output_len = plain.length() + 16 - (plain.length() % 16) 吗? - velcrow
显示剩余5条评论

1

还可以看看我对这个问题的回答。

我建议查看cryptopp。这里有一个代码示例:

CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption encryptor;
byte* key;
size_t keylen;
// ... acquire key

encryptor.SetKey( key, keylen );

std::string input;
std::string result;
// read input ...

StringSource( input, true,
       new StreamTransformationFilter( encryptor, new StringSink( result ),
     StreamTransformationFilter::PKCS_PADDING));

StreamTransformationFilter 中填充模式的取值可以是:

BlockPaddingScheme { 
  NO_PADDING, ZEROS_PADDING, PKCS_PADDING, ONE_AND_ZEROS_PADDING, 
  DEFAULT_PADDING 
}

编辑:在示例中替换填充模式为pkcs


非常感谢。如果您能给我一个链接,其中提供了使用aes、cbc和pkcs填充对字符串进行编码的C++代码示例,我将不胜感激。谢谢...:D - she
很不幸,当我输入命令make install安装crypto++库时出现了错误。我将尝试使用openssl。无论如何,非常感谢:)。在openssl中,我找到了命令:EVP_CIPHER_CTX_set_padding()。我将尝试阅读并使用它。祝你度过愉快的假期! - she
你用过 OpenSSL 吗?我测试了在网上找到的一个例子,但它没有正常工作。如果你有一个能够正确运行的代码,请发给我链接。谢谢。 - she
我尝试运行这里的示例1:http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=21877。当我写g++ sample1.cpp -o sample1 -lcrypto时,它会弹出许多错误,例如:undefined reference to `CryptoPP::BlockTransformation::AdvancedProcessBlocks(unsigned char const*, unsigned char const*, unsigned char*, unsigned int, unsigned int) const' /tmp/ccHGNwCg.o:(.rodata._ZTVN8CryptoPP13AlgorithmImplINS_25SimpleKeyingInterfaceImplINS_8TwoBasesINS_11BlockCipherENS_13Rijndael_InfoEEES5_EES6_EE[vtab;e... - she
@she:你成功安装了cryptopp吗?我认为你的编译器找不到cryptopp的包含文件。你需要将其添加到命令选项中,例如g++ --I/usr/include/cryptopp ...,或者在代码中更改#include指令为#include <cryptopp/aes.h>等(假设它安装在/usr/include下)。此外,我认为-l选项应该是-lcryptopp。并验证libcryptopp.so是否位于编译器的默认库搜索路径中,否则您需要通过-L选项将其定向到那里。顺便说一句,好链接。祝你好运! - davka
显示剩余25条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接