├── README.md ├── caesarCipher.cpp ├── eulerTotient.cpp └── playfairCipher.cpp /README.md: -------------------------------------------------------------------------------- 1 | # css - Cryptography and Security Systems 2 | -------------------------------------------------------------------------------- /caesarCipher.cpp: -------------------------------------------------------------------------------- 1 | #pragma GCC optimize("Ofast") 2 | #pragma GCC optimize("unroll-loops") 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long int ll; 9 | typedef unsigned long long int ull; 10 | typedef long double ld; 11 | 12 | typedef pair pll; 13 | typedef vector vll; 14 | typedef vector vpll; 15 | typedef map mll; 16 | typedef map mcl; 17 | 18 | #define inf 1<<30 19 | #define mx9 1000000007 20 | #define test(t) int t; cin >> t; while(t--) 21 | #define f(i, a, b) for(int (i) = (a); (i) < (b); ++(i)) 22 | #define all(a) (a).begin(), (a).end() 23 | #define endl "\n" 24 | #define ff first 25 | #define ss second 26 | #define pb push_back 27 | #define deb(x) cout << #x << ": " << x << endl; 28 | #define deb2(x,y) cout << #x << ": " << x << " ~ " << #y << ": " << y << endl; 29 | #define deba(arr) cout << #arr << " ~ [ "; for(auto n: arr) cout << n << " "; cout << "]" << endl; 30 | #define debap(arr) cout << #arr << " ~ [ "; for(auto n: arr) cout << n.first << "-" << n.second << " "; cout << "]" << endl; 31 | #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); 32 | 33 | void solve() 34 | { 35 | mcl mapChar; 36 | map cntMap; 37 | ll cnt = 0; 38 | for (char c = 'a'; c <= 'z'; c++) { 39 | mapChar[c] = cnt; 40 | cntMap[cnt] = c; 41 | cnt += 1; 42 | } 43 | 44 | // STARTS 45 | ll type; 46 | cin >> type; 47 | ll toBeEncrypted = 1; 48 | ll toBeDecrypted = 2; 49 | 50 | if(type == toBeEncrypted) { 51 | // Encryption 52 | string input; 53 | cin >> input; 54 | 55 | ll key; 56 | cin >> key; 57 | 58 | string decrypted = ""; 59 | ll encryptedLength = input.length(); 60 | f(i, 0, encryptedLength) { 61 | if(input[i] == ' ') 62 | continue; 63 | decrypted += (cntMap[(mapChar[input[i]] + key) % 26]); 64 | } 65 | cout << decrypted << endl; 66 | } 67 | else { 68 | // Decryption 69 | string input; 70 | cin >> input; 71 | 72 | ll key; 73 | cin >> key; 74 | 75 | string encrypted = ""; 76 | ll decryptedLength = input.length(); 77 | f(i, 0, decryptedLength) { 78 | if(input[i] == ' ') 79 | continue; 80 | ll curr = mapChar[input[i]] - key; 81 | if(curr < 0) { 82 | curr += 26; 83 | } 84 | encrypted += (cntMap[curr]); 85 | } 86 | cout << encrypted << endl; 87 | } 88 | cout << endl; 89 | } 90 | 91 | int main() 92 | { 93 | fast; 94 | #ifndef ONLINE_JUDGE 95 | freopen("input.txt", "r", stdin); 96 | freopen("output.txt", "w", stdout); 97 | #endif 98 | test(t) 99 | solve(); 100 | return 0; 101 | } 102 | 103 | /* 104 | 6 105 | 106 | 1 107 | abc 108 | 5 109 | 110 | 1 111 | abc 112 | 1 113 | 114 | 1 115 | tushar 116 | 8 117 | 118 | 2 119 | fgh 120 | 5 121 | 122 | 2 123 | bcd 124 | 1 125 | 126 | 2 127 | bcapiz 128 | 8 129 | */ 130 | 131 | /* 132 | fgh 133 | bcd 134 | bcapiz 135 | abc 136 | abc 137 | tushar 138 | */ -------------------------------------------------------------------------------- /eulerTotient.cpp: -------------------------------------------------------------------------------- 1 | #pragma GCC optimize("Ofast") 2 | #pragma GCC optimize("unroll-loops") 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long int ll; 9 | typedef unsigned long long int ull; 10 | typedef long double ld; 11 | 12 | typedef pair pll; 13 | typedef vector vll; 14 | typedef vector vpll; 15 | typedef map mll; 16 | typedef map mcl; 17 | 18 | #define inf 1<<30 19 | #define mx9 1000000007 20 | #define test(t) int t; cin >> t; while(t--) 21 | #define f(i, a, b) for(int (i) = (a); (i) < (b); ++(i)) 22 | #define all(a) (a).begin(), (a).end() 23 | #define endl "\n" 24 | #define ff first 25 | #define ss second 26 | #define pb push_back 27 | #define deb(x) cout << #x << ": " << x << endl; 28 | #define deb2(x,y) cout << #x << ": " << x << " ~ " << #y << ": " << y << endl; 29 | #define deba(arr) cout << #arr << " ~ [ "; for(auto n: arr) cout << n << " "; cout << "]" << endl; 30 | #define debap(arr) cout << #arr << " ~ [ "; for(auto n: arr) cout << n.first << "-" << n.second << " "; cout << "]" << endl; 31 | #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); 32 | 33 | bool isPrime(int n) 34 | { 35 | // Corner cases 36 | if (n <= 1) 37 | return false; 38 | if (n <= 3) 39 | return true; 40 | 41 | if (n % 2 == 0 || n % 3 == 0) 42 | return false; 43 | 44 | for (int i = 5; i * i <= n; i = i + 6) 45 | if (n % i == 0 || n % (i + 2) == 0) 46 | return false; 47 | 48 | return true; 49 | } 50 | 51 | ll formula(ll n) { 52 | if (n == 1) { 53 | return 1; 54 | } 55 | ll curr = n; 56 | mll mp; 57 | f(i, 2, sqrt(n) + 2) { 58 | if(isPrime(curr)) { 59 | mp[curr] += 1; 60 | curr = 1; 61 | } 62 | while(curr % i == 0) { 63 | curr /= i; 64 | mp[i] += 1; 65 | } 66 | if(curr == 1) { 67 | break; 68 | } 69 | } 70 | ld ans = n; 71 | for(auto it: mp) { 72 | ld abc = (ld)(1 - (ld)(1.0 / it.ff)); 73 | ans *= (abc); 74 | } 75 | return ans; 76 | } 77 | 78 | ll eulerTotient(ll n) { 79 | if(n == 1) { 80 | return n; 81 | } 82 | if(isPrime(n)) { 83 | return n - 1; 84 | } 85 | ll curr = n; 86 | mll mp; 87 | f(i, 2, sqrt(n) + 2) { 88 | if(isPrime(curr)) { 89 | mp[curr] += 1; 90 | curr = 1; 91 | } 92 | while(curr % i == 0) { 93 | curr /= i; 94 | mp[i] += 1; 95 | } 96 | if(curr == 1) { 97 | break; 98 | } 99 | } 100 | 101 | ll ans = 1; 102 | // debap(mp) 103 | for(auto it: mp) { 104 | f(i, 0, it.ss) 105 | ans *= eulerTotient(it.ff); 106 | } 107 | return ans; 108 | } 109 | 110 | void solve() 111 | { 112 | ll n; 113 | cin >> n; 114 | // deb(n) 115 | cout << formula(n) << endl; 116 | } 117 | 118 | int main() 119 | { 120 | fast; 121 | #ifndef ONLINE_JUDGE 122 | freopen("input.txt", "r", stdin); 123 | freopen("output.txt", "w", stdout); 124 | #endif 125 | test(t) 126 | solve(); 127 | return 0; 128 | } -------------------------------------------------------------------------------- /playfairCipher.cpp: -------------------------------------------------------------------------------- 1 | #pragma GCC optimize("Ofast") 2 | #pragma GCC optimize("unroll-loops") 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long int ll; 9 | typedef unsigned long long int ull; 10 | typedef long double ld; 11 | 12 | typedef pair pll; 13 | typedef vector vll; 14 | typedef vector vpll; 15 | typedef map mll; 16 | typedef map mcl; 17 | 18 | #define inf 1<<30 19 | #define mx9 1000000007 20 | #define test(t) int t; cin >> t; while(t--) 21 | #define f(i, a, b) for(int (i) = (a); (i) < (b); ++(i)) 22 | #define all(a) (a).begin(), (a).end() 23 | #define endl "\n" 24 | #define ff first 25 | #define ss second 26 | #define pb push_back 27 | #define deb(x) cout << #x << ": " << x << endl; 28 | #define deb2(x,y) cout << #x << ": " << x << " ~ " << #y << ": " << y << endl; 29 | #define deba(arr) cout << #arr << " ~ [ "; for(auto n: arr) cout << n << " "; cout << "]" << endl; 30 | #define debap(arr) cout << #arr << " ~ [ "; for(auto n: arr) cout << n.first << "-" << n.second << " "; cout << "]" << endl; 31 | #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); 32 | 33 | void solve() 34 | { 35 | string input; 36 | cin >> input; 37 | 38 | string key; 39 | cin >> key; 40 | 41 | // MATRIX 42 | char matrix[5][5]; 43 | vector matrixVector; 44 | 45 | mcl mapCharCnt; 46 | f(i, 0, key.length()) { 47 | char currChar = key[i]; 48 | if(currChar == 'j') { 49 | currChar = 'i'; 50 | } 51 | 52 | if (!mapCharCnt[currChar]) { 53 | matrixVector.pb(currChar); 54 | mapCharCnt[currChar] += 1; 55 | } 56 | } 57 | 58 | for(char c = 'a'; c <= 'z'; c++) { 59 | if(mapCharCnt[c] || c == 'j') { 60 | continue; 61 | } 62 | 63 | matrixVector.pb(c); 64 | mapCharCnt[c] += 1; 65 | } 66 | 67 | // deba(matrixVector) 68 | 69 | map> coordinates; 70 | 71 | ll k = 0; 72 | // cout << "~ matrix ~" << endl; 73 | f(i, 0, 5) { 74 | f(j, 0, 5) { 75 | matrix[i][j] = matrixVector[k++]; 76 | coordinates[matrix[i][j]] = {i, j}; 77 | // cout << matrix[i][j] << " "; 78 | } 79 | // cout << endl; 80 | } 81 | // cout << endl; 82 | 83 | // ENCRYPTION 84 | 85 | string bogusChar = "x"; 86 | ll inputLen = input.length(); 87 | f(i, 0, input.length() - 1) { 88 | string curr = input; 89 | if(input[i] == input[i + 1]) { 90 | input = curr.substr(0, i + 1); 91 | input += bogusChar; 92 | input += curr.substr(i + 1, inputLen); 93 | inputLen += 1; 94 | } 95 | } 96 | 97 | 98 | // Length check 99 | 100 | if(input.length() % 2) { 101 | input += bogusChar; 102 | } 103 | 104 | deb(input) 105 | 106 | string encrypted = ""; 107 | 108 | f(i, 0, input.length() - 1) { 109 | ll firstChar = input[i]; 110 | ll secChar = input[i + 1]; 111 | 112 | ll firstCharX = coordinates[firstChar].ff; 113 | ll firstCharY = coordinates[firstChar].ss; 114 | 115 | ll secCharX = coordinates[secChar].ff; 116 | ll secCharY = coordinates[secChar].ss; 117 | 118 | // deb2(firstCharX, firstCharY) 119 | // deb2(secCharX, secCharY) 120 | 121 | if(firstCharX == secCharX) { 122 | ll first = firstCharY + 1; 123 | ll sec = secCharY + 1; 124 | first %= 5; 125 | sec %= 5; 126 | encrypted += matrix[firstCharX][first]; 127 | encrypted += matrix[secCharX][sec]; 128 | } 129 | else if(firstCharY == secCharY) { 130 | ll first = firstCharX + 1; 131 | ll sec = secCharX + 1; 132 | first %= 5; 133 | sec %= 5; 134 | encrypted += matrix[first][firstCharY]; 135 | encrypted += matrix[sec][secCharY]; 136 | } 137 | else { 138 | encrypted += matrix[firstCharX][secCharY]; 139 | encrypted += matrix[secCharX][firstCharY]; 140 | } 141 | i += 1; 142 | } 143 | cout << "encrypted: " << encrypted << endl << endl; 144 | } 145 | 146 | int main() 147 | { 148 | fast; 149 | #ifndef ONLINE_JUDGE 150 | freopen("input.txt", "r", stdin); 151 | freopen("output.txt", "w", stdout); 152 | #endif 153 | test(t) 154 | solve(); 155 | return 0; 156 | } 157 | 158 | /* 159 | 160 | 3 161 | abc 162 | college 163 | hammer 164 | college 165 | tushar 166 | nankani 167 | 168 | */ --------------------------------------------------------------------------------