使用Crypto ++时,无效的PKCS#7块填充

时间:2017-11-24 14:07:24

标签: c++ encryption crypto++ pkcs#7

我们正在使用cryptopp库。我们正在使用以下编码。加密是工作文件没有任何问题,我们能够获得密文。但是在解密时收到错误"发现了块填充"。可能是什么问题......?

#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"
#include "osrng.h"
using namespace CryptoPP;
using CryptoPP::AutoSeededRandomPool;

class cspl_crypto{

public:
  cspl_crypto();
  byte* generate_block(int size);
  char* encrypt_rijndael(byte[], byte[], int, char*, int);
  char* decrypt_rijndael(byte[], byte[], int, char*, int);
  string readFile();
  void writeFile(string);
};

cspl_crypto::cspl_crypto()
{

}

int main(int argc, char* argv[])
{
  vector<byte> plain;
  cspl_crypto ccrypto;
  AutoSeededRandomPool prng;

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

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

将字符串转换为char *

  string str("testing"); //ccrypto.readFile()
char plainArray[str.size()];
strcpy(plainArray, str.c_str());

char* cipher  = ccrypto.encrypt_rijndael(key, iv, sizeof(key), plainArray, 
sizeof(plainArray));

//char cipherCharArray[cipherText.size()];
// strcpy(cipherCharArray, cipherText.c_str());

char* recover = ccrypto.decrypt_rijndael(key, iv, sizeof(key), cipher, 
sizeof(cipher));

// cout << "Recovered text: " << recoverText << endl;

  return 0;
}

加密阻止:

char* cspl_crypto::encrypt_rijndael(byte key[], byte iv[], int keysize, char 
plainText[], int plainTextSize){
    vector<byte> cipher;

       std::vector<byte> plain(plainText, plainText + plainTextSize);

    CBC_Mode<AES>::Encryption enc;
enc.SetKeyWithIV(key, keysize, iv, keysize);

// 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());
    char returnValue[cipher.size()];
    copy(cipher.begin(), cipher.end(), returnValue);

    return returnValue;
}

Decyption Block:

char* cspl_crypto::decrypt_rijndael(byte key[], byte iv[], int keysize, char 
cipher[], int size ){
 std::vector<byte> v(cipher, cipher + size);
 vector<byte> recover;
  CBC_Mode<AES>::Decryption dec;
  dec.SetKeyWithIV(key, keysize, iv, keysize);

  // Recovered text will be less than cipher text
  recover.resize(v.size());
  ArraySink rs(&recover[0], recover.size());

  ArraySource(v.data(), v.size(), true,
    new StreamTransformationFilter(dec, new Redirector(rs)));

  // Set recovered text length now that its known
  recover.resize(rs.TotalPutLength());

        char returnValue[recover.size()];
        copy(recover.begin(), recover.end(), returnValue);

    return returnValue;
}

库:

string cspl_crypto::readFile(){

  string line;
  string returnValue = "";
  ifstream myfile ("N07.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      returnValue +=  line + '\n';
    }
    myfile.close();
  }

  else returnValue = "Unable to open file"; 

return returnValue;

}

0 个答案:

没有答案