Invalid PKCS #7 block padding when using Crypto++

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 15:23:31

问题


We are using cryptopp library. We are using the below coding. Encryption is working file without any issue and we are able to get the cipher text. But getting an error while decrypting as "Block padding found". What could be the issue...?

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

Converting string to 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;
}

Encryption Block:

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

Library:

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;

}

来源:https://stackoverflow.com/questions/47474980/invalid-pkcs-7-block-padding-when-using-crypto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!