Simple Crypt IO Device
[toc align_right="yes" depth="3"]
Writing a Custom I/O Device with encryption via SimpleCrypt class
Creating a custom IO device was already described in Writing a Custom I/O Device. The encryption is used from Simple encryption with SimpleCrypt.
The example app can be found on gitorious: "qtdevnet-wiki-mvc/qtdevnet-simplecryptiodevide":https://www.gitorious.org/qtdevnet-wiki-mvc/qtdevnet-simplecryptiodevide .
Usage
The following code snippet shows how we would use the custom I/O device to encrypt data and store the result in a file:
<br /> QFile file&amp;#40;"output.dat&quot;&#41;;<br /> SimpleCryptDevice device(&file); // stream to store the encrypted data
device.setBlockSize(256);<br /> device.setKey(Q_UINT64_C(0x0c2ad4a4acb9f023));<br /> device.setCompressionMode(SimpleCrypt::CompressionAlways);<br /> device.setIntegrityProtectionMode(SimpleCrypt::ProtectionHash);<br /> device.open(QIODevice::WriteOnly);<br /> QTextStream stream(&device);<br /> out << "Hello World&quot;;<br /> out << "My text to encrypt&quot;;<br />
The <code>compressionMode</code> and <code>Integrity Protection</code> can be changed if needed.
Also, if needed, a signal <code>blockWritten</code> can be connected.
Implementation
The basic implementation is the same as in Custom I/O Device. The big difference is, that the data can't be stored directly when the client writes it to the device, as the encryption/decryption is done block wise.
This means <code>readData</code> and <code>writeData</code> must be changed.
<code>SimpleCryptIoDevice</code> has a property blockSize. Data that is written is stored in an internal buffer of size blockSize. When the buffer size is reached, the data is encrypted and stored. This is needed, as <code>SimpleCrypt</code> (in it's used version) does not allow to encrypt to a stream.
Efficiency
Note that because <code>SimpleCrypt</code> uses a header and both the compression and the data protection hash or checksum are calculated and stored at the the block level, using <code>SimpleCryptDevice</code> in this form results in a larger output stream than when using the <code>SimpleCrypt</code> class directly. Perhaps a future version of <code>SimpleCrypt</code> will support a streaming interface to increase efficiency in use cases such as these.
readData
For reading, alway a complete block must be read from the device. Then the needed data is moved to the data buffer of the client. As there might be data left in the buffer, each read furst gets the data of the internal buffer. when it's empty, new data is read from the underlying device.
<br />qint64 SimpleCryptDevice::readData(char* data, qint64 maxSize)<br />{<br /> int bytesRead = 0;<br /> if(!m_byteBuffer.isEmpty())<br /> {<br /> for(int copyByte = 0; copyByte < qMin(m_byteBuffer.size(), (int)maxSize); +''copyByte,''+bytesRead)<br /> data[bytesRead] = m_byteBuffer[copyByte];
m_byteBuffer.remove(0, bytesRead);<br /> }
while(m_byteBuffer.isEmpty() && (bytesRead < maxSize) && !m_underlyingDevice->atEnd())<br /> {<br /> int sizeOfCypher = 0;<br /> int bytesReallyRead = m_underlyingDevice->read((char*)&sizeOfCypher, sizeof(sizeOfCypher));
if(bytesReallyRead != sizeof(sizeOfCypher))<br /> return <s>1;
<br /> QByteArray myCypherText;<br /> myCypherText.resize(sizeOfCypher);<br /> bytesReallyRead = m_underlyingDevice</s>>read(myCypherText.data(), sizeOfCypher);
if(bytesReallyRead != bytesRead)<br /> {<br /> m_byteBuffer = m_crypto.decryptToByteArray(myCypherText);<br /> if (m_crypto.lastError() != SimpleCrypt::ErrorNoError)<br /> {<br /> return <s>1;<br /> }<br /> else<br /> {<br /> int copyByte = 0;<br /> for(copyByte = 0; (copyByte < m_byteBuffer.size()) && (bytesRead < (int)maxSize); +''copyByte,bytesRead)<br /> {<br /> data[bytesRead] = m_byteBuffer[copyByte];<br /> }<br /> m_byteBuffer.remove(0, copyByte);<br /> }<br /> }<br /> }<br /> return bytesRead;<br />}<br />
The stored data always contains an int with the size of the encrypted buffer.
h3. writeData
To write the data to the underlying device, first the current block needs to be filled. To achieve this, all data is attached to the buffer <code>m_byteBuffer</code>. unless the buffer is smaller than the block size, one block is removed of the buffer and stored in the underlying device.
<br />qint64 SimpleCryptDevice::writeData(const char* data, qint64 maxSize)<br />{<br /> m_byteBuffer.append(data, (int)maxSize);<br /> quint64 realBytesWritten = 0;
<br /> // always write blocks of m_blockSize bytes [[Image:|Image:]]!<br /> while(m_byteBuffer.size() > m_blockSize)<br /> {<br /> QByteArray bytesToWrite = m_byteBuffer.left(m_blockSize);<br /> m_byteBuffer.remove(0, m_blockSize);
<br /> realBytesWritten''= writeBlock(bytesToWrite);<br /> }
<br /> emit encryptedBytesWritten(realBytesWritten);
<br /> return maxSize;<br />}<br />
writing one block is fairly easy. The block is encrypted by a call to <code>SimpleCrypt::encryptToByteArray</code> and the size of the encrypted data and the data itself is written to the underlying device.
<br />int SimpleCryptDevice::writeBlock(const QByteArray&amp; bytesToWrite)<br />{<br /> quint64 realBytesWritten = 0;<br /> QByteArray myCypherBytes = m_crypto.encryptToByteArray(bytesToWrite); // cypher the bytes<br /> if (m_crypto.lastError() == SimpleCrypt::ErrorNoError)<br /> {<br /> // store the byte block incl. the size<br /> int sizeOfCypher = myCypherBytes.size();
<br /> realBytesWritten ''= m_underlyingDevice->write((const char*)&sizeOfCypher, sizeof(sizeOfCypher));<br /> realBytesWritten''= m_underlyingDevice</s>>write(myCypherBytes.data(), sizeOfCypher);<br /> emit blockWritten();<br /> return realBytesWritten;<br /> }<br /> return 0;<br />}<br />
To ensure no data is left when the device is closed, during close or destructor, the last buffer is flushed to the device.
<br />void SimpleCryptDevice::flushEnd()<br />{<br /> if(openMode() & WriteOnly)<br /> {<br /> quint64 realBytesWritten = writeBlock(m_byteBuffer);<br /> emit encryptedBytesWritten(realBytesWritten);<br /> }<br />}<br />
That's all.