Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Cifra de César em C (Criptografia)

Esse é um programa que criptografa um texto com ate 1000 caracteres (pode ser alterado no codigo fonte). O método de criptografia usado é a crifra de César uma das maneiras mais simples de criptografia.

Mais em: https://pt.wikipedia.org/wiki/Cifra_de_C%C3%A9sar

Pra criptografar uma mensagem crie um arquivo nescal.txt no diretório do programa com o texto a ser criptografado. Depois rode o programa no terminal com a chave. Ex: ./caesar 3

Source:
#include <stdio.h>

#include <stdlib.h>

#include <string.h>



void rot13(char *texto, int k);



int

main(int argc, char *argv[])

{



    if(argc != 2){

        printf("Use: ./caesar k \n");

        return 1;

    }



    FILE *fp = fopen("nescal.txt", "r");

    if(fp == NULL){

        printf("Erro ao abrir arquivo.");

        fclose(fp);

        return 2;

    }



    char texto[1000] = "";

    fread(texto, 1000, 1, fp);



    int k = atoi(argv[1]);

    k = k % 26;



    rot13(texto, k);

    printf("%s\n", texto);



    return 0;

}



void rot13(char *texto, int k)

{

//c i =(p i + k) % 26

    for(int i = 0; i < strlen(texto); i++){

    if(texto[i] >= 'a' && texto[i] <= 'z')

    texto[i] = 'a' + ( ((texto[i] - 'a') + k) % 26 );

    if(texto[i] >= 'A' && texto[i] <= 'Z')

    texto[i] = 'A' + ( ((texto[i] - 'A') + k) % 26 );

    }

}



This post first appeared on Salathiel++, please read the originial post: here

Share the post

Cifra de César em C (Criptografia)

×

Subscribe to Salathiel++

Get updates delivered right to your inbox!

Thank you for your subscription

×