#include <iostream> #include <vector> #include <cmath> #include <string> using namespace std; // Função para calcular o inverso modular usando o Algoritmo de Euclides Estendido int modInverse(int a, int m) { int m0 = m, t, q; int x0 = 0, x1 = 1; if (m == 1) return 0; while (a > 1) { q = a / m; t = m; m = a % m, a = t; t = x0; x0 = x1 - q * x0; x1 = t; } if (x1 < 0) x1 += m0; return x1; } // Função para calcular a potência modular long long modExp(long long base, long long exp, long long mod) { long long result = 1; while (exp > 0) { if (exp % 2 == 1) result = (result * base) % mod; base = (base * base) % mod; exp /= 2; } return result; } // Função para mapear caractere para número baseado em A=11, Z=36, a=37, z=62 int charToNumber(char c) { if (c >= 'A' && c <= 'Z') return 11 + (c - 'A'); if (c >= 'a' && c <= 'z') return 37 + (c - 'a'); return 0; // Caso inválido } // Função para mapear número para caractere char numberToChar(int num) { if (num >= 11 && num <= 36) return 'A' + (num - 11); if (num >= 37 && num <= 62) return 'a' + (num - 37); return '?'; // Caso inválido } // Função para codificar uma string em um vetor de números (blocos para RSA) vector<int> encodeMessage(const string& message, int n) { vector<int> encodedBlocks; for (char c : message) { int num = charToNumber(c); if (!encodedBlocks.empty() && encodedBlocks.back() * 100 + num < n) { encodedBlocks.back() = encodedBlocks.back() * 100 + num; // Junta caracteres no mesmo bloco } else { encodedBlocks.push_back(num); // Começa um novo bloco } } return encodedBlocks; } // Função para decodificar um vetor de números de volta para string string decodeMessage(const vector<int>& decryptedBlocks) { string message = ""; for (int block : decryptedBlocks) { while (block > 0) { int num = block % 100; block /= 100; message = numberToChar(num) + message; } } return message; } int main() { int p, q, e; cout << "Digite dois números primos (p e q): "; cin >> p >> q; cout << "Digite o expoente de criptografia (e): "; cin >> e; int n = p * q; int phi = (p - 1) * (q - 1); int d = modInverse(e, phi); cout << "Chave Pública: (" << e << ", " << n << ")\n"; cout << "Chave Privada: (" << d << ", " << n << ")\n"; string inputMessage; cout << "Digite uma mensagem (apenas letras, ex: 'Acz'): "; cin >> inputMessage; vector<int> encodedBlocks = encodeMessage(inputMessage, n); vector<int> encryptedBlocks, decryptedBlocks; // Criptografia for (int block : encodedBlocks) encryptedBlocks.push_back(modExp(block, e, n)); // Descriptografia for (int block : encryptedBlocks) decryptedBlocks.push_back(modExp(block, d, n)); // Armazena a mensagem descriptografada antes de exibir a criptografada string decryptedMessage = decodeMessage(decryptedBlocks); // Exibe a mensagem descriptografada antes da criptografada cout << "Mensagem Descriptografada: " << decryptedMessage << "\n" << endl; // Agora exibe a mensagem criptografada cout << "Mensagem Criptografada: "; for (int block : encryptedBlocks) cout << block << " "; cout << "\n" << endl; return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}