A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/d1/d1c/base64__encoding_8cpp_source.html below:

TheAlgorithms/C++: ciphers/base64_encoding.cpp Source File

32const

std::string chars =

33 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

;

40

std::string base64_encode(

const

std::string &input) {

41

std::string base64_string;

44 for

(uint32_t i = 0; i < input.size(); i += 3) {

45 char

first_byte = input[i];

48

base64_string.push_back(chars[first_byte >> 2]);

50 if

(i + 1 < input.size()) {

51 char

second_byte = input[i + 1];

56

base64_string.push_back(

57

chars[(((first_byte & 3) << 4) | ((second_byte & 0xF0) >> 4))]);

59 if

(i + 2 < input.size()) {

60 char

third_byte = input[i + 2];

65

base64_string.push_back(chars[((third_byte & 0xC0) >> 6) |

66

((second_byte & 0x0F) << 2)]);

68

base64_string.push_back(chars[(third_byte & 0x3F)]);

71

base64_string.push_back(chars[((second_byte & 0x0F) << 2)]);

72

base64_string.push_back(

'='

);

76

base64_string.push_back(chars[((first_byte & 3) << 4)]);

77

base64_string.push_back(

'='

);

78

base64_string.push_back(

'='

);

90

uint8_t find_idx(

const char

c) {

91 if

(c >=

'A'

&& c <=

'Z'

) {

93

}

else if

(c >=

'a'

&& c <=

'z'

) {

95

}

else if

(c >=

'0'

&& c <=

'9'

) {

97

}

else if

(c ==

'+'

) {

99

}

else if

(c ==

'/'

) {

110

std::string base64_decode(

const

std::string &base64_str) {

113 for

(uint32_t i = 0; i < base64_str.size(); i += 4) {

115 char

first_byte = base64_str[i];

117 char

second_byte = base64_str[i + 1];

121 char

first_actual_byte =

static_cast<char>

(

122

(find_idx(first_byte) << 2) | ((find_idx(second_byte)) >> 4));

123

base64_decoded.push_back(first_actual_byte);

124 if

(i + 2 < base64_str.size() && base64_str[i + 2] !=

'='

) {

126 char

third_byte = base64_str[i + 2];

130 char

second_actual_byte =

131 static_cast<char>

(((find_idx(second_byte) & 0x0F) << 4) |

132

(find_idx(third_byte) >> 2));

133

base64_decoded.push_back(second_actual_byte);

135 if

(i + 3 < base64_str.size() && base64_str[i + 3] !=

'='

) {

137 char

fourth_byte = base64_str[i + 3];

140 char

third_actual_byte =

141 static_cast<char>

(((find_idx(third_byte) & 0x03) << 6) |

142

find_idx(fourth_byte));

143

base64_decoded.push_back(third_actual_byte);

147 return

base64_decoded;

159 "To err is human, but to really foul things up you need a computer."

;

160

std::string base64_str = ciphers::base64_encoding::base64_encode(str);

162 "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZW" 163 "VkIGEgY29tcHV0ZXIu"

;

165

assert(base64_str == verify);

166

std::string original_str =

167

ciphers::base64_encoding::base64_decode(base64_str);

169

assert(original_str == str);

173 "Man is distinguished, not only by his reason, but by this singular " 174 "passion from other animals, which is a lust of the mind, that by a " 175 "perseverance of delight in the continued and indefatigable generation " 176 "of knowledge, exceeds the short vehemence of any carnal pleasure."

;

178

base64_str = ciphers::base64_encoding::base64_encode(str);

180 "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieS" 181 "B0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBh" 182 "IGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodC" 183 "BpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25v" 184 "d2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbG" 187

assert(base64_str == verify);

188

original_str = ciphers::base64_encoding::base64_decode(base64_str);

190

assert(original_str == str);

static void test()

Self-test implementations.

int main()

Main function.

Functions for Base64 Encoding and Decoding implementation.

Algorithms for encryption and decryption.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4