Crypto++ AES加密

#include "stdafx.h"
#include 
#include       // StreamTransformationFilter
#include     // CFB_Mode
#include    // std:cerr  
#include    // std::stringstream  
#include 
 
 
using namespace std;
using namespace CryptoPP;
#pragma comment( lib, "cryptlib.lib" )
 
std::string ECB_AESEncryptStr(std::string sKey, const char *plainText)
{
    std::string outstr;
      
    //填key  
    SecByteBlock key(AES::MAX_KEYLENGTH);  
    memset(key,0x30,key.size() );  
    sKey.size()<=AES::MAX_KEYLENGTH?memcpy(key,sKey.c_str(),sKey.size()):memcpy(key,sKey.c_str(),AES::MAX_KEYLENGTH);  
          
      
    AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);  
      
    ECB_Mode_ExternalCipher::Encryption ecbEncryption(aesEncryption);
    StreamTransformationFilter ecbEncryptor(ecbEncryption, new HexEncoder(new StringSink(outstr)));  
    ecbEncryptor.Put((byte *)plainText, strlen(plainText));  
    ecbEncryptor.MessageEnd();  
      
    return outstr;  
}  
 
 
 
std::string ECB_AESDecryptStr(std::string sKey, const char *cipherText)  
{  
    std::string outstr;  
      
    //填key  
    SecByteBlock key(AES::MAX_KEYLENGTH);  
    memset(key,0x30,key.size() );  
    sKey.size()<=AES::MAX_KEYLENGTH?memcpy(key,sKey.c_str(),sKey.size()):memcpy(key,sKey.c_str(),AES::MAX_KEYLENGTH);  
          
    ECB_Mode::Decryption ecbDecryption((byte *)key, AES::MAX_KEYLENGTH);  
          
    HexDecoder decryptor(new StreamTransformationFilter(ecbDecryption, new StringSink(outstr)));  
    decryptor.Put((byte *)cipherText, strlen(cipherText));  
    decryptor.MessageEnd();  
      
    return outstr;  
}  
 
 
 
std::string CBC_AESEncryptStr(std::string sKey, std::string sIV, const char *plainText)
{
    std::string outstr;
      
    //填key  
    SecByteBlock key(AES::MAX_KEYLENGTH);  
    memset(key,0x30,key.size() );  
    sKey.size()<=AES::MAX_KEYLENGTH?memcpy(key,sKey.c_str(),sKey.size()):memcpy(key,sKey.c_str(),AES::MAX_KEYLENGTH);  
          
    //填iv  
    byte iv[AES::BLOCKSIZE];  
    memset(iv,0x30,AES::BLOCKSIZE); 
sIV.size()<=AES::BLOCKSIZE?memcpy(iv,sIV.c_str(),sIV.size()):memcpy(iv,sIV.c_str(),AES::BLOCKSIZE);
      
    AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);  
      
    CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv);  
      
    StreamTransformationFilter cbcEncryptor(cbcEncryption, new HexEncoder(new StringSink(outstr)));  
    cbcEncryptor.Put((byte *)plainText, strlen(plainText));  
    cbcEncryptor.MessageEnd();  
      
    return outstr;  
}  
 
std::string CBC_AESDecryptStr(std::string sKey, std::string sIV, const char *cipherText)  
{  
    std::string outstr;  
      
    //填key  
    SecByteBlock key(AES::MAX_KEYLENGTH);  
    memset(key,0x30,key.size() );  
    sKey.size()<=AES::MAX_KEYLENGTH?memcpy(key,sKey.c_str(),sKey.size()):memcpy(key,sKey.c_str(),AES::MAX_KEYLENGTH);  
          
    //填iv  
    byte iv[AES::BLOCKSIZE];  
    memset(iv,0x30,AES::BLOCKSIZE);  
sIV.size()<=AES::BLOCKSIZE?memcpy(iv,sIV.c_str(),sIV.size()):memcpy(iv,sIV.c_str(),AES::BLOCKSIZE);
 
 
    CBC_Mode::Decryption cbcDecryption((byte *)key, AES::MAX_KEYLENGTH, iv);  
          
    HexDecoder decryptor(new StreamTransformationFilter(cbcDecryption, new StringSink(outstr)));  
    decryptor.Put((byte *)cipherText, strlen(cipherText));  
    decryptor.MessageEnd();  
      
    return outstr;  
}  
 
std::string CBC_CTS_AESEncryptStr(std::string sKey, std::string sIV, const char *plainText)
{
    std::string outstr;
      
    //填key  
    SecByteBlock key(AES::MAX_KEYLENGTH);  
    memset(key,0x30,key.size() );  
    sKey.size()<=AES::MAX_KEYLENGTH?memcpy(key,sKey.c_str(),sKey.size()):memcpy(key,sKey.c_str(),AES::MAX_KEYLENGTH);  
          
    //填iv  
    byte iv[AES::BLOCKSIZE];  
    memset(iv,0x30,AES::BLOCKSIZE); 
sIV.size()<=AES::BLOCKSIZE?memcpy(iv,sIV.c_str(),sIV.size()):memcpy(iv,sIV.c_str(),AES::BLOCKSIZE);
      
    AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);  
      
    CBC_CTS_Mode_ExternalCipher::Encryption cbcctsEncryption(aesEncryption, iv);  
      
    StreamTransformationFilter cbcctsEncryptor(cbcctsEncryption, new HexEncoder(new StringSink(outstr)));  
    cbcctsEncryptor.Put((byte *)plainText, strlen(plainText));  
    cbcctsEncryptor.MessageEnd();  
      
    return outstr;  
}  
 
 
 
 
std::string CBC_CTS_AESDecryptStr(std::string sKey, std::string sIV, const char *cipherText)  
{  
    std::string outstr;  
      
    //填key  
    SecByteBlock key(AES::MAX_KEYLENGTH);  
    memset(key,0x30,key.size() );  
    sKey.size()<=AES::MAX_KEYLENGTH?memcpy(key,sKey.c_str(),sKey.size()):memcpy(key,sKey.c_str(),AES::MAX_KEYLENGTH);  
          
    //填iv  
    byte iv[AES::BLOCKSIZE];  
    memset(iv,0x30,AES::BLOCKSIZE);  
sIV.size()<=AES::BLOCKSIZE?memcpy(iv,sIV.c_str(),sIV.size()):memcpy(iv,sIV.c_str(),AES::BLOCKSIZE);
 
 
    CBC_CTS_Mode::Decryption cbcctsDecryption((byte *)key, AES::MAX_KEYLENGTH, iv);  
          
    HexDecoder decryptor(new StreamTransformationFilter(cbcctsDecryption, new StringSink(outstr)));  
    decryptor.Put((byte *)cipherText, strlen(cipherText));  
    decryptor.MessageEnd();  
      
    return outstr;  
}  
 
 
 
 
 
 
 
 
 
 
std::string CFB_AESEncryptStr(std::string sKey, std::string sIV, const char *plainText)
{
    std::string outstr;
      
    //填key  
    SecByteBlock key(AES::MAX_KEYLENGTH);  
    memset(key,0x30,key.size() );  
    sKey.size()<=AES::MAX_KEYLENGTH?memcpy(key,sKey.c_str(),sKey.size()):memcpy(key,sKey.c_str(),AES::MAX_KEYLENGTH);  
          
    //填iv  
    byte iv[AES::BLOCKSIZE];  
    memset(iv,0x30,AES::BLOCKSIZE); 
sIV.size()<=AES::BLOCKSIZE?memcpy(iv,sIV.c_str(),sIV.size()):memcpy(iv,sIV.c_str(),AES::BLOCKSIZE);
      
    AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);  
      
    CFB_Mode_ExternalCipher::Encryption cfbEncryption(aesEncryption, iv);  
      
    StreamTransformationFilter cfbEncryptor(cfbEncryption, new HexEncoder(new StringSink(outstr)));  
    cfbEncryptor.Put((byte *)plainText, strlen(plainText));  
    cfbEncryptor.MessageEnd();  
      
    return outstr;  
}  
 
 
 
 
std::string CFB_AESDecryptStr(std::string sKey, std::string sIV, const char *cipherText)  
{  
    std::string outstr;  
      
    //填key  
    SecByteBlock key(AES::MAX_KEYLENGTH);  
    memset(key,0x30,key.size() );  
    sKey.size()<=AES::MAX_KEYLENGTH?memcpy(key,sKey.c_str(),sKey.size()):memcpy(key,sKey.c_str(),AES::MAX_KEYLENGTH);  
          
    //填iv  
    byte iv[AES::BLOCKSIZE];  
    memset(iv,0x30,AES::BLOCKSIZE);  
sIV.size()<=AES::BLOCKSIZE?memcpy(iv,sIV.c_str(),sIV.size()):memcpy(iv,sIV.c_str(),AES::BLOCKSIZE);
 
 
    CFB_Mode::Decryption cfbDecryption((byte *)key, AES::MAX_KEYLENGTH, iv);  
          
    HexDecoder decryptor(new StreamTransformationFilter(cfbDecryption, new StringSink(outstr)));  
    decryptor.Put((byte *)cipherText, strlen(cipherText));  
    decryptor.MessageEnd();  
      
    return outstr;  
}  
 
 
 
 
std::string OFB_AESEncryptStr(std::string sKey, std::string sIV, const char *plainText)
{
    std::string outstr;
      
    //填key  
    SecByteBlock key(AES::MAX_KEYLENGTH);  
    memset(key,0x30,key.size() );  
    sKey.size()<=AES::MAX_KEYLENGTH?memcpy(key,sKey.c_str(),sKey.size()):memcpy(key,sKey.c_str(),AES::MAX_KEYLENGTH);  
          
    //填iv  
    byte iv[AES::BLOCKSIZE];  
    memset(iv,0x30,AES::BLOCKSIZE); 
sIV.size()<=AES::BLOCKSIZE?memcpy(iv,sIV.c_str(),sIV.size()):memcpy(iv,sIV.c_str(),AES::BLOCKSIZE);
      
    AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);  
      
    OFB_Mode_ExternalCipher::Encryption ofbEncryption(aesEncryption, iv);  
      
    StreamTransformationFilter ofbEncryptor(ofbEncryption, new HexEncoder(new StringSink(outstr)));  
    ofbEncryptor.Put((byte *)plainText, strlen(plainText));  
    ofbEncryptor.MessageEnd();  
      
    return outstr;  
}  
 
 
 
 
std::string OFB_AESDecryptStr(std::string sKey, std::string sIV, const char *cipherText)  
{  
    std::string outstr;  
      
    //填key  
    SecByteBlock key(AES::MAX_KEYLENGTH);  
    memset(key,0x30,key.size() );  
    sKey.size()<=AES::MAX_KEYLENGTH?memcpy(key,sKey.c_str(),sKey.size()):memcpy(key,sKey.c_str(),AES::MAX_KEYLENGTH);  
          
    //填iv  
    byte iv[AES::BLOCKSIZE];  
    memset(iv,0x30,AES::BLOCKSIZE);  
sIV.size()<=AES::BLOCKSIZE?memcpy(iv,sIV.c_str(),sIV.size()):memcpy(iv,sIV.c_str(),AES::BLOCKSIZE);
 
 
    OFB_Mode::Decryption ofbDecryption((byte *)key, AES::MAX_KEYLENGTH, iv);  
          
    HexDecoder decryptor(new StreamTransformationFilter(ofbDecryption, new StringSink(outstr)));  
    decryptor.Put((byte *)cipherText, strlen(cipherText));  
    decryptor.MessageEnd();  
      
    return outstr;  
}  
 
 
std::string CTR_AESEncryptStr(std::string sKey, std::string sIV, const char *plainText)
{
    std::string outstr;
      
    //填key  
    SecByteBlock key(AES::MAX_KEYLENGTH);  
    memset(key,0x30,key.size() );  
    sKey.size()<=AES::MAX_KEYLENGTH?memcpy(key,sKey.c_str(),sKey.size()):memcpy(key,sKey.c_str(),AES::MAX_KEYLENGTH);  
          
    //填iv  
    byte iv[AES::BLOCKSIZE];  
    memset(iv,0x30,AES::BLOCKSIZE); 
sIV.size()<=AES::BLOCKSIZE?memcpy(iv,sIV.c_str(),sIV.size()):memcpy(iv,sIV.c_str(),AES::BLOCKSIZE);
      
    AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);  
      
    CTR_Mode_ExternalCipher::Encryption ctrEncryption(aesEncryption, iv);  
      
    StreamTransformationFilter ctrEncryptor(ctrEncryption, new HexEncoder(new StringSink(outstr)));  
    ctrEncryptor.Put((byte *)plainText, strlen(plainText));  
    ctrEncryptor.MessageEnd();  
      
    return outstr;  
}  
 
 
 
 
std::string CTR_AESDecryptStr(std::string sKey, std::string sIV, const char *cipherText)  
{  
    std::string outstr;  
      
    //填key  
    SecByteBlock key(AES::MAX_KEYLENGTH);  
    memset(key,0x30,key.size() );  
    sKey.size()<=AES::MAX_KEYLENGTH?memcpy(key,sKey.c_str(),sKey.size()):memcpy(key,sKey.c_str(),AES::MAX_KEYLENGTH);  
          
    //填iv  
    byte iv[AES::BLOCKSIZE];  
    memset(iv,0x30,AES::BLOCKSIZE);  
sIV.size()<=AES::BLOCKSIZE?memcpy(iv,sIV.c_str(),sIV.size()):memcpy(iv,sIV.c_str(),AES::BLOCKSIZE);
 
 
    CTR_Mode::Decryption ctrDecryption((byte *)key, AES::MAX_KEYLENGTH, iv);  
          
    HexDecoder decryptor(new StreamTransformationFilter(ctrDecryption, new StringSink(outstr)));  
    decryptor.Put((byte *)cipherText, strlen(cipherText));  
    decryptor.MessageEnd();  
      
    return outstr;  
}  
 
 
int _tmain(int argc, _TCHAR* argv[])
{
string plainText = "This Program shows how to use ECB, CBC, CBC_CTS, CFB, OFB and CTR mode of AES in Crypto++.";
string aesKey = "0123456789ABCDEF0123456789ABCDEF";//256bits, also can be 128 bits or 192bits
string aesIV = "ABCDEF0123456789";//128 bits
string ECB_EncryptedText,ECB_DecryptedText,
CBC_EncryptedText,CBC_DecryptedText,
CBC_CTS_EncryptedText,CBC_CTS_DecryptedText,
CFB_EncryptedText,CFB_DecryptedText,
OFB_EncryptedText,OFB_DecryptedText,
CTR_EncryptedText,CTR_DecryptedText;
 
 
//ECB
ECB_EncryptedText = ECB_AESEncryptStr(aesKey, plainText.c_str());//ECB加密
ECB_DecryptedText = ECB_AESDecryptStr(aesKey,ECB_EncryptedText.c_str());//ECB解密
 
 
//CBC
CBC_EncryptedText = CBC_AESEncryptStr(aesKey, aesIV, plainText.c_str());//CBC加密
CBC_DecryptedText = CBC_AESDecryptStr(aesKey, aesIV, CBC_EncryptedText.c_str());//CBC解密
 
 
//CBC_CTS
CBC_CTS_EncryptedText = CBC_CTS_AESEncryptStr(aesKey, aesIV, plainText.c_str());//CBC_CTS加密
CBC_CTS_DecryptedText = CBC_CTS_AESDecryptStr(aesKey, aesIV, CBC_CTS_EncryptedText.c_str());//CBC_CTS解密
 
//CFB
CFB_EncryptedText = CFB_AESEncryptStr(aesKey, aesIV, plainText.c_str());//CFB加密
CFB_DecryptedText = CFB_AESDecryptStr(aesKey, aesIV, CFB_EncryptedText.c_str());//CFB解密
 
 
//OFB
OFB_EncryptedText = OFB_AESEncryptStr(aesKey, aesIV, plainText.c_str());//OFB加密
OFB_DecryptedText = OFB_AESDecryptStr(aesKey, aesIV, OFB_EncryptedText.c_str());//OFB解密
 
 
//CTR
CTR_EncryptedText = CTR_AESEncryptStr(aesKey, aesIV, plainText.c_str());//CTR加密
CTR_DecryptedText = CTR_AESDecryptStr(aesKey, aesIV, CTR_EncryptedText.c_str());//CTR解密
 
 
 
 
cout << "Crypto++ AES-256 加密测试"<< endl;
cout << "分别使用ECB,CBC, CBC_CTR,CFB,OFB和CTR模式"<< endl;
cout << "加密用密钥:" << aesKey << endl;
cout << "密钥长度:" << AES::MAX_KEYLENGTH*8 <<"bits" << endl;
cout << "IV:" << aesIV << endl;
cout << endl;
 
 
cout << "ECB测试"<< endl;
cout << "原文:" << plainText << endl;
cout << "密文:" << ECB_EncryptedText << endl;
cout << "恢复明文:" << ECB_DecryptedText << endl << endl;
 
cout << "CBC测试"<< endl;
cout << "原文:" << plainText << endl;
cout << "密文:" << CBC_EncryptedText << endl;
cout << "恢复明文:" << CBC_DecryptedText << endl << endl;
 
 
cout << "CBC_CTS测试"<< endl;
cout << "原文:" << plainText << endl;
cout << "密文:" << CBC_CTS_EncryptedText << endl;
cout << "恢复明文:" << CBC_CTS_DecryptedText << endl << endl;
 
 
cout << "CFB测试"<< endl;
cout << "原文:" << plainText << endl;
cout << "密文:" << CFB_EncryptedText << endl;
cout << "恢复明文:" << CFB_DecryptedText << endl << endl;
 
cout << "OFB测试"<< endl;
cout << "原文:" << plainText << endl;
cout << "密文:" << OFB_EncryptedText << endl;
cout << "恢复明文:" << OFB_DecryptedText << endl << endl;
 
 
cout << "CTR测试"<< endl;
cout << "原文:" << plainText << endl;
cout << "密文:" << CTR_EncryptedText << endl;
cout << "恢复明文:" << CTR_DecryptedText << endl << endl;
 
getchar();
return 0;
}

https://blog.csdn.net/happy423/article/details/47321741

 

有一个问题,crypto ,我个人使用这个库做CBC模式的AES加密解密的时候碰见问题,最后弃用,不管是上面的CBC 还是下面的CBC,在加密的时候没有问题,解密,在release64位模式下,会报填充快异常,最终也没有解决这个异常,异常在crypto库中

 

// encrypt
std::string Encrypt(const std::string &strText)
{
    std::string strCipher;
    CBC_Mode::Encryption aesEncryptor(m_arrByteKey, m_nKeyLen, m_arrByteIv);
    StringSource(strText, true, new StreamTransformationFilter(aesEncryptor, new StringSink(strCipher)));
    
    std::string strEncoded;
    StringSource s2(strCipher, true,
        new HexEncoder(
            new StringSink(strEncoded)
        ) // HexEncoder
    ); // StringSource
    return strEncoded;
}

// decrypt
std::string Decrypt(const std::string &strCipher)
{
    std::string strDecoded;
    StringSource s2(strCipher, true,
        new HexDecoder(
            new StringSink(strDecoded)
        ) // HexEncoder
    ); // StringSource

    std::string strText;
    CBC_Mode::Decryption aesEncryptor(m_arrByteKey, m_nKeyLen, m_arrByteIv);
    StringSource(strDecoded, true, new StreamTransformationFilter(aesEncryptor, new StringSink(strText)));
    
    return strText;
}

https://www.cnblogs.com/lit10050528/p/4335120.html

 

可以使用下面的例子做异常捕获

https://www.cryptopp.com/wiki/Cbc_mode

AutoSeededRandomPool prng;

SecByteBlock key(AES::DEFAULT_KEYLENGTH);
prng.GenerateBlock( key, key.size() );

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

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

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

try
{
    cout << "plain text: " << plain << endl;

    CBC_Mode< AES >::Encryption e;
    e.SetKeyWithIV( key, key.size(), iv );

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

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

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

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

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

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

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

可以在解密的catch处断点收到异常

Error : StreamTransformationFilter: invalid PKCS #7 block padding found

如下也有人碰过到

https://stackoverflow.com/questions/18945622/aes-decryption-and-invalid-pkcs-7-block-padding

https://stackoverflow.com/questions/34762869/streamtransformationfilter-invalid-pkcs-7-block-padding-found-in-aes-decryptio

到最后我也没有有效的解决这个问题,如果有大神解决了,可以赐教下

你可能感兴趣的:(Crypto++ AES加密)