OpenLexocad
27.0
|
Simple encryption and decryption of strings and byte arrays. More...
#include <simplecrypt.h>
Public Types | |
enum | CompressionMode { CompressionAuto, CompressionAlways, CompressionNever } |
enum | IntegrityProtectionMode { ProtectionNone, ProtectionChecksum, ProtectionHash } |
enum | Error { ErrorNoError, ErrorNoKeySet, ErrorUnknownVersion, ErrorIntegrityFailed } |
enum | CryptoFlag { CryptoFlagNone = 0, CryptoFlagCompression = 0x01, CryptoFlagChecksum = 0x02, CryptoFlagHash = 0x04 } |
Public Member Functions | |
SimpleCrypt () | |
SimpleCrypt (quint64 key) | |
void | setKey (quint64 key) |
bool | hasKey () const |
void | setCompressionMode (CompressionMode mode) |
CompressionMode | compressionMode () const |
void | setIntegrityProtectionMode (IntegrityProtectionMode mode) |
IntegrityProtectionMode | integrityProtectionMode () const |
Error | lastError () const |
QString | encryptToString (const QString &plaintext) |
QString | encryptToString (QByteArray plaintext) |
QByteArray | encryptToByteArray (const QString &plaintext) |
QByteArray | encryptToByteArray (QByteArray plaintext) |
QString | decryptToString (const QString &cyphertext) |
QByteArray | decryptToByteArray (const QString &cyphertext) |
QString | decryptToString (QByteArray cypher) |
QByteArray | decryptToByteArray (QByteArray cypher) |
Q_DECLARE_FLAGS (CryptoFlags, CryptoFlag) | |
Simple encryption and decryption of strings and byte arrays.
This class provides a simple implementation of encryption and decryption of strings and byte arrays.
The class uses a 64 bit key. Simply create an instance of the class, set the key, and use the encryptToString() method to calculate an encrypted version of the input string. To decrypt that string again, use an instance of SimpleCrypt initialized with the same key, and call the decryptToString() method with the encrypted string. If the key matches, the decrypted version of the string will be returned again.
If you do not provide a key, or if something else is wrong, the encryption and decryption function will return an empty string or will return a string containing nonsense. lastError() will return a value indicating if the method was succesful, and if not, why not.
SimpleCrypt is prepared for the case that the encryption and decryption algorithm is changed in a later version, by prepending a version identifier to the cypertext.
CompressionMode describes if compression will be applied to the data to be encrypted.
enum SimpleCrypt::Error |
Error describes the type of error that occured.
IntegrityProtectionMode describes measures taken to make it possible to detect problems with the data or wrong decryption keys.
Measures involve adding a checksum or a cryptograhpic hash to the data to be encrypted. This increases the length of the resulting cypertext, but makes it possible to check if the plaintext appears to be valid after decryption.
SimpleCrypt::SimpleCrypt | ( | ) |
Constructor.
Constructs a SimpleCrypt instance without a valid key set on it.
|
explicit |
|
inline |
Returns the CompressionMode that is currently in use.
QByteArray SimpleCrypt::decryptToByteArray | ( | const QString & | cyphertext | ) |
Decrypts a cyphertext string encrypted with this class with the set key back to the plain text version.
If an error occured, such as non-matching keys between encryption and decryption, an empty string or a string containing nonsense may be returned.
QByteArray SimpleCrypt::decryptToByteArray | ( | QByteArray | cypher | ) |
Decrypts a cyphertext binary encrypted with this class with the set key back to the plain text version.
If an error occured, such as non-matching keys between encryption and decryption, an empty string or a string containing nonsense may be returned.
QString SimpleCrypt::decryptToString | ( | const QString & | cyphertext | ) |
Decrypts a cyphertext string encrypted with this class with the set key back to the plain text version.
If an error occured, such as non-matching keys between encryption and decryption, an empty string or a string containing nonsense may be returned.
QString SimpleCrypt::decryptToString | ( | QByteArray | cypher | ) |
Decrypts a cyphertext binary encrypted with this class with the set key back to the plain text version.
If an error occured, such as non-matching keys between encryption and decryption, an empty string or a string containing nonsense may be returned.
QByteArray SimpleCrypt::encryptToByteArray | ( | const QString & | plaintext | ) |
Encrypts the
This method returns a byte array, that is useable for storing a binary format. If you need a string you can store in a text file, use encryptToString() instead.
QByteArray SimpleCrypt::encryptToByteArray | ( | QByteArray | plaintext | ) |
Encrypts the
This method returns a byte array, that is useable for storing a binary format. If you need a string you can store in a text file, use encryptToString() instead.
QString SimpleCrypt::encryptToString | ( | const QString & | plaintext | ) |
Encrypts the
QString SimpleCrypt::encryptToString | ( | QByteArray | plaintext | ) |
Encrypts the
|
inline |
Returns true if SimpleCrypt has been initialized with a key.
|
inline |
Returns the IntegrityProtectionMode that is currently in use.
|
inline |
Returns the last error that occurred.
SimpleCrypt::Q_DECLARE_FLAGS | ( | CryptoFlags | , |
CryptoFlag | |||
) |
|
inline |
Sets the compression mode to use when encrypting data. The default mode is Auto.
Note that decryption is not influenced by this mode, as the decryption recognizes what mode was used when encrypting.
|
inline |
Sets the integrity mode to use when encrypting data. The default mode is Checksum.
Note that decryption is not influenced by this mode, as the decryption recognizes what mode was used when encrypting.
void SimpleCrypt::setKey | ( | quint64 | key | ) |
(Re-) initializes the key with the given