-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into SSOC-2-ShriDharayacharya
- Loading branch information
Showing
90 changed files
with
7,211 additions
and
571 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include <iostream> | ||
#include <sstream> | ||
using namespace std; | ||
|
||
int calculateParityBits(int m, int low, int high) { | ||
//Binary algorithm to find required number of bits | ||
if (high == 0) { | ||
high = m + 1; | ||
} | ||
|
||
int r = (low + high) / 2; | ||
|
||
if (low == high) { | ||
return low; | ||
} else if ((1 << r) >= r + m + 1) { //#r = redundant bit, m = data bit | ||
return calculateParityBits(m, low, r); | ||
} else { | ||
return calculateParityBits(m, r + 1, high); | ||
} | ||
} | ||
|
||
int isPowerOfTwo(int num){ | ||
//Performs a bitwise AND operation between num and num - 1. | ||
//This operation turns off the rightmost set bit in num | ||
//(i.e., the least significant bit). | ||
|
||
//If the result of the bitwise AND operation is zero, it means that num | ||
//had only one set bit (power of 2) before the bitwise operation, and | ||
//that bit was turned off. In this case, the number is considered a power of 2 | ||
return (num & (num - 1)) == 0; | ||
} | ||
|
||
int calculateParityValue(string encoded_block, int block_size, int pos) { | ||
int count = 0; | ||
for (int i = 0; i < block_size; i++) { | ||
//If value of bit is '1' and corresponds to current parity | ||
if (encoded_block[i] == '1' && ((i + 1) & pos) != 0) { | ||
count++; | ||
} | ||
} | ||
return count % 2; | ||
} | ||
|
||
string hamming_encode(string& message) { | ||
int m = message.length(); // Length of the message | ||
int r = calculateParityBits(m,0,0); // Calculate number of parity bits | ||
|
||
int block_size = m + r; //Total size of the encoded block | ||
|
||
//Allocate memory for the encoded block | ||
string encoded_block(block_size,' '); | ||
|
||
int data_bit = 0; | ||
int numOnes=0; | ||
//Iterate over block and copy data | ||
for (int position = 1; position <= block_size; position++) { | ||
if (isPowerOfTwo(position)) { | ||
encoded_block[position - 1] = 'P'; //Mark as parity bit | ||
numOnes++; //Count number of ones | ||
} else { | ||
encoded_block[position - 1] = message[data_bit]; //Data bit, copy from the message | ||
data_bit++; | ||
} | ||
} | ||
for (int position = 1; position <= block_size; position++) { | ||
if (encoded_block[position - 1] == 'P') { | ||
// Calculate and assign the required parity value | ||
int parity = calculateParityValue(encoded_block, block_size, position); | ||
encoded_block[position - 1] = (parity % 2 == 0) ? '0' : '1'; | ||
} | ||
} | ||
|
||
encoded_block[block_size] = '\0'; // Null terminator | ||
|
||
return encoded_block; | ||
} | ||
|
||
int main(){ | ||
string message = "11110010010"; // Example message | ||
string encoded_message = hamming_encode(message); | ||
|
||
cout<<"Message entered:"<<message<<endl; | ||
cout<<"Encoded Message:"<<encoded_message<<endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
//AFFINE CIPHER | ||
//The Affine cipher is a standard substitution cipher. | ||
//Encryption: | ||
// Cipher Text, C=(aX+b) mod 26 | ||
// where a, b are key values and X is letter to be encrypted. | ||
//Decryption: | ||
// Plain Text, X=[a^-1(C-b)] mod 26 | ||
// where alphabets from (a-z) are numbered sequentially from (0-25). | ||
|
||
#include <iostream> | ||
#include <string> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
int a, b; //Key values for affine cipher | ||
int m = 26; | ||
|
||
string cipher(const string& plainText) { | ||
string ciphertext; | ||
for (size_t i = 0; i < plainText.length(); i++) { | ||
char t = plainText[i]; | ||
int alphaVal = t - 'a'; // 'a' has ASCII value 97 | ||
int c = (a * alphaVal + b) % m; | ||
char changedChar = c + 'a'; | ||
ciphertext += changedChar; | ||
} | ||
|
||
return ciphertext; | ||
} | ||
|
||
string decipher(const string& ciphertext) { | ||
string plaintext; | ||
int inverseA; | ||
for (int i = 1; i < m; i++) { | ||
if ((a * i) % m == 1) { | ||
inverseA = i; | ||
break; | ||
} | ||
} | ||
|
||
for (size_t i = 0; i < ciphertext.length(); i++) { | ||
char c = ciphertext[i]; | ||
int alphaVal = c - 'a'; | ||
int decipherT = (inverseA * (alphaVal - b + m)) % m; | ||
char changedChar = decipherT + 'a'; | ||
plaintext += changedChar; | ||
} | ||
|
||
return plaintext; | ||
} | ||
|
||
bool isCoprime(int a, int b) { | ||
int gcd = __gcd(a, b); | ||
return gcd == 1; | ||
} | ||
|
||
int modInverse(int a, int m) { | ||
a = a % m; | ||
for (int x = 1; x < m; x++) { | ||
if ((a * x) % m == 1) | ||
return x; | ||
} | ||
return -1; // Should not reach here if a and m are coprime | ||
} | ||
|
||
int main() { | ||
cout << "Enter plain text: "; | ||
string plainText; | ||
getline(cin, plainText); | ||
|
||
cout << "Enter keys a and b: "; | ||
cin >> a >> b; | ||
|
||
if (!isCoprime(a, m)) { | ||
cout << "Key 'a' and modulus 'm' are not coprime. Choose different keys.\n"; | ||
return 0; | ||
} | ||
|
||
string ciphertext = cipher(plainText); | ||
cout << "Cipher text:\n" << ciphertext << "\n"; | ||
|
||
string decryptedText = decipher(ciphertext); | ||
cout << "Decrypted text:\n" << decryptedText << "\n"; | ||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <cmath> | ||
#include <cstdlib> | ||
#include <ctime> | ||
|
||
using namespace std; | ||
|
||
int gcd(int a, int b) { | ||
if (a < b) | ||
return gcd(b, a); | ||
else if (a % b == 0) | ||
return b; | ||
else | ||
return gcd(b, a % b); | ||
} | ||
|
||
int gen_key(int q) { | ||
int key = rand_r() % q + pow(10, 20); | ||
while (gcd(q, key) != 1) { | ||
key = rand_r() % q + pow(10, 20); | ||
} | ||
return key; | ||
} | ||
|
||
int power(int a, int b, int c) { | ||
int x = 1; | ||
int y = a; | ||
|
||
while (b > 0) { | ||
if (b % 2 != 0) { | ||
x = (x * y) % c; | ||
} | ||
y = (y * y) % c; | ||
b = b / 2; | ||
} | ||
return x % c; | ||
} | ||
|
||
vector<int> encrypt(string msg, int q, int h, int g) { | ||
vector<int> en_msg; | ||
int k = gen_key(q); | ||
int s = power(h, k, q); | ||
int p = power(g, k, q); | ||
for (int i = 0; i < msg.length(); i++) { | ||
en_msg.push_back(msg[i]); | ||
} | ||
|
||
cout << "g^k used: " << p << endl; | ||
cout << "g^ak used: " << s << endl; | ||
|
||
for (int i = 0; i < en_msg.size(); i++) { | ||
en_msg[i] = s * en_msg[i]; | ||
} | ||
|
||
return en_msg; | ||
} | ||
|
||
string decrypt(vector<int> en_msg, int p, int key, int q) { | ||
string dr_msg; | ||
int h = power(p, key, q); | ||
for (int i = 0; i < en_msg.size(); i++) { | ||
dr_msg.push_back(en_msg[i] / h); | ||
} | ||
|
||
return dr_msg; | ||
} | ||
|
||
int main() { | ||
srand(time(0)); | ||
string msg; | ||
cout << "Enter the message: "; | ||
getline(cin, msg); | ||
cout << "Original Message: " << msg << endl; | ||
int q = rand_r() % static_cast<int>(pow(10, 50) - pow(10, 20) + 1) + pow(10, 20); | ||
int g = rand_r() % (q - 2) + 2; | ||
int key = gen_key(q); | ||
int h = power(g, key, q); | ||
cout << "g used: " << g << endl; | ||
cout << "g^a used: " << h << endl; | ||
vector<int> en_msg = encrypt(msg, q, h, g); | ||
string dr_msg = decrypt(en_msg, en_msg[0], key, q); | ||
cout << "Decrypted Message: " << dr_msg << endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <iostream> | ||
#include <unordered_map> | ||
using namespace std; | ||
|
||
unordered_map<char, string> morseCodeMap = { | ||
{'A', ".-"}, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."}, {'E', "."}, {'F', "..-."}, {'G', "--."}, {'H', "...."}, {'I', ".."}, | ||
{'J', ".---"}, {'K', "-.-"}, {'L', ".-.."}, {'M', "--"}, {'N', "-."}, {'O', "---"}, {'P', ".--."}, {'Q', "--.-"}, {'R', ".-."}, | ||
{'S', "..."}, {'T', "-"}, {'U', "..-"}, {'V', "...-"}, {'W', ".--"}, {'X', "-..-"}, {'Y', "-.--"}, {'Z', "--.."}, | ||
{'0', "-----"}, {'1', ".----"}, {'2', "..---"}, {'3', "...--"}, {'4', "....-"}, {'5', "....."}, {'6', "-...."}, | ||
{'7', "--..."}, {'8', "---.."}, {'9', "----."}, {' ', "/"} | ||
}; | ||
|
||
string convertToMorseCode(const string& text) { | ||
string morseCode; | ||
for (char ch : text) { | ||
char upperChar = toupper(ch); | ||
if (morseCodeMap.count(upperChar) > 0) { | ||
morseCode += morseCodeMap[upperChar]; | ||
morseCode += ' '; // Add space between characters | ||
} | ||
} | ||
return morseCode; | ||
} | ||
|
||
int main() { | ||
string text; | ||
cout << "Enter a text string: "; | ||
getline(cin, text); | ||
|
||
string morseCode = convertToMorseCode(text); | ||
cout << "Text: " << text << endl; | ||
cout << "Morse Code: " << morseCode << endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
Written by nuggetbucket54 | ||
========== COMPRESSION LOGIC ========== | ||
1. Start with an empty dictionary and an empty input buffer. | ||
2. Read symbols from the input data one at a time. | ||
3. Initialize a prefix as an empty string. | ||
4. Append the current symbol to the prefix. | ||
5. If the prefix is already in the dictionary, update the prefix by | ||
appending the current symbol and repeat step 5. | ||
6. If the prefix is not in the dictionary, add the prefix to the dictionary with a unique index | ||
and output the index of the previous prefix and the current symbol. | ||
7. Clear the prefix and repeat from step 4 until all symbols are processed. | ||
========== DECOMPRESSION LOGIC ========== | ||
During decompression, the algorithm uses the dictionary entries to reconstruct the original data. | ||
It reads the index and symbol pairs, retrieves the corresponding phrases from the dictionary, and | ||
outputs them to reconstruct the original data. | ||
*/ | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <map> | ||
using namespace std; | ||
|
||
vector <pair <int, char>> compress(string data) { | ||
map <string, int> storage; | ||
vector <pair <int, char>> compressed_data; | ||
string current_symbol = ""; | ||
int index = 1; | ||
|
||
for (char character: data) { | ||
current_symbol += character; | ||
if (storage.find(current_symbol) == storage.end()) { | ||
storage[current_symbol] = index; | ||
compressed_data.push_back(make_pair( | ||
storage[current_symbol.substr(0, current_symbol.length() - 1)], character | ||
)); | ||
index += 1; | ||
current_symbol = ""; | ||
} | ||
} | ||
|
||
return compressed_data; | ||
} | ||
|
||
string decompress(vector <pair <int, char>> data) { | ||
map <int, string> storage; | ||
string decompressed_data = ""; | ||
int index = 1; | ||
for (auto character: data) { | ||
storage[index] = storage[character.first] + character.second; | ||
decompressed_data += storage[index]; | ||
index += 1; | ||
} | ||
|
||
return decompressed_data; | ||
} | ||
|
||
|
||
int main() { | ||
string message = "ABBCBCABABCAABCAAB"; | ||
vector <pair <int, char>> compressed_message = compress(message); | ||
string decompressed_message = decompress(compressed_message); | ||
|
||
cout << "Original Message: " << message << endl; | ||
cout << "Compressed Message: "; | ||
for (auto element: compressed_message) { | ||
cout << "(" << element.first << ", " << element.second << "), "; | ||
} | ||
cout << endl; | ||
cout << "Decompresed Message: " << decompressed_message << endl; | ||
} |
Oops, something went wrong.