#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> // Função para calcular o máximo divisor comum (MDC) int mdc(int a, int b) { while (b != 0) { int temp = b; b = a % b; a = temp; } return a; } // Função para calcular a potência modular (base^exp % mod) long long potencia_modular(long long base, long long exp, long long mod) { long long resultado = 1; base = base % mod; while (exp > 0) { if (exp % 2 == 1) { resultado = (resultado * base) % mod; } exp = exp >> 1; base = (base * base) % mod; } return resultado; } // Função para codificar uma string em números com base no pré-código void codificar_texto(const char *texto, int *codigo, int tamanho_bloco) { for (int i = 0; i < strlen(texto); i++) { if (texto[i] >= 'A' && texto[i] <= 'Z') { codigo[i] = 11 + (texto[i] - 'A'); } else if (texto[i] == ' ') { codigo[i] = 10; // Espaço = 10 } else { codigo[i] = 0; // Caracteres desconhecidos } } } // Função para calcular o inverso modular de e em relação a φ(n) int inverso_modular(int e, int phi) { int t = 0, new_t = 1, r = phi, new_r = e; while (new_r != 0) { int quociente = r / new_r; int temp = new_t; new_t = t - quociente * new_t; t = temp; temp = new_r; new_r = r - quociente * new_r; r = temp; } if (r > 1) return -1; // Não existe inverso modular if (t < 0) t += phi; return t; } int main() { // Variáveis para os números primos e o expoente público int p, q, e, tamanho_bloco; printf("Digite os numeros primos p e q: "); scanf("%d %d", &p, &q); printf("Digite o numero E (expoente publico): "); scanf("%d", &e); printf("Digite o tamanho do bloco: "); scanf("%d", &tamanho_bloco); // Calcula n, z (φ(n)) e verifica se e é válido int n = p * q; int phi = (p - 1) * (q - 1); if (mdc(e, phi) != 1) { printf("O numero E não e' coprimo com φ(n). Tente novamente.\n"); return 1; } // Calcula o expoente privado d int d = inverso_modular(e, phi); if (d == -1) { printf("Nao foi possível calcular o inverso modular.\n"); return 1; } printf("Chave publica: (n = %d, e = %d)\n", n, e); printf("Chave privada: (n = %d, d = %d)\n", n, d); // Entrada de texto para codificação char texto[256]; printf("Digite o texto para criptografar (A-Z e espaço): "); getchar(); // Limpa o buffer fgets(texto, 256, stdin); texto[strcspn(texto, "\n")] = '\0'; // Remove o \n do final // Codifica o texto int codigo[256] = {0}; codificar_texto(texto, codigo, tamanho_bloco); printf("\nTexto codificado: "); for (int i = 0; i < strlen(texto); i++) { printf("%d ", codigo[i]); } // Criptografia RSA printf("\nTexto criptografado: "); for (int i = 0; i < strlen(texto); i++) { long long cifra = potencia_modular(codigo[i], e, n); printf("%lld ", cifra); } printf("\n"); return 0; }
Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!
OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.
#include <stdio.h>
int main()
{
char name[50];
printf("Enter name:");
scanf("%s", name);
printf("Hello %s \n" , name );
return 0;
}
C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C 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);
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.
data-type array-name[size];
data-type array-name[size][size];
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.
Two types of functions are present in C
Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,
User defined functions are the ones which are written by the programmer based on the requirement.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
//code
}