Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 + '
';
    }
    myfile.close();
  }

  else returnValue = "Unable to open file"; 

return returnValue;

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
518 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...