Cipher Class
 All Classes Namespaces Files Functions Variables Typedefs Macros Pages
cipher.h
Go to the documentation of this file.
1 // ================================================================
2 // Description: Cipher class.
3 // Copyright: Copyright (c) 2012 by Joe Linoff
4 // Version: 1.2
5 // Author: Joe Linoff
6 //
7 // LICENSE
8 // The cipher package is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // The cipher package is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details. You should have received
17 // a copy of the GNU General Public License along with the change
18 // tool; if not, write to the Free Software Foundation, Inc., 59
19 // Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 // ================================================================
21 #ifndef cipher_h
22 #define cipher_h
23 
24 #include <string>
25 #include <vector>
26 #include <utility> // pair
27 
28 #define CIPHER_DEFAULT_CIPHER "aes-256-cbc"
29 #define CIPHER_DEFAULT_DIGEST "sha1"
30 #define CIPHER_DEFAULT_COUNT 1
31 
74 class Cipher
75 {
76 public:
77  typedef unsigned int uint;
78  typedef unsigned char uchar;
79  typedef uchar aes_key_t[32];
80  typedef uchar aes_iv_t[32];
81  typedef uchar aes_salt_t[8];
82  typedef std::pair<uchar*,uint> kv1_t;
83 public:
87  Cipher();
88 
97  Cipher(const std::string& cipher,
98  const std::string& digest,
99  uint count,
100  bool embed=true);
101 
105  ~Cipher();
106 public:
114  std::string encrypt(const std::string& plaintext,
115  const std::string& pass="",
116  const std::string& salt="");
117 
157  void encrypt_file(const std::string& ifn,
158  const std::string& ofn,
159  const std::string& pass="",
160  const std::string& salt="");
161 public:
169  std::string decrypt(const std::string& ciphertext,
170  const std::string& pass="",
171  const std::string& salt="");
172 
210  void decrypt_file(const std::string& ifn,
211  const std::string& ofn,
212  const std::string& pass="",
213  const std::string& salt="");
214 public:
221  std::string encode_base64(uchar* ciphertext,
222  uint ciphertext_len) const;
223 
229  kv1_t encode_cipher(const std::string& plaintext) const;
230 
236  kv1_t decode_base64(const std::string& mimetext) const;
237 
244  std::string decode_cipher(uchar* ciphertext,
245  uint ciphertext_len) const;
246 public:
253  std::string file_read(const std::string& fn) const;
261  void file_write(const std::string& fn,
262  const std::string& data,
263  bool nl=false) const;
264 public:
270  void debug(bool b=true) {m_debug=b;}
275  bool debug() const {return m_debug;}
276 private:
281  void set_salt(const std::string& salt);
286  void init(const std::string& pass);
287 
288 private:
289  std::string m_pass;
290  std::string m_cipher;
291  std::string m_digest;
296  bool m_embed;
297  bool m_debug;
298 };
299 
300 #endif
std::string decrypt(const std::string &ciphertext, const std::string &pass="", const std::string &salt="")
Decrypt a buffer using AES 256 CBC (SHA1).
Definition: cipher.cc:218
The cipher object encrypts plaintext data or decrypts ciphertext data.
Definition: cipher.h:74
aes_key_t m_key
Definition: cipher.h:293
std::pair< uchar *, uint > kv1_t
Definition: cipher.h:82
void encrypt_file(const std::string &ifn, const std::string &ofn, const std::string &pass="", const std::string &salt="")
Encrypt a file.
Definition: cipher.cc:204
void file_write(const std::string &fn, const std::string &data, bool nl=false) const
Write ASCII data to a file.
Definition: cipher.cc:498
bool m_embed
Definition: cipher.h:296
std::string encrypt(const std::string &plaintext, const std::string &pass="", const std::string &salt="")
Encrypt buffer using AES 256 CBC (SHA1).
Definition: cipher.cc:183
std::string m_digest
Definition: cipher.h:291
std::string m_cipher
Definition: cipher.h:290
std::string decode_cipher(uchar *ciphertext, uint ciphertext_len) const
Cipher decode.
Definition: cipher.cc:371
std::string file_read(const std::string &fn) const
Read a file into a buffer.
Definition: cipher.cc:482
Cipher()
Constructor.
Definition: cipher.cc:149
~Cipher()
Destructor.
Definition: cipher.cc:176
uchar aes_iv_t[32]
Definition: cipher.h:80
void set_salt(const std::string &salt)
Convert string salt to internal format.
Definition: cipher.cc:406
bool m_debug
Definition: cipher.h:297
unsigned int uint
Definition: cipher.h:77
aes_iv_t m_iv
Definition: cipher.h:294
unsigned char uchar
Definition: cipher.h:78
aes_salt_t m_salt
Definition: cipher.h:292
std::string m_pass
Definition: cipher.h:289
uint m_count
Definition: cipher.h:295
void init(const std::string &pass)
Initialize the cipher: set the key and IV values.
Definition: cipher.cc:429
uchar aes_salt_t[8]
Definition: cipher.h:81
kv1_t encode_cipher(const std::string &plaintext) const
Cipher encode.
Definition: cipher.cc:322
std::string encode_base64(uchar *ciphertext, uint ciphertext_len) const
Base64 encode.
Definition: cipher.cc:261
void debug(bool b=true)
Set the internal debug flag.
Definition: cipher.h:270
kv1_t decode_base64(const std::string &mimetext) const
Base64 decode.
Definition: cipher.cc:290
uchar aes_key_t[32]
Definition: cipher.h:79
void decrypt_file(const std::string &ifn, const std::string &ofn, const std::string &pass="", const std::string &salt="")
Decrypt a file.
Definition: cipher.cc:247
bool debug() const
Is debug mode set?
Definition: cipher.h:275