From cb22194f93a709e90295d134257f8e377439e477 Mon Sep 17 00:00:00 2001 From: Ashish Bazad Date: Sun, 15 Oct 2023 23:11:31 +0530 Subject: [PATCH] Adding base64 encoder and decoder code --- .DS_Store | Bin 0 -> 6148 bytes c++/.DS_Store | Bin 0 -> 6148 bytes c++/Base64_Encoder_Decoder.cpp | 182 +++++++++++++++++++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 .DS_Store create mode 100644 c++/.DS_Store create mode 100644 c++/Base64_Encoder_Decoder.cpp diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..756d8941560a826b3387863e38aada62b9a5042e GIT binary patch literal 6148 zcmeHK&x_MQ6n@jzZE6vVDC|WDc<`{xT6fEWc&W7>{B<#+2bG#Mp&LyzC26;mQs`O# z6~U{2iT{fy{oc$hq;0%-5RrLh=9|oX?@i`Q@}@&XqBjfg619lPL1C<2Mpa@w&Sl3c zrl$!MDn?F{GMds6O?KF}!zy4E_}3KRZ?{PSzS|K>@%vpoPUJ91WB>-zLuCX!`l(DU zqDn(Lpo2{oJt$`d^L%1reG4W@oKM^B@1jzzUD~TVb*JgP^UmbloBGpP-t)6JeDzYw zB)Dk%!K*l$4_enB%5>_-X_P64IE+Af`#MfTIq%6?8s^H^(*sV!X$)HX%jNND`_Mh< zbXJG%^4>Apos-kms^Q$Yb?5$z!N+8r%1>%lB#_CJT`_n8pV2UDKKHUTktr*}?5p&I zeCRi#2-z>9kX-1H;pfwW@)Cu<6m+c72k_3x-{O5m3RnfK0!0NlKe#B2 zU4v_lDs`YzM*v_A-OAABvjqFN2D=8=8ZiPB+7+l>g*jpf?T&ue@VW-q8nrtKbNCQu zW?@b!Ld}l&uA-CZYP7Xgz$&n*KviG1c>jO&`}6;%lfAMESOxwo1w^gy^?O*7*;{uO x$9t`d@*ahad25Zz1(n&3RfD(UeH3LFbGZTR8eD5c56u1%P%_xcD)3ho_yvG0!>0fM literal 0 HcmV?d00001 diff --git a/c++/.DS_Store b/c++/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..c4e967db71f3bf7b6570b09a33f29f180798ef79 GIT binary patch literal 6148 zcmeHKO-lnY5Phi?Dg`NsAcC^Lpdg6nZEXcXz4QmD-AesfyQQs%-uCX<-{DWxH_5oz zc6(Kk8JN7uY~Cb!yEF{|*M1os0Sy2(x?p9CO^L~K@fE9RW|QcAjT0Q>8eNQ#tcc8CeS`c1}TjL*|7>8Dwre11HYZQ|R98si;YvgRXM z6FgvsIlqdOb)M-X_DidvMP@S;VFc9CBYT7)xpSPZVZAT0cF9V~OYo#SXt6&m)^g8P zl+N!CmMR@`rJ3KqtbInmQXS5?uZ(pr{Q6{`fsd!__U2xfYO`x*3YY?>z%MDlGh3`y z4`{6^U<#N5g#z;X5YYu=#XO+@>0slp0K_WO-WbbWLO79Pte6Mn6PgQDVxT&`Vz@wO zKGJcqVjeKi;q>z1bY`bF6z6AW{m2i8ivwC~3YY?m3T%XKL(c#1`uqPP$$F-MDe$ip zaMez))8>}+Y+c!$oV5}ChAt-K@_+?}fj^2lR*vF5x;N${35c;`9*`ND{SojoSYrzO Gr~;q)fM~)1 literal 0 HcmV?d00001 diff --git a/c++/Base64_Encoder_Decoder.cpp b/c++/Base64_Encoder_Decoder.cpp new file mode 100644 index 0000000..19281c8 --- /dev/null +++ b/c++/Base64_Encoder_Decoder.cpp @@ -0,0 +1,182 @@ +// __builtin_clz(x): the number of zeros at the beginning of the number +// __builtin_ctz(x): the number of zeros at the end of the number +// __builtin_popcount(x): the number of ones in the number +// __builtin_parity(x): the parity (even or odd) of the number of ones +// __builtin_ffs(int) finds the index of the first (most right) set bit +#include +using namespace std; +typedef long long int ll; +typedef long double ld; +typedef unsigned long long int ull; +typedef vector vi; +typedef vector vvi; +typedef vector vll; +typedef pair pii; +typedef pair pll; +typedef vector vpii; +typedef vector vpll; +#define repi(i,s,e) for(ll i=s;i=s;i--) +#define all(x) x.begin(),x.end() +#define begz(x) __builtin_clz(x) +#define endz(x) __builtin_ctz(x) +#define setbits(x) __builtin_popcount(x) +#define ffs(x) __builtin_ffs(x) +#define sz(x) x.size() +#define YES cout<<"YES\n" +#define NO cout<<"NO\n" +#define Yes cout<<"Yes\n" +#define No cout<<"No\n" +#define pb push_back +#define F first +#define S second +#define sp " " +#define nl "\n" +#define INF numeric_limits::infinity(); +const ll MOD = 1e9 + 7; +void input_output_path(); +class DSU{private:vll parent;vll rank;public:DSU(ll n){parent.resize(n);rank.resize(n,0);for(ll i=0;i rank[root_y])parent[root_y] = root_x;else{parent[root_y] = root_x;rank[root_x]++;}}}}; +string reverse_str(string str){string s="";for(ll i=str.size()-1 ; i>=0 ; i--)s+=str[i];return s;} +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);} +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;} +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;} +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;}} +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;} +ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);} +ll lcm(ll a, ll b){return (a / gcd(a, b)) * b;} +bool vpsort(const pair& a, const pair& b){if (a.first == b.first)return a.second < b.second;return a.first < b.first;} +bool isPowerOfTwo(ull n){return n && !(n & (n - 1));} +ll round_pow2(ll x){return pow(2,ceil(log(x)/log(2)));} +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;} + +string base64_encode(const string input) +{ + static const string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + string encoded; + + int i = 0, j = 0; + unsigned char char_array_3[3], char_array_4[4]; + + for( int k = 0; k < input.size(); k++) + { + char_array_3[i++] = input[k]; + if ( i == 3 ) + { + char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; + char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); + char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); + char_array_4[3] = char_array_3[2] & 0x3f; + + for ( int m = 0; m < 4; ++m ) + { + encoded += base64_chars[char_array_4[m]]; + } + i = 0; + } + } + if ( i > 0 ) + { + for ( int k = 8; k < 3; ++k ) + { + char_array_3[k] = '\0'; + } + char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; + char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); + char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); + + for (int k = 0; k < i + 1; ++k) + { + encoded += base64_chars[char_array_4[k]]; + } + + while (i++ < 3) + { + encoded += '='; + } + } + return encoded; +} + +string base64_decode(const string input) +{ + static const string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + string decoded; + + int i = 0, j = 0, in_ = 0; + unsigned char char_array_4[4], char_array_3[3]; + + for ( int k = 0; k < input.size(); ++k) + { + if ( input[k] == '=' ) + break; + if ( base64_chars.find( input[k] ) == string::npos ) + return "Invalid base64 character!"; + + char_array_4[i++] = input[k]; + if (i == 4) + { + for (int m = 0; m < 4; ++m) { + char_array_4[m] = base64_chars.find(char_array_4[m]); + } + + char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); + char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); + char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; + + for (int m = 0; m < 3; ++m) { + decoded += char_array_3[m]; + } + i = 0; + } + } + if (i > 0) + { + for ( int k = i; k < 4; ++k ) + char_array_4[k] = 0; + + for (int k = 0; k < 4; ++k) + char_array_4[k] = base64_chars.find(char_array_4[k]); + + char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); + char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); + + for (int k = 0; k < i - 1; ++k) + decoded += char_array_3[k]; + } + + return decoded; +} +void solve() +{ + cout<<"Choose mode:\n1. Encode to base64\n2. Decode from base64\n"; + int choice; + cin>>choice; + + string input; + cout<<"Enter the string: "; + cin>>input; + + string result; + switch(choice) + { + case 1: + result = base64_encode(input); + cout<<"Encode String: "<