- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
-
- #define TAILLE 3000
- #define CRYPT 0
- #define DECRYPT 1
-
- void calcul_reste(unsigned long int puiss,int cod);
-
- char mess[TAILLE],decryptage[TAILLE];
- unsigned long int longmess,cryptage[TAILLE],N,Ea,Da;
-
- void main(void)
- {
- // saisie du message :
- printf("\ntaper le message :");
- gets(mess);
- longmess = strlen(mess);
-
- //saisie des clés de codage :
- printf("\non propose : N=105,Ea=29,Da=5 ou : N=56461,Ea=5389,Da=12209\n");
- printf("\nentrez N :");
- scanf("%ld",&N);
- printf("\nentrez EA :");
- scanf("%ld",&Ea);
- printf("\nentrez Da :");
- scanf("%ld",&Da);
-
- // cryptage :
- calcul_reste(Ea,CRYPT);
- printf("\nle message crypte est :\n");
- for(int i=0; i<longmess; i++)
- printf("%d ",cryptage[i]);
-
- // décryptage :
- calcul_reste(Da,DECRYPT);
- printf("\nle message decrypte est :\n");
- for(int i=0; i<longmess; i++)
- printf("%c",decryptage[i]);
-
- printf("\n\n");
- system("PAUSE"); // ne sert que sous le logiciel DEV-C++ afin d'avoir
- // le temps de voir ce qui s'affiche
- }
-
- void calcul_reste(unsigned long int puiss,int cod)
- {
- unsigned long int reste,tmp,data;
- for(int i=0; i<longmess; i++)
- {
- //cod=0 si l'on crypte et cod=1 si l'on decrypte
- if(cod==0)data=(unsigned long int)mess[i];
- if(cod==1)data=cryptage[i];
- tmp=1;
- for(int j=0; j<puiss; j++)
- {
- tmp = data * tmp;
- tmp = (unsigned long int)fmod (tmp, N);
- }
- reste=tmp;
- if(cod==0){cryptage[i]=reste;}
- if(cod==1){decryptage[i]=(char)reste;}
- }
- }
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define TAILLE 3000
#define CRYPT 0
#define DECRYPT 1
void calcul_reste(unsigned long int puiss,int cod);
char mess[TAILLE],decryptage[TAILLE];
unsigned long int longmess,cryptage[TAILLE],N,Ea,Da;
void main(void)
{
// saisie du message :
printf("\ntaper le message :");
gets(mess);
longmess = strlen(mess);
//saisie des clés de codage :
printf("\non propose : N=105,Ea=29,Da=5 ou : N=56461,Ea=5389,Da=12209\n");
printf("\nentrez N :");
scanf("%ld",&N);
printf("\nentrez EA :");
scanf("%ld",&Ea);
printf("\nentrez Da :");
scanf("%ld",&Da);
// cryptage :
calcul_reste(Ea,CRYPT);
printf("\nle message crypte est :\n");
for(int i=0; i<longmess; i++)
printf("%d ",cryptage[i]);
// décryptage :
calcul_reste(Da,DECRYPT);
printf("\nle message decrypte est :\n");
for(int i=0; i<longmess; i++)
printf("%c",decryptage[i]);
printf("\n\n");
system("PAUSE"); // ne sert que sous le logiciel DEV-C++ afin d'avoir
// le temps de voir ce qui s'affiche
}
void calcul_reste(unsigned long int puiss,int cod)
{
unsigned long int reste,tmp,data;
for(int i=0; i<longmess; i++)
{
//cod=0 si l'on crypte et cod=1 si l'on decrypte
if(cod==0)data=(unsigned long int)mess[i];
if(cod==1)data=cryptage[i];
tmp=1;
for(int j=0; j<puiss; j++)
{
tmp = data * tmp;
tmp = (unsigned long int)fmod (tmp, N);
}
reste=tmp;
if(cod==0){cryptage[i]=reste;}
if(cod==1){decryptage[i]=(char)reste;}
}
}