Last active
November 11, 2015 17:18
-
-
Save Drowze/48a212d0eed9f0e23b00 to your computer and use it in GitHub Desktop.
Only simple letters for now
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> | |
void caesar_cipher(FILE *input, FILE *output, int k){ | |
int ch; | |
k %= 26; //26 indica uma volta completa, logo qualquer k>26 é excessivo | |
do{ | |
ch = fgetc(input); | |
if(ch >= 65 && ch <= 90) { //letra maiuscula? (valores de 'A' e 'Z' da tabela ASCII) | |
ch += k; | |
if(ch > 90) | |
ch -= 26; //se tiver passado do 'Z', subtrai uma volta completa | |
} | |
else | |
if(ch >= 97 && ch <= 122) { //letra minuscula? (valores de 'a' e 'z' da tabela ASCII) | |
ch += k; | |
if(ch > 122) | |
ch -= 26; //se tiver passado do 'z', subtrai uma volta completa | |
} | |
if(ch != EOF) | |
fputc(ch, output); | |
}while(ch != EOF); | |
} | |
void caesar_decipher(FILE *input, FILE *output, int k){ | |
int ch; | |
k %= 26; //26 indica uma volta completa, logo qualquer k>26 é excessivo | |
do{ | |
ch = fgetc(input); | |
if(ch >= 65 && ch <= 90) { //letra maiuscula? (valores de 'A' e 'Z' da tabela ASCII) | |
ch -= k; | |
if(ch < 65) | |
ch += 26; //se tiver passado do 'A', soma uma volta completa | |
} | |
else | |
if(ch >= 97 && ch <= 122) { //letra minuscula? (valores de 'a' e 'z' da tabela ASCII) | |
ch -= k; | |
if(ch < 97) | |
ch += 26; //se tiver passado do 'a', soma uma volta completa | |
} | |
if(ch != EOF) | |
fputc(ch, output); | |
}while(ch != EOF); | |
} | |
int main(){ | |
FILE *input = NULL; | |
FILE *output = NULL; | |
int k = 3; | |
input = fopen("teste.txt", "r"); | |
output = fopen("teste_out.txt", "w"); | |
if(input == NULL || output == NULL){ //se deu erro | |
printf("shit"); | |
return 1; | |
} | |
caesar_cipher(input, output, k); //encripta usando a chave k | |
//caesar_cipher(input, output, k); //decripta usando a chave k | |
fclose(input); | |
fclose(output); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment