-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaes_class.h
60 lines (56 loc) · 2.12 KB
/
aes_class.h
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
#pragma once
#define aes_size 16
class AES_key {
uint8_t key[aes_size];
public:
AES_key();
operator const uint8_t*() const;
AES_key(const char* k);
};
/** This class is intended to pass the data safely to the web-service. Th correct pattern of usage:
* On the c++ side:
*
* \code
* AES_key k("16Characters Key");// there should be exactly 16 characters
* AES_buffer b;
* b << "The string to encode";
* b.EncryptAES_ECB(k);
* std::string b64 = b.toBase64();//we pass that string to server, pay attention that + is incorrect URL sign, the string should be URL encoded
* //we get ""BeqQb5e6PKt2IFM5WTLnDj/PRNRxN7U24+MT28ZTdZQ="" in b64, pay attention to "+" that should be URL encoded or replaced to be passed correctly in the URL.
* \code
*
* on the Node.js server side
*
* \code
* const base64string = "BeqQb5e6PKt2IFM5WTLnDj/PRNRxN7U24+MT28ZTdZQ=";//we got it from the c++ side
* const value = CryptoJS.enc.Base64.parse(base64string);
* const key = CryptoJS.enc.Utf8.parse("16Characters Key");
* console.log(CryptoJS.AES.decrypt({ ciphertext: value },key, { keySize: 16, mode: CryptoJS.mode.ECB }).toString(CryptoJS.enc.Utf8));
* // we get "The string to encode" on console
* \code
*
*/
class AES_buffer {
std::vector<uint8_t> buf;
int actual;
public:
AES_buffer();;
operator uint8_t*();
size_t length() const;
void operator <<(uint8_t b);;
void operator <<(const char* str);
std::string toHEX();
std::string toBase64();
void EncryptAES_ECB(const AES_key& key);
};
/**
* And the supershort version of encrypted passing :
* The previous example reduces to:
* \code
* std::string b64 = aes_base64_encrypt("The string to encode", "16Characters Key");//we pass that string to server, pay attention that + is incorrect URL sign, the string should be URL encoded
* //we get ""BeqQb5e6PKt2IFM5WTLnDj/PRNRxN7U24+MT28ZTdZQ="" in b64, pay attention to "+" that should be URL encoded or replaced to be passed correctly in the URL.
* std::string toServer = encodeURL(b64.c_str()); // ready to be part of URL
* \code
*/
std::string aes_base64_encrypt(const char* string, const char* key16chars);
std::string encodeURL(const char* str);