Encrypt/Decrypt byte array Crypto++

后端 未结 1 581
北海茫月
北海茫月 2021-01-03 12:43

I am trying to encrypt a byte array using AES. I have been able to encrypt strings and files no problem, however byte arrays seem to not be working for me. I pass in a byte

相关标签:
1条回答
  • 2021-01-03 13:05

    Here's the way to do things using ArraySource and ArraySink. The Redirector ensures the ArraySink survives so you can call TotalPutLength.

    #include <iostream>
    #include <string>
    using namespace std;
    
    #include "cryptlib.h"
    #include "filters.h"
    #include "files.h"
    #include "modes.h"
    #include "hex.h"
    #include "aes.h"
    using namespace CryptoPP;
    
    int main(int argc, char* argv[])
    {
      byte key[AES::MAX_KEYLENGTH];
      byte iv[AES::BLOCKSIZE];
      vector<byte> plain, cipher, recover;
      HexEncoder encoder(new FileSink(cout));
    
      memset(key, 0x00, sizeof(key));
      memset(iv, 0x00, sizeof(iv));
    
      string str("Attack at dawn!");
      std::copy(str.begin(), str.end(), std::back_inserter(plain));
    
      cout << "Plain text: ";
      encoder.Put(plain.data(), plain.size());
      encoder.MessageEnd();
      cout << endl;
    
      /////////////////////////////////////////////////////////////
    
      CBC_Mode<AES>::Encryption enc;
      enc.SetKeyWithIV(key, sizeof(key), iv, sizeof(iv));
    
      // Make room for padding
      cipher.resize(plain.size()+AES::BLOCKSIZE);
      ArraySink cs(&cipher[0], cipher.size());
    
      ArraySource(plain.data(), plain.size(), true,
        new StreamTransformationFilter(enc, new Redirector(cs)));
    
      // Set cipher text length now that its known
      cipher.resize(cs.TotalPutLength());
    
      cout << "Cipher text: ";
      encoder.Put(cipher.data(), cipher.size());
      encoder.MessageEnd();
      cout << endl;
    
      /////////////////////////////////////////////////////////////
    
      CBC_Mode<AES>::Decryption dec;
      dec.SetKeyWithIV(key, sizeof(key), iv, sizeof(iv));
    
      // Recovered text will be less than cipher text
      recover.resize(cipher.size());
      ArraySink rs(&recover[0], recover.size());
    
      ArraySource(cipher.data(), cipher.size(), true,
        new StreamTransformationFilter(dec, new Redirector(rs)));
    
      // Set recovered text length now that its known
      recover.resize(rs.TotalPutLength());
    
      cout << "Recovered text: ";
      encoder.Put(recover.data(), recover.size());
      encoder.MessageEnd();
      cout << endl;
    
      return 0;
    }
    

    Running the program results in:

    $ ./test.exe
    Plain text: 41747461636B206174206461776E21
    Cipher text: 85928E5511BFE9E6EE235BCACC4894D4
    Recovered text: 41747461636B206174206461776E21
    

    Here's the example using ByteQueue. The Redirector helps chain the pipeline together because a ByteQueue is a BufferedTransformation.

    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;
    
    #include "cryptlib.h"
    #include "filters.h"
    #include "files.h"
    #include "modes.h"
    #include "queue.h"
    #include "hex.h"
    #include "aes.h"
    using namespace CryptoPP;
    
    int main(int argc, char* argv[])
    {
      byte key[AES::MAX_KEYLENGTH];
      byte iv[AES::BLOCKSIZE];
      HexEncoder encoder(new FileSink(cout));
    
      memset(key, 0x00, sizeof(key));
      memset(iv, 0x00, sizeof(iv));
    
      ByteQueue plain, cipher, recover;
      string str("Attack at dawn!");
      plain.Put(reinterpret_cast<const byte*>(str.data()), str.size());
    
      cout << "Plain text: ";
      plain.CopyTo(encoder);
      encoder.MessageEnd();
      cout << endl;
    
      /////////////////////////////////////////////////////////////
    
      CBC_Mode<AES>::Encryption enc;
      enc.SetKeyWithIV(key, sizeof(key), iv, sizeof(iv));
    
      StreamTransformationFilter f1(enc, new Redirector(cipher));
      plain.CopyTo(f1);
      f1.MessageEnd();
    
      cout << "Cipher text: ";
      cipher.CopyTo(encoder);
      encoder.MessageEnd();
      cout << endl;
    
      /////////////////////////////////////////////////////////////
    
      CBC_Mode<AES>::Decryption dec;
      dec.SetKeyWithIV(key, sizeof(key), iv, sizeof(iv));
    
      StreamTransformationFilter f2(dec, new Redirector(recover));
      cipher.CopyTo(f2);
      f2.MessageEnd();
    
      cout << "Recovered text: ";
      recover.CopyTo(encoder);
      encoder.MessageEnd();
      cout << endl;
    
      return 0;
    }
    

    It also results in:

    skylake:cryptopp$ ./test.exe
    Plain text: 41747461636B206174206461776E21
    Cipher text: 85928E5511BFE9E6EE235BCACC4894D4
    Recovered text: 41747461636B206174206461776E21
    
    0 讨论(0)
提交回复
热议问题