Cipher Class
|
The cipher object encrypts plaintext data or decrypts ciphertext data. More...
#include <cipher.h>
Public Types | |
typedef uchar | aes_iv_t [32] |
typedef uchar | aes_key_t [32] |
typedef uchar | aes_salt_t [8] |
typedef std::pair< uchar *, uint > | kv1_t |
typedef unsigned char | uchar |
typedef unsigned int | uint |
Public Member Functions | |
Cipher () | |
Constructor. More... | |
Cipher (const std::string &cipher, const std::string &digest, uint count, bool embed=true) | |
Constructor. More... | |
~Cipher () | |
Destructor. More... | |
void | debug (bool b=true) |
Set the internal debug flag. More... | |
bool | debug () const |
Is debug mode set? More... | |
kv1_t | decode_base64 (const std::string &mimetext) const |
Base64 decode. More... | |
std::string | decode_cipher (uchar *ciphertext, uint ciphertext_len) const |
Cipher decode. More... | |
std::string | decrypt (const std::string &ciphertext, const std::string &pass="", const std::string &salt="") |
Decrypt a buffer using AES 256 CBC (SHA1). More... | |
void | decrypt_file (const std::string &ifn, const std::string &ofn, const std::string &pass="", const std::string &salt="") |
Decrypt a file. More... | |
std::string | encode_base64 (uchar *ciphertext, uint ciphertext_len) const |
Base64 encode. More... | |
kv1_t | encode_cipher (const std::string &plaintext) const |
Cipher encode. More... | |
std::string | encrypt (const std::string &plaintext, const std::string &pass="", const std::string &salt="") |
Encrypt buffer using AES 256 CBC (SHA1). More... | |
void | encrypt_file (const std::string &ifn, const std::string &ofn, const std::string &pass="", const std::string &salt="") |
Encrypt a file. More... | |
std::string | file_read (const std::string &fn) const |
Read a file into a buffer. More... | |
void | file_write (const std::string &fn, const std::string &data, bool nl=false) const |
Write ASCII data to a file. More... | |
Private Member Functions | |
void | init (const std::string &pass) |
Initialize the cipher: set the key and IV values. More... | |
void | set_salt (const std::string &salt) |
Convert string salt to internal format. More... | |
Private Attributes | |
std::string | m_cipher |
uint | m_count |
bool | m_debug |
std::string | m_digest |
bool | m_embed |
aes_iv_t | m_iv |
aes_key_t | m_key |
std::string | m_pass |
aes_salt_t | m_salt |
The cipher object encrypts plaintext data or decrypts ciphertext data.
All data is in ASCII because it is MIME encoded.
The default cipher used is AES-256-CBC from the openssl library but there are many others available. The default digest used is SHA1 which is stronger than MD5.
The algorithms mimic openssl so files created with this object and with the openssl tool are interchangeable.
Here is how you would use it to encrypt and decrypt plaintext data in memory.
typedef std::pair<uchar*,uint> Cipher::kv1_t |
typedef unsigned char Cipher::uchar |
typedef unsigned int Cipher::uint |
Cipher::Cipher | ( | const std::string & | cipher, |
const std::string & | digest, | ||
uint | count, | ||
bool | embed = true |
||
) |
|
inline |
|
inline |
Cipher::kv1_t Cipher::decode_base64 | ( | const std::string & | mimetext | ) | const |
string Cipher::decrypt | ( | const std::string & | ciphertext, |
const std::string & | pass = "" , |
||
const std::string & | salt = "" |
||
) |
Decrypt a buffer using AES 256 CBC (SHA1).
ciphertext | The encrypted data. |
pass | The passphrase. |
salt | The optional salt. |
Definition at line 218 of file cipher.cc.
References DBG_BDUMP, DBG_FCT, DBG_MDUMP, decode_base64(), decode_cipher(), init(), m_salt, SALTED_PREFIX, and set_salt().
Referenced by decrypt_file().
void Cipher::decrypt_file | ( | const std::string & | ifn, |
const std::string & | ofn, | ||
const std::string & | pass = "" , |
||
const std::string & | salt = "" |
||
) |
Decrypt a file.
Here is a usage example.
ifn | The encrypted file. |
ofn | The plaintext file. |
pass | The passphrase. |
salt | The optional salt. |
Definition at line 247 of file cipher.cc.
References DBG_FCT, decrypt(), file_read(), and file_write().
Cipher::kv1_t Cipher::encode_cipher | ( | const std::string & | plaintext | ) | const |
string Cipher::encrypt | ( | const std::string & | plaintext, |
const std::string & | pass = "" , |
||
const std::string & | salt = "" |
||
) |
Encrypt buffer using AES 256 CBC (SHA1).
plaintext | The plaintext buffer. |
pass | The passphrase. |
salt | The optional salt. |
Definition at line 183 of file cipher.cc.
References DBG_BDUMP, DBG_FCT, DBG_MDUMP, encode_base64(), encode_cipher(), init(), and set_salt().
Referenced by encrypt_file().
void Cipher::encrypt_file | ( | const std::string & | ifn, |
const std::string & | ofn, | ||
const std::string & | pass = "" , |
||
const std::string & | salt = "" |
||
) |
Encrypt a file.
Here is a usage example.
ifn | The plaintext file. |
ofn | The encrypted file. |
pass | The passphrase. |
salt | The optional salt. |
runtime_error | If a problem occurs. |
Definition at line 204 of file cipher.cc.
References DBG_FCT, encrypt(), file_read(), and file_write().
string Cipher::file_read | ( | const std::string & | fn | ) | const |
Read a file into a buffer.
fn | The file name. |
runtime_error | if the file doesn't exist. |
Definition at line 482 of file cipher.cc.
References DBG_FCT.
Referenced by decrypt_file(), and encrypt_file().
void Cipher::file_write | ( | const std::string & | fn, |
const std::string & | data, | ||
bool | nl = false |
||
) | const |
Write ASCII data to a file.
fn | The file name. |
data | The data to write. |
nl | Append a trailing new line. |
runtime_error | if the file cannot be written. |
Definition at line 498 of file cipher.cc.
References DBG_FCT.
Referenced by decrypt_file(), and encrypt_file().
|
private |
Initialize the cipher: set the key and IV values.
pass | The passphrase. |
Definition at line 429 of file cipher.cc.
References DBG_FCT, DBG_PKV, DBG_TDUMP, m_cipher, m_count, m_digest, m_iv, m_key, m_pass, and m_salt.
Referenced by decrypt(), and encrypt().
|
private |
|
private |
Definition at line 296 of file cipher.h.
Referenced by encode_cipher().
|
private |
Definition at line 294 of file cipher.h.
Referenced by decode_cipher(), encode_cipher(), and init().
|
private |
Definition at line 293 of file cipher.h.
Referenced by decode_cipher(), encode_cipher(), and init().
|
private |
Definition at line 292 of file cipher.h.
Referenced by decrypt(), encode_cipher(), init(), and set_salt().