no save
Assistance
Achat
News

Forum | programmation
[C] programme vigenere [Cryptographie]
nabilmohcine, le jeu. 22 juin 2006 à 13:38:21
slt j'ai lu ton msg et tes site j'ai pris quelque chose merci bien voici mon programme
#include<dos.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
/*------------------------------------------------------------------*/
/*------------------INITIALISATION DES VARIABLES GLOBALES-----------*/
int c2=0; long t[50];long tab[100];char message[500]="";
long P,G,S,x; long alpha;
char nom_fich_txt[]="msg.txt",nom_fich_chif[]="msge.txt",nom_fich_dech[]="msge_dech.txt",nom_chif[]="fich_chiff.txt";
char message_chiffre[]="chiffr.txt";
/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
long nb_hasard(long u,long v)
{ long xf;
xf=random(v+1);
while(xf<=u)
{
xf=random(v+1);
}
return xf;
}
/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
long pgcd(long u,long v)
{
long r;
while(v>0)
{
r=u%v;
u=v;
v=r;
}
return u;
}
/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
long test_nb_premier(long n)
{
long d;
if(n%2==0) return (n==2);
for(d=3;d*d<=n;d=d+2)
if(n%d==0) return 0;
return 1;

}
/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
long nb_premier(long l)
{ long n; char s[25];
n=rand();
if(test_nb_premier(n)==1)
{ ltoa(n,s,10);
if (strlen(s)==l)
return n;
return nb_premier(l);
}
return nb_premier(l);
}
/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
long modulo(long a,long xf, long n) // a^x mod n
{
long s;
s=1;
while(xf)
{ if(xf&1) s=(s*a)%n;
xf>>=1;
a=(a*a)%n;
}
return s;
}
/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
int facteur_premier(long fact[],long n)
{
long d=3,xf;
int i=0,j;
while(n%2==0)
{ fact[i++]=2;
n=n/2;
}
while(d*d<=n)
{ if(n%d==0)
{ fact[i++]=d;
n=n/d;
}
else d=d+2;
}
if(n>1)
fact[i++]=n;
return i;

}
/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
int redondance(long fac[],int n) //fct qui elimine les elts redondants
{
int i=0,j,l;
while(i<(n-1))
{
j=i+1;
while(j<n)
{
if(fac[i]==fac[j])
{
for(l=j;l<(n-1);l++)
{
fac[l]=fac[l+1];
}
n--;
}
else j++;
}
i++ ;
}
return n;
}
/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
int generateur(long p)
{
long fac[50];
int j,n=facteur_premier(fac,p-1);
n=redondance(fac,n);
for(j=0;j<n;j++)
{
int i=0;
while((modulo(fac[j],(p-1)/fac[i],p)!=1)&&(i<n))
{
i++;
}
if(i==n) return fac[j]; //g est un generateur
}
return -1; //pas de generateur pour ce groupe
}
/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
void clefs_elgamal(long n)
{
P=nb_premier(n);
G=generateur(P);
S=nb_hasard(2,P-2);
while((pgcd(S,P-1))!=1)
{
S=nb_hasard(2,P-2);
}
x=modulo(G,S,P);
}
/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
void cree_fichier(char *nom_fich)
{
FILE *pf;
char string[500];
pf=fopen(nom_fich,"wt");
if(pf==NULL)
{
printf("Impossible d ouvrir le fichier");
exit(0);
}
printf("\t\n\nEntrer votre texte SVP :\n");
gets(string);
while(!fprintf(pf,"%s",string))
{
printf("Erreur dans l ecriture dans le fichier\n");
exit(1);
}
fclose(pf);
}
/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
void chaine_dans_fich(char *nom_fich,char *data)
{
FILE *pf;
char string[500];
pf=fopen(nom_fich,"wt");
if(pf==NULL)
{
printf("Impossible d ouvrir le fichier");
exit(0);
}
while(!fprintf(pf,"%s",data))
{
printf("Erreur dans l ecriture dans le fichier\n");
exit(1);
}
fclose(pf);
}
/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
void conversion_charchiffre(char *message_char,char *message_chiffre)
{
FILE *pft,*pfch;int c,a;char str[2],str2[2];
pft=fopen(message_char,"rt");
if(pft==NULL)
{
printf("Impossible d ouvrir le fichier");
exit(0);
}
pfch=fopen(message_chiffre,"wt");
if(pfch==NULL)
{
printf("Impossible d ouvrir le fichier");
exit(0);
}
while((c=fgetc(pft))!=EOF)
{
if(c==32) a=0;
else a=c-64;
itoa(a,str,10);
if(strlen(str)==1)
{
strcpy(str2,"0");
strcat(str2,str);
}
else strcpy(str2,str);
fprintf(pfch,"%s",str2);
}
fclose(pft);
fclose(pfch);
}
/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
void lire_fichier_txt(char *nom_fich)
{ c2++;
FILE *pf;
char string[500];
pf=fopen(nom_fich,"rt");
if(pf==NULL)
{
printf("Impossible d ouvrir le fichier");
exit(0);
}
while(!fgets( string,500,pf))
{
printf("Erreur dans l ecriture dans le fichier\n");
exit(1);
}
puts(string);
if(!feof(pf))
{
printf("Fin de fichier non atteinte\n");
exit(2);
}
fclose(pf);
}

/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
void messages_elementaires(char *nom_fichier,int n)
{
FILE *pf; char *mess;char me[50];
int c1,a;int i,j;
pf=fopen(nom_fichier,"r");
if(pf==NULL)
{
printf("Impossible d ouvrir le fichier");
exit(0);
}
c2=0;
while(!feof(pf))
{
c1=0;
while((c1<n)&&(a=fgetc(pf))!=EOF)
{
me[c1]=(char)a;
c1++;
}
if((c1<n)&&(fgetc(pf)==EOF))
{ int tmp=n-1;
while(c1!=0)
{
me[tmp]=me[c1-1];
tmp--;
c1--;
}
for(int h=0;h<=tmp;h++) me[h]='0';
}
t[c2]= atol(me);
c2++;
}
fclose(pf);
}
/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
int taille(long a)
{ int l; char s[25];
ltoa(a,s,10);
l=strlen(s);
return l;
}
/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
void reassemblage(long tf[],long n)
{
char * mess,tmp[10]; int m,i,j,k;
char zero[10]="";
for(i=0;i<c2;i++)
{ strcpy(zero,"");
m=taille(tf[i]) ;
if(m<n)
{
ltoa(tf[i],tmp,10);
for(k=0;k<n-m;k++) strcat(zero,"0");
strcat(zero,tmp);//
strcat(message,zero);
}
else
{
ltoa(tf[i],tmp,10);
strcat(message,tmp);
}
}
}
/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
void chiffre_elgamal(char *nom_fich_txt)
{
long k;
conversion_charchiffre(nom_fich_txt,nom_fich_chif);
messages_elementaires(nom_fich_chif,taille(P)-1);
k=nb_hasard(2, P-2);
alpha=modulo(G,k,P);
for(int i=0;i<=c2;i++)
tab[i]=modulo((t[i]*modulo(x,k,P)),1,P);
reassemblage(tab,taille(P));
chaine_dans_fich(message_chiffre,message);
}
/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
long inverse_modulaire(long a, long b)
{
long q,r,r1=a,r2=b,u,u1=1,u2=0,v,v1=0,v2=1;
do
{
q=r1/r2;
r=r1%r2;
u=u1-q*u2;
v=v1-q*v2;
r1=r2;
u1=u2;
v1=v2;
r2=r;
u2=u;
v2=v;
} while(r!=0);
return u1;
}
/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
void conversion_chiffrechar(char *message_dechi,char *message_chiffre)
{
FILE *pfa,*pfch;int c,cc,a,h;char str[2],string[500];
pfa=fopen(message_dechi,"w");
if(pfa==NULL)
{
printf("Impossible d ouvrir le fichier");
exit(0);
}
pfch=fopen(message_chiffre,"r");
if(pfch==NULL)
{
printf("Impossible d ouvrir le fichier");
exit(0);
}
while(fgets( string,500,pfch))
{
for(h=0;h<strlen(string);h++)
{
str[0]=string[h];
str[1]=string[++h];
c=atoi(str);
if(c==0) a=32;
else a=c+64;
fprintf(pfa,"%c",a);
}
}
fclose(pfa);
fclose(pfch);
}
/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
void dechiffre_elgamal(char *data)
{ long inv; int i;
messages_elementaires(message_chiffre,taille(P));
inv=inverse_modulaire((modulo(alpha,S,P)),P);
for( i=0;i<=c2;i++)
tab[i]=modulo((t[i]*modulo(inv,1,P)),1,P);
strcpy(message,"");
reassemblage(tab,taille(P)-1);
chaine_dans_fich(nom_chif,message);
conversion_chiffrechar(nom_fich_dech,nom_chif);
}

/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/*------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
void main()
{
textbackground(1);
clrscr();
textcolor(14);
for(int r=0;r<3;r++)
printf("\n");
printf("\n\n\n\n\n\t\t\t\t xxxxxxxxxx\n\n");delay(1000);
printf(" \n\n\n\t\t\t ********* CRYPTOGRAPHIE********"); delay(1000);
printf("\n \n\n\n\n\n\n\t\t * ALGORITHME ELGamal *\n \t\t ********************************************\n"); delay(1000);
printf("\n\n\n\n\n\n \t\t\t\t*sssssssssssssssss\n");delay(1000);
printf("\n \t\t\tnnnnnnnnnnnnnnnn") ;delay(1000);
int n;
clrscr();
cree_fichier(nom_fich_txt);
printf("\n\n\n");
printf("\n\nEntrer la longeur du nombre premier … utiliser : ");
scanf("%d",&n);
printf("\n\n\n\n");
clefs_elgamal(n);
printf("\t\t\t\t***********GENERATION DES CLEFS ELGamal*********\n");
printf("\t\t\t\tLa clef public Elgamal est : ( %ld, %ld, %ld)\n ", P,G,x);
printf("\t\t\t\tLa clef privee Elgamal est : ( %ld, %ld)\n ", P,S);
printf("\t\t\t\t **************************************\n");
printf("\t\t\t\t *************************\n\n\n\n\n");
printf("\n******************LE MESSAGE ORIGINE****************\n\n");
lire_fichier_txt(nom_fich_txt);
chiffre_elgamal(nom_fich_txt);
printf("\n*****************LE MESSAGE CONVERTIT***************\n\n");
lire_fichier_txt(nom_fich_chif);
printf("\n******************LE MESSAGE CHIFFRE*****************\n\n");
lire_fichier_txt(message_chiffre);
dechiffre_elgamal(message);
printf("\n*****************LE MESSAGE DECHIFFRE***************\n\n");
lire_fichier_txt(nom_fich_dech);
getch();
printf("\n************\n");
clrscr();for(int v=0;v<18;v++)printf("\n");
printf("\n\n\n\n\t VOUS REMERCIE D'AVOIR UTILISER LEUR PROGRAME");
printf("\n\n\n\t\t\t FIN DE PROGRAMME\n");
printf("\n ***********************************************\n");
printf(" ******************************\n");
printf(" ********************\n");

for( v=0;v<10;v++){printf("\n");delay(100);}delay(2000);
getch();
}
mon seul prblm que j'arrive pas à l'entrer dons un un autre programme si tu as des nouveaux envoier les moi et merci bien
nabil
Précédentleenders
janv. 02


REPONSES
leenders
janv. 02
nabilmohcine
juin 06
Version Web
Réalisé par RedShift
no save