Qt-AES/README.md

136 lines
4.3 KiB
Markdown
Raw Permalink Normal View History

2017-06-06 17:15:23 -07:00
# Qt-AES
2017-07-06 13:49:28 -07:00
Small and portable AES encryption class for Qt.
2020-05-15 16:58:38 -07:00
Native support for all key sizes - 128/192/256 bits - ECB, CBC, CFB and OFB modes
AES-NI support for all key sizes - ECB, CBC modes
2017-06-23 14:43:59 -07:00
## Usage
2017-07-03 11:08:18 -07:00
2017-07-07 14:20:06 -07:00
### Available Methods
```
2018-04-05 16:15:32 -07:00
// Encode of rawText with key
// iv is used in CBC mode
// return the encrypted byte array
QByteArray encode(const QByteArray rawText, const QByteArray key, const QByteArray iv = QByteArray());
2017-07-07 14:20:06 -07:00
2018-04-05 16:15:32 -07:00
// Decode of rawText with key
// iv is used in CBC mode
// return the decrypted byte array
QByteArray decode(const QByteArray rawText, const QByteArray key, const QByteArray iv = QByteArray());
2017-07-07 14:20:06 -07:00
2018-04-05 16:15:32 -07:00
// Key expansion in Rijndael schedule
// return the new expanded key as byte array
2017-07-07 14:20:06 -07:00
QByteArray expandKey(const QByteArray key);
```
The same methods are available as static calls
```
QAESEncryption::Crypt => encode(...)
QAESEncryption::Decrypt => decode(...)
QAESEncryption::ExpandKey => expandKey(...)
```
2018-04-05 16:15:32 -07:00
#### AES Levels
The class supports all AES key lenghts
* AES_128
* AES_192
* AES_256
2018-04-05 16:13:03 -07:00
#### Modes
The class supports the following operating modes
* ECB
* CBC
* CFB
* OFB
2017-07-07 14:20:06 -07:00
2018-03-28 16:03:51 -07:00
#### Padding
2018-03-28 17:50:41 -07:00
By default the padding method is `ISO`, however, the class supports:
2018-04-05 16:13:03 -07:00
* ZERO
* PKCS7
* ISO
2018-03-28 16:03:30 -07:00
2017-07-07 14:20:06 -07:00
### Example
Sample code using a 128bit key in ECB mode
2017-07-06 14:30:08 -07:00
```
#include "qaesencryption.h"
2018-04-05 16:16:11 -07:00
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
QByteArray encodedText = encryption.encode(plainText, key);
2017-07-06 14:30:08 -07:00
2018-04-05 16:16:11 -07:00
QByteArray decodedText = encryption.decode(encodedText, key);
2017-07-06 14:30:08 -07:00
```
Example for 256bit CBC using QString
```
#include <QCryptographicHash>
#include "qaesencryption.h"
2018-04-05 16:16:11 -07:00
QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::CBC);
2017-07-06 14:30:08 -07:00
2018-04-05 16:16:11 -07:00
QString inputStr("The Advanced Encryption Standard (AES), also known by its original name Rijndael "
"is a specification for the encryption of electronic data established by the U.S. "
"National Institute of Standards and Technology (NIST) in 2001");
QString key("your-string-key");
QString iv("your-IV-vector");
2017-07-06 14:30:08 -07:00
2018-04-05 16:16:11 -07:00
QByteArray hashKey = QCryptographicHash::hash(key.toLocal8Bit(), QCryptographicHash::Sha256);
QByteArray hashIV = QCryptographicHash::hash(iv.toLocal8Bit(), QCryptographicHash::Md5);
2017-07-03 11:08:18 -07:00
2018-04-05 16:16:11 -07:00
QByteArray encodeText = encryption.encode(inputStr.toLocal8Bit(), hashKey, hashIV);
QByteArray decodeText = encryption.decode(encodeText, hashKey, hashIV);
2018-03-28 17:50:41 -07:00
2018-04-05 16:16:11 -07:00
QString decodedString = QString(encryption.removePadding(decodeText));
2018-03-28 17:50:41 -07:00
2018-04-05 16:16:11 -07:00
//decodedString == inputStr !!
2017-07-03 11:08:18 -07:00
```
2017-07-07 14:20:06 -07:00
### Example via static invocation
Static invocation without creating instances, 256 bit key, ECB mode, starting from *QString* text/key
2017-07-06 14:30:08 -07:00
```
#include <QCryptographicHash>
#include "qaesencryption.h"
2018-04-05 16:16:11 -07:00
QString inputStr("The Advanced Encryption Standard (AES), also known by its original name Rijndael "
"is a specification for the encryption of electronic data established by the U.S. "
"National Institute of Standards and Technology (NIST) in 2001");
QString key("your-string-key");
QString iv("your-IV-vector");
QByteArray hashKey = QCryptographicHash::hash(key.toLocal8Bit(), QCryptographicHash::Sha256);
QByteArray hashIV = QCryptographicHash::hash(iv.toLocal8Bit(), QCryptographicHash::Md5);
//Static invocation
QByteArray encrypted = QAESEncryption::Crypt(QAESEncryption::AES_256, QAESEncryption::CBC,
inputStr.toLocal8Bit(), hashKey, hashIV);
//...
// Removal of Padding via Static function
QString decodedString = QString(QAESEncryption::RemovePadding(decodeText));
2017-07-06 14:30:08 -07:00
```
2020-05-15 16:58:38 -07:00
## AES New Instructions Set
To use the hardware acceleration provided by the AES New Instructions Set, define USE_INTEL_AES_IF_AVAILABLE
If the CPU supports it, the code will switch to use AESNI automatically.
The feature is enabled by default
2017-07-07 10:08:29 -07:00
## Unit Testing
2017-07-07 13:24:57 -07:00
The unit testing vectors used are included in [NIST-Recommendation for Block Cipher Modes of Operation](http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf)
2017-07-07 10:08:29 -07:00
2017-07-13 09:07:19 -07:00
Please note that this code is not audited or AES-certified by any competent authority, use it at your own risk.
2017-07-10 14:12:09 -07:00
## Dependencies
* qtcore
No OpenSSL required.
2017-07-06 14:30:08 -07:00
## Contact
Question or suggestions are welcome!
Please use the GitHub issue tracking to report suggestions or issues.
2017-07-10 14:12:09 -07:00
## License
2017-07-06 14:30:08 -07:00
This software is provided under the [UNLICENSE](http://unlicense.org/)
2018-10-18 08:21:05 -07:00
## Known Issues
Please take a look at the list of currently open issues