2

How can I use alphabetical order during encryption instead of ascii table numbers? For example, if I want to use one time pad encryption (text+key)modulo26, it seems to be taking ASCII number. I tried the following in C language:

encrypted[i] = (text[i]+key[i])%26 

But the cmd is showing me many question marks instead of a cipher text. Here is my complete code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define clear_buffer while(getchar()!='\n');

char* encrypt(char texte[],char cle[]){
    char *encrypted;
    int i=0;
    texte=texte-'a';
    cle=cle-'a';
    while(texte[i]!='\0' && cle[i]!='\0'){
        encrypted[i]='a'+(texte[i]+cle[i])%26;
        i++;
    }
    encrypted[i+1]='\0';
    return encrypted;   

    }
char* decrypt(char encrypted[],char cle[]){
    char *decrypted;
    int i=0;

    while(encrypted[i]!='\0' && cle[i]!='\0'){
        decrypted[i]=(encrypted[i]-cle[i])%26;
        i++;
    }
    decrypted[i+1]=0;
    return decrypted;

}
int main()
{
    char reponse,texte[100],cle[100],encrypted[100];
    int i=0;

    do{
        printf("Voulez vous crypter ou decrypter un texte?(Ecrire C pour crypter et D pour decrypter)\n");
        scanf("%c",&reponse);
    }while (reponse!='C'&& reponse!='D'&& reponse!='c'&& reponse!='d');//controle pour obliger l'utilisateur à donner c ou d
    if(reponse=='C'||reponse=='c'){
            clear_buffer;//vider le buffer apres le scanf de la reponse
        printf("Donner un texte a crypter\n");
        fgets(texte,100,stdin);
        while(texte[i]!=0)
            i++;
        if (i>0 && texte[i-1]!='\n')
            clear_buffer;
        printf("Donner une cle de meme taille\n");
        fgets(cle,100,stdin);
        i=0;
        while(cle[i]!=0)
            i++;
        if (i>0 && cle[i-1]!='\n')
            clear_buffer;
        printf("Le texte crypte est:%s\n",encrypt(texte,cle));

    }else{
            clear_buffer;//vider le buffer apres le scanf de la reponse
            //do{
        printf("Donner un texte (deja crypte) à decrypter\n");
        fgets(encrypted,100,stdin); 
        i=0;
        while(encrypted[i]!=0)
            i++;
        if (i>0 && encrypted[i-1]!='\n')
            clear_buffer;
        printf("Donner la cle (deja utilisee pour crypter\n");
        fgets(cle,100,stdin);
        i=0;
        while(cle[i]!=0)
            i++;
        if (i>0 && cle[i-1]!='\n')
            clear_buffer;
    //  }while(sizeof encrypted!=sizeof cle);
        printf("Le texte decrypte est:%s\n",decrypt(encrypted,cle));
    }
    system("pause");
    return 0;
}
Fuu
  • 3,424
  • 4
  • 33
  • 49
user105453
  • 21
  • 4

1 Answers1

2

Assuming that you are working only with lower case, you can subtract the ascii offset and convert the text and key to a value between 0-25 and then do the encryption process and add the offset at the end.

text = text - 'a';
key = key - 'a';
char encrypt = 'a' + ( text + key ) % 26

And also in your code you havent allocated memory for the 2 char pointers.

char *encrypted = (char*)malloc(100);

char *decrypted = (char*)malloc(100);
DollarAkshay
  • 2,063
  • 1
  • 21
  • 39