Hello, I'm having some AES encryption/decryption problems. The issue comes
into play with certian IV's.

My IV's are completely randomized for security, however, I'm not sure If I'm
loading them in/out correctly.

I get the: StreamTransformationFilter: ciphertext length is not a multiple
of block size error.


CODE:

int xxz568::AESEncrypt(std::string key, std::string input, std::string
&output) {
   byte keyB[32], ivB[AES::BLOCKSIZE];
   std::string hashKey = sha1(key).substr(0, 32);
   memcpy(keyB, hashKey.c_str(), 32); 
   //
   memcpy(ivB, generateRandLen(), 16);
   //
   std::string hold, final, hexVec, decVec;
   HexEncode((const char *)ivB, hexVec);
   cout << "IV: " << ivB << " | " << hexVec << endl; 
   output = hexVec.substr(0, 32);
   //convert output back to binary for usage as the initVector
   HexDecode(output, decVec);
   //store dec Vec in the iv slot
   memcpy(ivB, decVec.c_str(), 16);
   StringSink* sink = new StringSink(hold);
   CBC_Mode<AES>::Encryption aes(keyB, sizeof(keyB), ivB);
   StreamTransformationFilter* aes_enc = new StreamTransformationFilter(aes,
sink);
   StringSource cipher(input, true, aes_enc);
   //convert to a hash
   HexEncode(hold, final);
   output += final;
   //
   return 1;
}

int xxz568::AESDecrypt(std::string key, std::string input, std::string
&output) {
   std::string bin, store2, init, test;
   HexDecode(input, bin);
   cout << "DEC: " << bin << endl;
   try{
      byte keyB[32], iv[AES::BLOCKSIZE];
      std::string hashKey = sha1(key).substr(0, 32);
      memcpy(keyB, hashKey.c_str(), 32); 
          //obtain IV
          init.assign(bin.substr(0, 16));
          memcpy(iv, init.c_str(), 16);
          HexEncode(init, test);
          store2.assign(bin.substr(16, bin.length()));
          cout << "IV: " << init << " | " << test << endl; 
          //
      StringSink* sink = new StringSink(output);
      CBC_Mode<AES>::Decryption aes(keyB, sizeof(keyB), iv);
      StreamTransformationFilter* aes_enc = new
StreamTransformationFilter(aes, sink);
      StringSource cipher(store2, true, aes_enc);
   }
   catch(CryptoPP::Exception e) {
      cout << e.GetWhat() << endl;
          output = "Decrypt Error:";
          output.append(e.GetWhat());
   }
   return 1;
}

byte * xxz568::generateRandLen() {
   AutoSeededRandomPool rng;
   byte randLen[16];
   rng.GenerateBlock(randLen, 16);
   //
   return randLen;
}

int xxz568::HexEncode(std::string input, std::string &output) {
   CryptoPP::StringSource foo(input, true,
          new CryptoPP::HexEncoder(
         new CryptoPP::StringSink(output), false));
   return 1;
}

int xxz568::HexDecode(std::string input, std::string &output) {
   CryptoPP::StringSource foo(input, true,
          new CryptoPP::HexDecoder(
         new CryptoPP::StringSink(output)));
   return 1;
}
-- 
View this message in context: 
http://old.nabble.com/AES-Problem-tp31186698p31186698.html
Sent from the Crypto++ Users mailing list archive at Nabble.com.

-- 
You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to [email protected].
More information about Crypto++ and this group is available at 
http://www.cryptopp.com.

Reply via email to