Last active
December 2, 2016 20:04
-
-
Save kkrico/d569e475c2c877b3bc4e5cf2aae3f788 to your computer and use it in GitHub Desktop.
Versão final. Corrigido o bug ao gerar criptografia
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
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <ctype.h> | |
#define ASCIIDIFERENCA 65 | |
int modulo(int a, int b) | |
{ | |
int r = a % b; | |
return r < 0 ? r + b : r; | |
} | |
char* toUpperString(char* str) | |
{ | |
int i = 0; | |
char* res = (char*)malloc(strlen(str)+1); | |
while(str[i]) | |
{ | |
res[i] = toupper(str[i]); | |
i++; | |
} | |
return res; | |
} | |
bool isAsciiUpper(char letra) | |
{ | |
int asciiCode = (int)letra; | |
return (asciiCode >= 65 && asciiCode <= 90); | |
} | |
char* criptografar(char* originalMsg, int n) | |
{ | |
char* msg = toUpperString(originalMsg); | |
for(int i = 0; msg[i] != '\0'; i++) | |
{ | |
if (isAsciiUpper(msg[i])) | |
{ | |
int codigoAscii = (int)(msg[i]); | |
int posicao = codigoAscii - ASCIIDIFERENCA; | |
int res = modulo((posicao + n), 26); | |
msg[i] = (char)(res + ASCIIDIFERENCA); | |
} | |
else | |
{ | |
msg[i] = (char)msg[i]; | |
} | |
} | |
return msg; | |
} | |
char* descriptografar(char* originalMsg, int n) | |
{ | |
char* msg = toUpperString(originalMsg); | |
for(int i = 0; msg[i] != '\0'; i++) | |
{ | |
if (isAsciiUpper(msg[i])) | |
{ | |
int codigoAscii = (int)(msg[i]); | |
int posicao = codigoAscii - ASCIIDIFERENCA; | |
// Para a questão II, descomentar a linha abaixo: | |
// int res = modulo((posicao + n), 26); | |
int res = modulo((posicao - n), 26); | |
msg[i] = (char)(res + ASCIIDIFERENCA); | |
} | |
else | |
{ | |
msg[i] = (char)msg[i]; | |
} | |
} | |
return msg; | |
} | |
char* criptografiaDoDaniel(char* originalMsg, int n) | |
{ | |
char* msg = toUpperString(originalMsg); | |
for(int i = 0; msg[i] != '\0'; i++) | |
{ | |
if (isAsciiUpper(msg[i])) | |
{ | |
int codigoAscii = (int)(msg[i]); | |
int posicao = codigoAscii - ASCIIDIFERENCA; | |
int res = modulo((posicao + (n * n)), 26); | |
msg[i] = (char)(res + ASCIIDIFERENCA); | |
} | |
else | |
{ | |
msg[i] = (char)msg[i]; | |
} | |
} | |
return msg; | |
} | |
char* descriptografiaDoDaniel(char* originalMsg, int n) | |
{ | |
char* msg = toUpperString(originalMsg); | |
for(int i = 0; msg[i] != '\0'; i++) | |
{ | |
if (isAsciiUpper(msg[i])) | |
{ | |
int codigoAscii = (int)(msg[i]); | |
int posicao = codigoAscii - ASCIIDIFERENCA; | |
int res = modulo(((posicao - (n * n))), 26); | |
msg[i] = (char)(res + ASCIIDIFERENCA); | |
} | |
else | |
{ | |
msg[i] = (char)msg[i]; | |
} | |
} | |
return msg; | |
} | |
int main(void) | |
{ | |
char* source = "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"; | |
char* criptografado = criptografar(source, 3); | |
char* descriptografado = descriptografar(criptografado, 3); | |
printf("Original \t %s", source); | |
printf("\n"); | |
printf("Criptografado \t %s", criptografado); | |
printf("\n"); | |
printf("Descriptogra.. \t %s", descriptografado); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment