Skip to content

Commit 1867788

Browse files
authored
Merge pull request csubhasundar#174 from ashish-bazad/main
Adding base64 encoder and decoder code cpp
2 parents 6efccb5 + cb22194 commit 1867788

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

c++/.DS_Store

6 KB
Binary file not shown.

c++/Base64_Encoder_Decoder.cpp

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
// __builtin_clz(x): the number of zeros at the beginning of the number
2+
// __builtin_ctz(x): the number of zeros at the end of the number
3+
// __builtin_popcount(x): the number of ones in the number
4+
// __builtin_parity(x): the parity (even or odd) of the number of ones
5+
// __builtin_ffs(int) finds the index of the first (most right) set bit
6+
#include<bits/stdc++.h>
7+
using namespace std;
8+
typedef long long int ll;
9+
typedef long double ld;
10+
typedef unsigned long long int ull;
11+
typedef vector<int> vi;
12+
typedef vector<vi> vvi;
13+
typedef vector<ll> vll;
14+
typedef pair<int,int> pii;
15+
typedef pair<long long int,long long int> pll;
16+
typedef vector<pii> vpii;
17+
typedef vector<pll> vpll;
18+
#define repi(i,s,e) for(ll i=s;i<e;i++)
19+
#define repd(i,e,s) for(ll i=e;i>=s;i--)
20+
#define all(x) x.begin(),x.end()
21+
#define begz(x) __builtin_clz(x)
22+
#define endz(x) __builtin_ctz(x)
23+
#define setbits(x) __builtin_popcount(x)
24+
#define ffs(x) __builtin_ffs(x)
25+
#define sz(x) x.size()
26+
#define YES cout<<"YES\n"
27+
#define NO cout<<"NO\n"
28+
#define Yes cout<<"Yes\n"
29+
#define No cout<<"No\n"
30+
#define pb push_back
31+
#define F first
32+
#define S second
33+
#define sp " "
34+
#define nl "\n"
35+
#define INF numeric_limits<float>::infinity();
36+
const ll MOD = 1e9 + 7;
37+
void input_output_path();
38+
class DSU{private:vll parent;vll rank;public:DSU(ll n){parent.resize(n);rank.resize(n,0);for(ll i=0;i<n;++i){parent[i]=i;}}ll find(ll x){if(parent[x]==x)return x;return parent[x]=find(parent[x]);}void union_sets(ll x, ll y){ll root_x = find(x);ll root_y = find(y);if (root_x != root_y){if (rank[root_x] < rank[root_y])parent[root_x] = root_y;else if (rank[root_x] > rank[root_y])parent[root_y] = root_x;else{parent[root_y] = root_x;rank[root_x]++;}}}};
39+
string reverse_str(string str){string s="";for(ll i=str.size()-1 ; i>=0 ; i--)s+=str[i];return s;}
40+
string DecimalToBinary(ll num){string str;if(num==0)return "0";while(num){if(num & 1)str+='1';else str+='0';num>>=1;}return reverse_str(str);}
41+
bool isPrime(ll n){if (n <= 1)return false; if (n==2) return true; for (ll i = 2; i < sqrt(n)+1; i++)if (n % i == 0)return false;return true;}
42+
ll gcdExtended(ll a, ll b, ll &x, ll &y){if (a == 0) {x = 0;y = 1;return b;}ll x1, y1;ll gcd = gcdExtended(b % a, a, x1, y1);x = y1 - (b / a) * x1;y = x1;return gcd;}
43+
ll modInverse(ll a, ll M){ll x, y;ll gcd = gcdExtended(a, M, x, y);if (gcd != 1) {return -1;} else {return (x % M + M) % M;}}
44+
ll modPow(ll base, ll exponent, ll modulus){ if (exponent==-1){return modInverse(base,modulus);} if (modulus == 1) return 0;ll result = 1;base %= modulus;while (exponent > 0){if (exponent % 2 == 1)result = (result * base) % modulus;base = (base * base) % modulus;exponent /= 2;}return result;}
45+
ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);}
46+
ll lcm(ll a, ll b){return (a / gcd(a, b)) * b;}
47+
bool vpsort(const pair<ll, ll>& a, const pair<ll, ll>& b){if (a.first == b.first)return a.second < b.second;return a.first < b.first;}
48+
bool isPowerOfTwo(ull n){return n && !(n & (n - 1));}
49+
ll round_pow2(ll x){return pow(2,ceil(log(x)/log(2)));}
50+
ll binaryToDecimal(string n){string num = n;ll dec_value = 0;ll base = 1;ll len = num.length();for (ll i = len - 1; i >= 0; i--){if (num[i] == '1')dec_value += base;base = base * 2;}return dec_value;}
51+
52+
string base64_encode(const string input)
53+
{
54+
static const string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
55+
string encoded;
56+
57+
int i = 0, j = 0;
58+
unsigned char char_array_3[3], char_array_4[4];
59+
60+
for( int k = 0; k < input.size(); k++)
61+
{
62+
char_array_3[i++] = input[k];
63+
if ( i == 3 )
64+
{
65+
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
66+
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
67+
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
68+
char_array_4[3] = char_array_3[2] & 0x3f;
69+
70+
for ( int m = 0; m < 4; ++m )
71+
{
72+
encoded += base64_chars[char_array_4[m]];
73+
}
74+
i = 0;
75+
}
76+
}
77+
if ( i > 0 )
78+
{
79+
for ( int k = 8; k < 3; ++k )
80+
{
81+
char_array_3[k] = '\0';
82+
}
83+
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
84+
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
85+
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
86+
87+
for (int k = 0; k < i + 1; ++k)
88+
{
89+
encoded += base64_chars[char_array_4[k]];
90+
}
91+
92+
while (i++ < 3)
93+
{
94+
encoded += '=';
95+
}
96+
}
97+
return encoded;
98+
}
99+
100+
string base64_decode(const string input)
101+
{
102+
static const string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
103+
string decoded;
104+
105+
int i = 0, j = 0, in_ = 0;
106+
unsigned char char_array_4[4], char_array_3[3];
107+
108+
for ( int k = 0; k < input.size(); ++k)
109+
{
110+
if ( input[k] == '=' )
111+
break;
112+
if ( base64_chars.find( input[k] ) == string::npos )
113+
return "Invalid base64 character!";
114+
115+
char_array_4[i++] = input[k];
116+
if (i == 4)
117+
{
118+
for (int m = 0; m < 4; ++m) {
119+
char_array_4[m] = base64_chars.find(char_array_4[m]);
120+
}
121+
122+
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
123+
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
124+
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
125+
126+
for (int m = 0; m < 3; ++m) {
127+
decoded += char_array_3[m];
128+
}
129+
i = 0;
130+
}
131+
}
132+
if (i > 0)
133+
{
134+
for ( int k = i; k < 4; ++k )
135+
char_array_4[k] = 0;
136+
137+
for (int k = 0; k < 4; ++k)
138+
char_array_4[k] = base64_chars.find(char_array_4[k]);
139+
140+
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
141+
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
142+
143+
for (int k = 0; k < i - 1; ++k)
144+
decoded += char_array_3[k];
145+
}
146+
147+
return decoded;
148+
}
149+
void solve()
150+
{
151+
cout<<"Choose mode:\n1. Encode to base64\n2. Decode from base64\n";
152+
int choice;
153+
cin>>choice;
154+
155+
string input;
156+
cout<<"Enter the string: ";
157+
cin>>input;
158+
159+
string result;
160+
switch(choice)
161+
{
162+
case 1:
163+
result = base64_encode(input);
164+
cout<<"Encode String: "<<result<<nl;
165+
break;
166+
case 2:
167+
result = base64_decode(input);
168+
cout<<"Decoded String: "<<result<<nl;
169+
break;
170+
default:
171+
cout<<"Invalid Choice!"<<nl;
172+
}
173+
}
174+
175+
int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);solve();return 0;}
176+
void input_output_path()
177+
{
178+
#ifndef ONLINE_JUDGE
179+
freopen("input.txt","r",stdin);
180+
freopen("output.txt","w",stdout);
181+
#endif
182+
}

0 commit comments

Comments
 (0)