hello, everyone,
I am trying to write some codes to encrypt and decrypt one small string plaintext using symmetric algorithms.
the crypto++ version is 4.1, the following is my source codes.
for encryption, I can get some result, but for decryption, always get nothing, seems wrong code.
after I get hex result from encryption, I use the hex encryption result as input to decrypt.
thanks
 
 
 
encryption:
 
char INPUTBUF[BUFSIZE],OUTPUTBUF[BUFSIZE];
 
     byte key[16], iv[16];
     HexEncoder hexEncoder;
     AutoSeededRandomPool rng;
     rng.GenerateBlock(key, 16);  
     rng.GenerateBlock(iv, 16);
     AESEncryption aesEncryption(key, 16);
    CBCPaddedEncryptor cbcEncryptor(aesEncryption, iv, new HexEncoder);
    cbcEncryptor.Put((byte*)INPUTBUF, (unsigned int) strlen(INPUTBUF));
    cbcEncryptor.MessageEnd();
 
    unsigned int outputLength = cbcEncryptor.MaxRetrieveable();
    char *outstr = new char[outputLength+1];
    cbcEncryptor.Get((byte *)outstr, outputLength);
    outstr[outputLength] = '\0';
    strcpy((char*)OUTPUTBUF,(char*)outstr);
    delete[] outstr;
 
 
 
decryption:
 
 
 byte key[16], iv[16];
 AutoSeededRandomPool rng;
  rng.GenerateBlock(key, 16);
  rng.GenerateBlock(iv, 16);
 
  AESDecryption aesDecryption(key, 16);
  CBCPaddedDecryptor cbcDecryptor(aesDecryption, iv, new HexDecoder);
  cbcDecryptor.Put((byte *)INPUTBUF, (unsigned int) strlen(INPUTBUF));
  cbcDecryptor.MessageEnd();
 
  unsigned int outputLength =cbcDecryptor.MaxRetrieveable();
(//note: the outputLength is always 0, do not know why???)
  char *outstr = new char[outputLength+1];
   cbcDecryptor.Get((byte *)outstr, outputLength);
  outstr[outputLength] = '\0';
   strcpy((char*)OUTPUTBUF,(char*)outstr);
  delete[] outstr;

Reply via email to