Untitled
unknown
plain_text
10 months ago
4.0 kB
12
Indexable
#include <iostream>
#include <vector>
using namespace std;
const int alphabet_size=26;
int mod(int a,int m){
int t = 0, newT = 1;
int r = m, newR = a;
while(newR!=0){
}
}
const int alphabet_size = 26;
// Function to perform modulo inverse of a number mod m using the Extended Euclidean Algorithm
int modInverse(int a, int m) {
int t = 0, newT = 1;
int r = m, newR = a;
while (newR != 0) {
int quotient = r / newR;
t -= quotient * newT;
r -= quotient * newR;
swap(t, newT);
swap(r, newR);
}
if (r > 1) return -1; // No inverse exists
if (t < 0) t += m;
return t;
}
// Function to compute the determinant of a 2x2 matrix mod m
int determinantMod26(const vector<vector<int>>& matrix) {
return (matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]) % alphabet_size;
}
// Function to compute the inverse of a 2x2 matrix mod m
vector<vector<int>> inverseMatrix(const vector<vector<int>>& matrix) {
int det = determinantMod26(matrix);
int invDet = modInverse(det, alphabet_size);
if (invDet == -1) {
cout << "Matrix is not invertible!" << endl;
return {{-1, -1}, {-1, -1}};
}
vector<vector<int>> adjugate = {
{matrix[1][1], -matrix[0][1]},
{-matrix[1][0], matrix[0][0]}
};
// Take modulo 26 of the adjugate matrix and multiply by the inverse determinant
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
adjugate[i][j] = (adjugate[i][j] * invDet) % alphabet_size;
if (adjugate[i][j] < 0) adjugate[i][j] += alphabet_size;
}
}
return adjugate;
}
// Function to perform matrix multiplication mod 26
vector<int> multiplyMatrixVector(const vector<vector<int>>& matrix, const vector<int>& vec) {
vector<int> result(2);
for (int i = 0; i < 2; ++i) {
result[i] = (matrix[i][0] * vec[0] + matrix[i][1] * vec[1]) % alphabet_size;
}
return result;
}
// Function to decrypt the cipher text using the inverse key matrix
string decryptMessage(const vector<vector<int>>& keyMatrixInv, const string& ciphertext) {
string decryptedMessage = "";
for (int i = 0; i < ciphertext.size(); i += 2) {
// Take two letters from the ciphertext
vector<int> cipherBlock = {ciphertext[i] - 'A', ciphertext[i + 1] - 'A'};
// Multiply the inverse key matrix with the cipher block to get the decrypted block
vector<int> plainBlock = multiplyMatrixVector(keyMatrixInv, cipherBlock);
// Convert the numeric values back to letters
decryptedMessage += char(plainBlock[0] + 'A');
decryptedMessage += char(plainBlock[1] + 'A');
}
return decryptedMessage;
}
int main() {
// Known plaintext and ciphertext
string plaintext = "HI"; // Plaintext "HI" => [7, 8]
string ciphertext = "FN"; // Ciphertext "FN" => [5, 13]
// Convert plaintext and ciphertext to numeric vectors
vector<int> pt = {plaintext[0] - 'A', plaintext[1] - 'A'}; // [7, 8]
vector<int> ct = {ciphertext[0] - 'A', ciphertext[1] - 'A'}; // [5, 13]
// Set up the system of equations to find the key matrix K
vector<vector<int>> keyMatrix = {{pt[0], pt[1]}, {ct[0], ct[1]}};
// In this example, we have to compute the key matrix manually based on known plaintext-ciphertext pairs.
// The actual algorithm is simplified here. You should solve the system of linear congruences using modular arithmetic.
// Decrypt the message (FNUXCF)
string encryptedMessage = "FNUXCF";
vector<vector<int>> keyMatrixInv = inverseMatrix(keyMatrix);
string decryptedMessage = decryptMessage(keyMatrixInv, encryptedMessage);
cout << "Decrypted message: " << decryptedMessage << endl;
return 0;
}
Editor is loading...
Leave a Comment