CMS 3D CMS Logo

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 #include <cassert>
9 
11  m_ctx(new BLOWFISH_CTX){
12  char* k = const_cast<char*>(key.c_str());
13  Blowfish_Init( m_ctx, reinterpret_cast<unsigned char*>(k), key.size());
14 }
15 
17  delete m_ctx;
18 }
19 
20 size_t cond::auth::Cipher::bf_process_alloc( const unsigned char* input,
21  size_t input_size,
22  unsigned char*& output,
23  bool decrypt ){
24  assert(input_size != 0);
25 
26  uInt32 L, R;
27  unsigned int j = sizeof(uInt32);
28 
29  unsigned int output_size=0;
30 
31  if( !input_size ) {
32  output = 0;
33  return 0;
34  }
35 
36  for ( unsigned int i=0; i < input_size; i+=(j*2)){
37  output_size = i+2*j;
38  }
39  output = (unsigned char*) malloc( output_size );
40  memset(output, 0, output_size);
41 
42  for (unsigned int i=0; i < input_size; i+=(j*2)) {
43  L = R = 0;
44  unsigned int nl = 0;
45  unsigned int nr = 0;
46  if( input_size > i+j ){
47  nl = j;
48  if( input_size > i+2*j ){
49  nr = j;
50  } else {
51  nr = input_size-i-j;
52  }
53  } else {
54  nl = input_size-i;
55  nr = 0;
56  }
57  if(nl) memcpy(&L, input+i, nl);
58  if(nr) memcpy(&R, input+i+j, nr);
59  if( !decrypt ){
60  Blowfish_Encrypt(m_ctx, &L, &R);
61  } else {
62  Blowfish_Decrypt(m_ctx, &L, &R);
63  }
64  memcpy(output+i, &L, j);
65  memcpy(output+i+j, &R, j);
66  }
67 
68  return output_size;
69 }
70 
71 size_t cond::auth::Cipher::encrypt( const std::string& input, unsigned char*& output ){
72  if( input.empty() ) {
73  output = 0;
74  return 0;
75  }
76  return bf_process_alloc( reinterpret_cast<const unsigned char*>(input.c_str()), input.size(), output, false );;
77 }
78 
79 std::string cond::auth::Cipher::decrypt( const unsigned char* input, size_t inputSize ){
80  if( !inputSize ) return "";
81  unsigned char* out = 0;
82  size_t outSize = bf_process_alloc( input, inputSize, out, true );
83  size_t i = 0;
84  for( i=0;i<outSize; i++ ) {
85  if( out[i]==0 ) break;
86  }
87 
88  char* sout = reinterpret_cast<char*>(out);
89  // the output can still contain one or more \0 chars...
90  //size_t soutSize = strlen( sout );
91  size_t soutSize = 0;
92  for( soutSize=0; soutSize<outSize; soutSize++) if( out[soutSize]==0 ) break;
93 
94  if( soutSize < outSize ){
95  outSize = soutSize;
96  }
97 
98  std::string ret("");
99  if( outSize )
100  ret = std::string( sout, outSize );
101  free (out );
102  return ret;
103 }
104 
106  if( input.empty() ) return "";
107  unsigned char* out = 0;
108  size_t outSize = bf_process_alloc( reinterpret_cast<const unsigned char*>(input.c_str()), input.size(), out, false );
109  char* b64out = 0;
110  size_t b64size = base64_encode_alloc( reinterpret_cast<const char*>(out), outSize, &b64out );
111  std::string ret( b64out, b64size );
112  free (out);
113  free (b64out);
114  return ret;
115 }
116 
118  if( b64in.empty() ) return "";
119  char* input = 0;
120  size_t inputSize = 0;
121  if( !base64_decode_alloc( b64in.c_str(), b64in.size(), &input, &inputSize ) ){
122  throwException("Input provided is not a valid base64 string.","Cipher::b64decrypt");
123  }
124  std::string ret = decrypt( reinterpret_cast<const unsigned char*>(input), inputSize );
125  free (input);
126  return ret;
127 }
128 
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
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:21
static std::string const input
Definition: EdmProvDump.cc:44
size_t encrypt(const std::string &input, unsigned char *&output)
Definition: Cipher.cc:71
size_t base64_encode_alloc(const char *in, size_t inlen, char **out)
Definition: base64.cc:117
int k[5][pyjets_maxn]
Cipher(const std::string &key)
Definition: Cipher.cc:10
#define base64_decode_alloc(in, inlen, out, outlen)
Definition: base64.h:60
size_t bf_process_alloc(const unsigned char *input, size_t input_size, unsigned char *&output, bool decrypt=false)
Definition: Cipher.cc:20
std::string b64encrypt(const std::string &input)
Definition: Cipher.cc:105
std::string decrypt(const unsigned char *input, size_t inputSize)
Definition: Cipher.cc:79
BLOWFISH_CTX * m_ctx
Definition: Cipher.h:34
std::string b64decrypt(const std::string &input)
Definition: Cipher.cc:117