forked from shanet/Crypto-Example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase64.cpp
63 lines (51 loc) · 1.71 KB
/
base64.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
#include "base64.h"
char* base64Encode(const unsigned char *message, const size_t length) {
BIO *bio;
BIO *b64;
FILE* stream;
int encodedSize = 4*ceil((double)length/3);
char *buffer = (char*)malloc(encodedSize+1);
if(buffer == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
exit(1);
}
stream = fmemopen(buffer, encodedSize+1, "w");
b64 = BIO_new(BIO_f_base64());
bio = BIO_new_fp(stream, BIO_NOCLOSE);
bio = BIO_push(b64, bio);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
BIO_write(bio, message, length);
(void)BIO_flush(bio);
BIO_free_all(bio);
fclose(stream);
return buffer;
}
int base64Decode(const char *b64message, const size_t length, unsigned char **buffer) {
BIO *bio;
BIO *b64;
int decodedLength = calcDecodeLength(b64message, length);
*buffer = (unsigned char*)malloc(decodedLength+1);
if(*buffer == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
exit(1);
}
FILE* stream = fmemopen((char*)b64message, length, "r");
b64 = BIO_new(BIO_f_base64());
bio = BIO_new_fp(stream, BIO_NOCLOSE);
bio = BIO_push(b64, bio);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
decodedLength = BIO_read(bio, *buffer, length);
(*buffer)[decodedLength] = '\0';
BIO_free_all(bio);
fclose(stream);
return decodedLength;
}
int calcDecodeLength(const char *b64input, const size_t length) {
int padding = 0;
// Check for trailing '=''s as padding
if(b64input[length-1] == '=' && b64input[length-2] == '=')
padding = 2;
else if (b64input[length-1] == '=')
padding = 1;
return (int)length*0.75 - padding;
}