CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Cipher.cc
Go to the documentation of this file.
3 #include <string.h>
4 // blowfish encryption
5 #include "blowfish.h"
6 // GNU base 64 encoding
7 #include "base64.h"
8 
9 cond::Cipher::Cipher( const std::string& key ):
10  m_ctx(new BLOWFISH_CTX){
11  char* k = const_cast<char*>(key.c_str());
12  Blowfish_Init( m_ctx, reinterpret_cast<unsigned char*>(k), key.size());
13 }
14 
16  delete m_ctx;
17 }
18 
19 size_t cond::Cipher::bf_process_alloc( const unsigned char* input,
20  size_t input_size,
21  unsigned char*& output,
22  bool decrypt ){
23  uInt32 L, R;
24  unsigned int j = sizeof(uInt32);
25 
26  unsigned int output_size=0;
27  for ( unsigned int i=0; i < input_size; i+=(j*2)){
28  output_size = i+2*j;
29  }
30  output = (unsigned char*) malloc( output_size );
31  memset(output, 0, output_size);
32 
33  for (unsigned int i=0; i < input_size; i+=(j*2)) {
34  L = R = 0;
35  unsigned int nl = 0;
36  unsigned int nr = 0;
37  if( input_size > i+j ){
38  nl = j;
39  if( input_size > i+2*j ){
40  nr = j;
41  } else {
42  nr = input_size-i-j;
43  }
44  } else {
45  nl = input_size-i;
46  nr = 0;
47  }
48  if(nl) memcpy(&L, input+i, nl);
49  if(nr) memcpy(&R, input+i+j, nr);
50  if( !decrypt ){
51  Blowfish_Encrypt(m_ctx, &L, &R);
52  } else {
53  Blowfish_Decrypt(m_ctx, &L, &R);
54  }
55  memcpy(output+i, &L, j);
56  memcpy(output+i+j, &R, j);
57  }
58 
59  return output_size;
60 }
61 
62 size_t cond::Cipher::encrypt( const std::string& input, unsigned char*& output ){
63  return bf_process_alloc( reinterpret_cast<const unsigned char*>(input.c_str()), input.size(), output, false );;
64 }
65 
66 std::string cond::Cipher::decrypt( const unsigned char* input, size_t inputSize ){
67  unsigned char* out = 0;
68  size_t outSize = bf_process_alloc( input, inputSize, out, true );
69  char* sout = reinterpret_cast<char*>(out);
70  // the output can still contain one or more \0 chars...
71  size_t soutSize = strlen( sout );
72  if( soutSize < outSize ){
73  outSize = soutSize;
74  }
75  std::string ret( sout, outSize );
76  free (out );
77  return ret;
78 }
79 
80 std::string cond::Cipher::b64encrypt( const std::string& input ){
81  unsigned char* out = 0;
82  size_t outSize = bf_process_alloc( reinterpret_cast<const unsigned char*>(input.c_str()), input.size(), out, false );
83  char* b64out = 0;
84  size_t b64size = base64_encode_alloc( reinterpret_cast<const char*>(out), outSize, &b64out );
85  std::string ret( b64out, b64size );
86  free (out);
87  free (b64out);
88  return ret;
89 }
90 
91 std::string cond::Cipher::b64decrypt( const std::string& b64in ){
92  char* input = 0;
93  size_t inputSize = 0;
94  if( !base64_decode_alloc( b64in.c_str(), b64in.size(), &input, &inputSize ) ){
95  throwException("Input provided is not a valid base64 string.","Cipher::b64decrypt");
96  }
97  std::string ret = decrypt( reinterpret_cast<const unsigned char*>(input), inputSize );
98  free (input);
99  return ret;
100 }
101 
int i
Definition: DBlmapReader.cc:9
BLOWFISH_CTX * m_ctx
Definition: Cipher.h:32
void Blowfish_Encrypt(BLOWFISH_CTX *ctx, uInt32 *xl, uInt32 *xr)
Definition: blowfish.cc:299
void Blowfish_Decrypt(BLOWFISH_CTX *ctx, uInt32 *xl, uInt32 *xr)
Definition: blowfish.cc:326
void Blowfish_Init(BLOWFISH_CTX *ctx, unsigned char *key, int keyLen)
Definition: blowfish.cc:355
unsigned int uInt32
Definition: blowfish.h:26
Cipher(const std::string &key)
Definition: Cipher.cc:9
std::string decrypt(const unsigned char *input, size_t inputSize)
Definition: Cipher.cc:66
size_t encrypt(const std::string &input, unsigned char *&output)
Definition: Cipher.cc:62
int j
Definition: DBlmapReader.cc:9
size_t bf_process_alloc(const unsigned char *input, size_t input_size, unsigned char *&output, bool decrypt=false)
Definition: Cipher.cc:19
size_t base64_encode_alloc(const char *in, size_t inlen, char **out)
Definition: base64.cc:117
int k[5][pyjets_maxn]
tuple out
Definition: dbtoconf.py:99
std::string b64encrypt(const std::string &input)
Definition: Cipher.cc:80
#define base64_decode_alloc(in, inlen, out, outlen)
Definition: base64.h:60
void throwException(std::string const &message, std::string const &methodName)
Definition: Exception.cc:17
std::string b64decrypt(const std::string &input)
Definition: Cipher.cc:91
list key
Definition: combine.py:13