Thread: Vigenere

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Unhappy Vigenere


    Hello friends,

    I need a help for my programming. When I get the Key, I did nothing appears on the screen. :chomp:

    Thanks !

    Code:
    #define CL 10 // Key length 
    #define TX 110 //Word length
    #include<stdio.h>
    #include<string.h> //for strlen()
    
    int main()
    {
    char txt [TX]={0};
    char cle[CL]={0};
    char txtkrypt[TX]={0};
    
    int a,b,i,j,s; //variable of encryption
    
    
    
    char grille [26][26]= {"abcdefghijklmnopqrstuvwxyz"};
    
    printf("enter the text : ");
    scanf("%s",txt);
    
    printf("enter the key :");
    scanf("%s",cle);
    a=0;
    b=0;
    s=strlen(cle); 
    do{
    for(i=0;i<26;i++){ //incrementation of i
    if(txt[a]==grille[0][i]){ 
    for(j=0;j<26;j++){ //incrementation of j
    if(cle[b]==grille[j][0]) 
    txtkrypt[a]=grille[j][i]; 
                     }
                            }                        
                     }
    a++; //incrementation of a (word character )
    b++; //incrementation of b (character of key)
    if(b==s)b=0; 
    }
    while(txt[a]!='\0'); //as a txt is not the end
    printf("text crypt:\n%s\n\n\n\n",txtkrypt); 
    return 0;
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    Nobody will care about your post, since your code is all on one line.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Please reformat your code so that it isn't all on a single line.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    Done! Sorry....
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    Indented code should look something like this.
    Code:
    #define CL 10                   // Key length
    #define TX 110                  //Word length
    #include<stdio.h>
    #include<string.h>              //for strlen()
    
    int main()
    {
      char txt[TX] = { 0 };
      char cle[CL] = { 0 };
      char txtkrypt[TX] = { 0 };
    
      int a, b, i, j, s;            //variable of encryption
    
    
    
      char grille[26][26] = { "abcdefghijklmnopqrstuvwxyz" };
    
      printf("enter the text : ");
      scanf("%s", txt);
    
      printf("enter the key :");
      scanf("%s", cle);
      a = 0;
      b = 0;
      s = strlen(cle);
      do {
        for (i = 0; i < 26; i++) {  //incrementation of i
          if (txt[a] == grille[0][i]) {
            for (j = 0; j < 26; j++) {  //incrementation of j
              if (cle[b] == grille[j][0])
                txtkrypt[a] = grille[j][i];
            }
          }
        }
        a++;                        //incrementation of a (word character )
        b++;                        //incrementation of b (character of key)
        if (b == s)
          b = 0;
      }
      while (txt[a] != '\0');       //as a txt is not the end
      printf("text crypt:\n%s\n\n\n\n", txtkrypt);
      return 0;
    }
    Some problems.
    1. char grille [26][26]= {"abcdefghijklmnopqrstuvwxyz"};
    This only initialises the first ROW of your grille. All the other rows are full of \0.

    2. scanf("%s", txt);
    OK if all you input is "hello"
    But if you try to input "The quick brown fox", then you're going to be disappointed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Vigenere


    Thanks ! But how can I fix the problem ??

    I try without the second grille, but for me the second grille is necessary no ?

    Code:
    #define CL 10                   // Key length
    #define TX 110                  //Word length
    #include<stdio.h>
    #include<string.h>              //for strlen()
    
    int main()
    {
      char txt[TX] = { 0 };
      char cle[CL] = { 0 };
      char txtkrypt[TX] = { 0 };
    
      int a, b, i, j, s;            //variable of encryption
    
    
    
      char grille[26] = { "abcdefghijklmnopqrstuvwxyz" };
    
      printf("enter the text : ");
      scanf("%s", txt);
    
      printf("enter the key :");
      scanf("%s", cle);
      a = 0;
      b = 0;
      s = strlen(cle);
      do {
        for (i = 0; i < 26; i++) {  //incrementation of i
          if (txt[a] == grille[0]) {
            for (j = 0; j < 26; j++) {  //incrementation of j
              if (cle[b] == grille[j])
                txtkrypt[a] = grille[j];
            }
          }
        }
        a++;                        //incrementation of a (word character )
        b++;                        //incrementation of b (character of key)
        if (b == s)
          b = 0;
      }
      while (txt[a] != '\0');       //as a txt is not the end
      printf("text crypt:\n%s\n\n\n\n", txtkrypt);
      return 0;
    }
  12. #7
  13. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    > for (i = 0; i < 26; i++) { //incrementation of i
    > if (txt[a] == grille[0]) {
    If txt[a] == grille[0] is false the first time around, then doing it a further 25 times isn't going to help.

    http://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo