forked from rabinnh/cpp-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSporeEncrypt.cpp
78 lines (62 loc) · 1.81 KB
/
CSporeEncrypt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* File: CSporeEncrypt.cpp
* Author: rbross
*
* Created on March 2, 2011, 12:43 PM
*/
#include "CSporeEncrypt.h"
#include <dlfcn.h>
int CSporeEncrypt::iKeySize = 0;
unsigned char *CSporeEncrypt::ucKey = NULL;
CSporeEncrypt::CSporeEncrypt()
{
}
CSporeEncrypt::~CSporeEncrypt()
{
}
// We will look for libsporekey.so in /usr/local/silverspore/bin/
void CSporeEncrypt::GetKey()
{
// Returns size of key. Caller must delete ucKey
int (*generatekey)(unsigned char **ucKey);
void *lib_handle;
int iSize;
CSporeEncrypt::ucKey = NULL;
iSize = 0;
lib_handle = dlopen("/usr/local/silverspore/bin/libspore.so", RTLD_LAZY);
if (!lib_handle)
return;
generatekey = (int (*)(unsigned char **)) dlsym(lib_handle, "generatekey");
if (dlerror() != NULL)
return;
CSporeEncrypt::iKeySize = (generatekey)(&CSporeEncrypt::ucKey);
dlclose(lib_handle);
}
/*
* ARGS:
* keydata == ascii text, the encryption passphrase
* keydatalen == how long keydata is
* in == the data to be encrypted
* out == the encrypted data.
* inlen == length of the in array
*/
void CSporeEncrypt::BF_encrypt(const unsigned char *keydata, int keydatalen, unsigned char *in, unsigned char *out, unsigned int inlen)
{
BF_KEY key;
unsigned char ivec[32];
int num = 0;
// set up for encryption
BF_set_key(&key, keydatalen, keydata);
memset(ivec, '\0', 32);
BF_cfb64_encrypt(in, out, inlen, &key, ivec, &num, BF_ENCRYPT);
}
void CSporeEncrypt::BF_decrypt(const unsigned char *keydata, int keydatalen, unsigned char *in, unsigned char *out, unsigned int inlen)
{
BF_KEY key;
unsigned char ivec[32];
int num = 0;
// set up for decryption
BF_set_key(&key, keydatalen, keydata);
memset(ivec, '\0', 32);
BF_cfb64_encrypt(in, out, inlen, &key, ivec, &num, BF_DECRYPT);
}