OutputAESEncodeStream::OutputAESEncodeStream(IByteWriterWithPosition* inTargetStream, const ByteList& inEncryptionKey, bool inOwnsStream) { mTargetStream = inTargetStream; mOwnsStream = inOwnsStream; if (!mTargetStream) return; mInIndex = mIn; // convert inEncryptionKey to internal rep and init encrypt [let's hope its 16...] mEncryptionKey = new unsigned char[inEncryptionKey.size()]; mEncryptionKeyLength = inEncryptionKey.size(); ByteList::const_iterator it = inEncryptionKey.begin(); size_t i = 0; for (; it != inEncryptionKey.end(); ++i, ++it) mEncryptionKey[i] = *it; mEncrypt.key(mEncryptionKey, mEncryptionKeyLength); // create IV and write it to output file [use existing PDFDate] MD5Generator md5; // encode current time PDFDate currentTime; currentTime.SetToCurrentTime(); md5.Accumulate(currentTime.ToString()); memcpy(mIV, (const unsigned char*)md5.ToStringAsString().c_str(),AES_BLOCK_SIZE); // md5 should give us the desired 16 bytes // now write mIV to the output stream mTargetStream->Write(mIV, AES_BLOCK_SIZE); }
void InputAESDecodeStream::Assign(IByteReader* inSourceReader, const ByteList& inKey) { mSourceStream = inSourceReader; // convert inEncryptionKey to internal rep and init decrypt [let's hope its 16...] mKeyLength = inKey.size(); mKey = new unsigned char[mKeyLength]; ByteList::const_iterator it = inKey.begin(); size_t i = 0; for (; it != inKey.end(); ++i, ++it) mKey[i] = *it; mDecrypt.key(mKey, mKeyLength); mIsIvInit = false; // first read flag. still need to read IV mReadBlockSize = AES_BLOCK_SIZE; mOutIndex = mOut + mReadBlockSize; mHitEnd = false; }