simpleaes

simpleaes Commit Details


Date:2013-02-25 22:17:00 (11 years 9 months ago)
Author:Natalie Adams
Branch:default
Commit:444839000904
Message:initial commit

Changes:
AMakefile (full)
Aexample
Amain.cpp (full)
AsimpleAes.cpp (full)
AsimpleAes.hpp (full)
AsimpleAes.o

File differences

Makefile
1
2
3
4
5
6
7
CPPFLAGS=-g -O2
example: main.cpp simpleAes.o
g++ $(CPPFLAGS) -o example main.cpp simpleAes.o -lcrypto
simpleAes.o: simpleAes.cpp simpleAes.hpp
g++ $(CPPFLAGS) -c simpleAes.cpp
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include "simpleAes.hpp"
using namespace std;
int main(){
string text = "This is a test2342 9r8j340 gf9gj0 98dfgj0df98b jdf09g8j";
cout << "String is: " << text << endl;
cout << "Encrypting.." << endl;
text = aes_encrypt(text, "key");
//cout << text << endl;
cout << "Decrypting.." << endl;
text = aes_decrypt(text, "key");
cout << "String is: " << text << endl;
return 0;
}
simpleAes.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*!
* Simple AES
* Brendan Long
* March 29, 2010
*
* Simplified encryption and decryption using OpenSSL's AES library.
* Remember to compile with -lcrypto and link against the library
* g++ (your stuff) -lcrypto simpleAes.cpp (or simpleAes.o)
*
* Implementation note: Using the default ivec (0) is not secure. For
* the full security that AES offers, use a different
* ivec each time (it does not need to be secret,
* just different.
*
* This code is released into the public domain. Yada yada..
* Read this for details: http://creativecommons.org/licenses/publicdomain/
*
* If for some reason public domain isn't good enough, you may use, alter,
* distribute or do anything else you want with this code with no restrictions.
*/
#include <openssl/aes.h>
#include <openssl/evp.h>
#include <iostream>
#include <string.h>
// On Linux, we'll seed openssl with /dev/urandom, on others we use the time
#ifdef __unix__
#include <fstream>
#elif _win32
#include <Wincrypt.h>
#endif
/*!
* Generates 16 random numbers for the initialization vector.
* On Linux, this uses /dev/urandom, on other platforms, it uses c's rand() function
* \param ivec An unsigned char[16] to fill with random numbers
* \return True on success, false otherwise
*/
bool getRandomIvec(unsigned char* ivec){
#ifdef __unix__
// Read 16 bytes from /dev/urandom
std::ifstream fin("/dev/urandom", std::ios_base::in | std::ios_base::binary);
if(!fin.fail()){
fin.read((char*)ivec, 16);
}
else{
fin.open("/dev/urandom", std::ios_base::in | std::ios_base::binary);
if(!fin.fail()){
fin.read((char*)ivec, 16);
}
else return false;
}
fin.close();
return true;
#elif _win32
// I don't have a clue if this works..
HCRYPTPROV hCryptProv;
CryptAcquireContext( &hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)
if(CryptGenRandom(hCryptProv, 16, (BYTE*) ivec)){
printf("Random sequence generated. \n");
}
else{
std::cout << "ERROR: CryptGenRandom failed." << std::endl;
return NULL;
}
#else
// uhh...
#endif
}
/*!
* Encrypts a string using AES 256
* Note: If the key is less than 32 bytes, it will be null padded.
* If the key is greater than 32 bytes, it will be truncated
* \param in The string to encrypt
* \param key The key to encrypt with
* \return The encrypted data
*/
std::string aes_encrypt(std::string in, std::string key){
unsigned char ivec[16];
getRandomIvec(ivec);
std::string ivecString((char*) ivec, 16); // Save this for later
// Always pad the key to 32 bits.. because we can
if(key.length() < 32){
key.append(32 - key.length(), '\0');
}
// Get some space ready for the output
unsigned char *output = new unsigned char[in.length() + AES_BLOCK_SIZE];
// Encrypt the data
int length, finalLength = 0;
EVP_CIPHER_CTX *encryptHandle = new EVP_CIPHER_CTX;
EVP_CIPHER_CTX_init(encryptHandle);
EVP_EncryptInit_ex(encryptHandle, EVP_aes_256_cbc(), NULL, (unsigned char*) key.c_str(), ivec);
EVP_EncryptUpdate(encryptHandle, output, &length, (unsigned char*)in.c_str(), in.length());
finalLength += length;
EVP_EncryptFinal_ex(encryptHandle, output + length, &length);
finalLength += length;
// Make the data into a string
std::string ret((char*) output, finalLength);
// clean up
delete output;
EVP_CIPHER_CTX_cleanup(encryptHandle);
delete encryptHandle;
return ivecString + ret;
}
/*!
* Decrypts a string using AES 256
* Note: If the key is less than 32 bytes, it will be null padded.
* If the key is greater than 32 bytes, it will be truncated
* \param in The string to decrypt
* \param key The key to decrypt with
* \return The decrypted data
*/
std::string aes_decrypt(std::string in, std::string key){
// Get the ivec from the front
unsigned char ivec[16];
strncpy((char*)ivec, in.c_str(), 16);
in = in.substr(16);
// Always pad the key to 32 bits.. because we can
if(key.length() < 32){
key.append(32 - key.length(), '\0');
}
// Create some space for output
unsigned char *output = new unsigned char[in.length()];
int length, finalLength = 0;
// Decrypt the string
EVP_CIPHER_CTX *encryptHandle = new EVP_CIPHER_CTX;
EVP_CIPHER_CTX_init(encryptHandle);
EVP_DecryptInit_ex(encryptHandle, EVP_aes_256_cbc(), NULL, (unsigned char*) key.c_str(), ivec);
EVP_DecryptUpdate(encryptHandle, output, &length, (unsigned char*)in.c_str(), in.length());
finalLength += length;
EVP_DecryptFinal_ex(encryptHandle, output + length, &length);
finalLength += length;
//std::cout << finalLength << std::endl;
// Make the output into a string
std::string ret((char*) output, finalLength);
// Clean up
delete output;
EVP_CIPHER_CTX_cleanup(encryptHandle);
delete encryptHandle;
return ret;
}
simpleAes.hpp
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
/*!
* Simple AES
* Brendan Long
* March 29, 2010
*
* Simplified encryption and decryption using OpenSSL's AES library.
* Remember to compile with -lcrypto and link against the library
* g++ (your stuff) -lcrypto simpleAes.cpp (or simpleAes.o)
*
* Implementation note: Using the default ivec (0) is not secure. For
* the full security that AES offers, use a different
* ivec each time (it does not need to be secret,
* just different.
*
* This code is released into the public domain. Yada yada..
* Read this for details: http://creativecommons.org/licenses/publicdomain/
*
* If for some reason public domain isn't good enough, you may use, alter,
* distribute or do anything else you want with this code with no restrictions.
*/
#include <string>
std::string aes_encrypt(std::string in, std::string key);
std::string aes_encrypt(std::string in, std::string key, unsigned char ivec[16]);
std::string aes_decrypt(std::string in, std::string key);
std::string aes_decrypt(std::string in, std::string key, unsigned char ivec[16]);

Archive Download the corresponding diff file

Branches

Tags

Page rendered in 0.41432s using 14 queries.