Vigenere

Description

cryptage decryptage

Source / Exemple :

////////////////////////////////programme de cryptage a la vigenere
#define  CL 10               //longeur de la clé
#define  TX 110             //longeur du texte a coder
#include<stdio.h>
#include<string.h>        //pour la fonction strlen()

main()
      {

	        char txt [TX]={0};
		char cle[CL]={0};                     
		char txtkrypt[TX]={0};
                char txtdekrypt[TX]={0};
                char txtadekrypt[TX]={0};	
                       
		int a,b,i,j,s;                     //variable de cryptage             
		
                int ax,bx,ix,jx,sx;              //variable de decryptage
                int ch;                         //choix du scanf
		
	        char grille [38][38] = {"abcdefghijklmnopqrstuvwxyz 0123456789",
                                        "bcdefghijklmnopqrstuvwxyz 123456789a",
                                  	"cdefghijklmnopqrstuvwxyz 0123456789bc",
                                      	"defghijklmnopqrstuvwxyz 0123456789abc",
                                        "efghijklmnopqrstuvwxyz 0123456789abcd",
                                       	"fghijklmnopqrstuvwxyz 0123456789abcde",
                                        "ghijklmnopqrstuvwxyz 0123456789abcdef",
                                     	"hijklmnopqrstuvwxyz 0123456789abcdefg",
                                       	"ijklmnopqrstuvwxyz 0123456789abcdefgh",
                                 	"jklmnopqrstuvwxyz 0123456789abcdefghi",
                                        "klmnopqrstuvwxyz 0123456789abcdefghij",
                                       	"lmnopqrstuvwxyz 0123456789abcdefghijk",
                                 	"mnopqrstuvwxyz 0123456789abcdefghijkl",
                                    	"nopqrstuvwxyz 0123456789abcdefghijklm",
                                  	"opqrstuvwxyz 0123456789abcdefghijklmn",
                                        "pqrstuvwxyz 0123456789abcdefghijklmno",
                                     	"qrstuvwxyz 0123456789abcdefghijklmnop",
                                    	"rstuvwxyz 0123456789abcdefghijklmnopq",
                                     	"stuvwxyz 0123456789abcdefghijklmnopqr",
                                       	"tuvwxyz 0123456789abcdefghijklmnopqrs",
                                       	"uvwxyz 0123456789bccdefghijklmnopqrst",
                                    	"vwxyz 0123456789abcdefghijklmnopqrstu",
                                     	"wxyz 0123456789abcdefghijklmnopqrstuv",
                                  	"xyz 0123456789abcdefghijklmnopqrstuvw",
                                       	"yz 0123456789abcdefghijklmnopqrstuvwx",
                                    	"yz 0123456789abcdefghijklmnopqrstuvwx",
                                        "z 0123456789abcdefghijklmnopqrstuvwxy",
                                 	" 0123456789abcdefghijklmnopqrstuvwxyz",
                                 	"0123456789abcdefghijklmnopqrstuvwxyz ",
                                     	"123456789abcdefghijklmnopqrstuvwxyz 0",
                                	"23456789abcdefghijklmnopqrstuvwxyz 01",
                                       	"3456789abcdefghijklmnopqrstuvwxyz 012",
                                  	"456789abcdefghijklmnopqrstuvwxyz 0123",
                                     	"56789abcdefghijklmnopqrstuvwxyz 01234",
                                       	"6789abcdefghijklmnopqrstuvwxyz 012345",
                                       	"789abcdefghijklmnopqrstuvwxyz 0123456",
                                     	"89abcdefghijklmnopqrstuvwxyz 01234567",
                                        "9abcdefghijklmnopqrstuvwxyz 012345678"};


      
                  printf("que voulez vous faire :\n\n-1  cryptage\n\n-2  decryptage\n\n\n\n");
                  scanf("%d",&ch);
                  switch (ch)
                            {
/////////////////////////////////////////cryptage0Oo°o0°///////////////////////////////////////////////////////////////////


                             case 1:       printf("CRYPTAGE\n\n");


                  printf("taper le texte a coder:\n\n\n\n");
                  scanf("%s",txt);                                           
	          while(getchar()!='\n');
		  printf("taper la clé :\n");
	          scanf("%s",cle);
                    

                              
                 


		  a=0;
		  b=0;

		  s=strlen(cle);                                            //longeur de la cle
		  do{
			  for(i=0;i<38;i++){                              //incrementation de i
				  if(txt[a]==grille[0][i]){              //si le caractere de txt egal un caractere de i
					  for(j=0;j<38;j++){            //incrementation de j 
					  if(cle[b]==grille[j][0])     //si le caractere de cle egal un caractere de j
						 txtkrypt[a]=grille[j][i];  //txt a coder =caractere en j et en i
					                   }                                         
				                          }      
			                   }
			  a++;                                                      //incrementation de a(caractere de txt)
			  b++;                                                     //incrementation de b(caractere de cle)
			  if(b==s)b=0;                                            //si b=longeur de la cle alors b=0
		    }                   
		  while(txt[a]!='\0');                                          //tant que txt n est pas a la fin
		  
	  	  printf("txtcodé:\n%s\n\n\n\n",txtkrypt);                    //affiche texte crypté


                              break;

//////////////////////////////////////////o0O°°0Oo°Decrypt4ge°/////////////////////////////////////////////////////////////

                              
                              case 2:         printf("DECRYPTAGE\n\n");


                  printf("taper la clé :\n");
                  scanf("%s",cle);
	          while(getchar()!='\n');
		  printf("taper le texte a decoder:\n");
	          scanf("%s",txtadekrypt);
                  

                 
                  ax=0;
                  bx=0;
                  sx=strlen(cle);

                  do {
                      for(jx=0;jx<38;jx++){
                                if(cle[bx]==grille[jx][0]){
                                          for(ix=0;ix<38;ix++){
                                             if(txtadekrypt[ax]==grille[jx][ix]){
                                                txtdekrypt[ax]=grille[0][ix];
                                                                     }
                                                              }
                                                          }
                                          }

                       ax++;
                       bx++;
                       if(bx==sx)bx=0;

                     }
                        while(txtadekrypt[ax]!='\0');
                        printf("DECRYPTAGE:\n\n\n\n%s\n\n\n",txtdekrypt);
                    	break;				   
		        default :        printf("nombre erroné.\n");
  }	
}

Conclusion :

bug : il ne prend pas en compte les espaces

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.