Thread: Can you help me with my Vignere encryption algorithm?

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    1

    Can you help me with my Vignere encryption algorithm?

    Is it right or am I doing something wrong? If so, what? The goal is to encrypt a user given message using a key given as a command line argument:
    Code:
    // Vignere
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <typec.h>
    
    int main (int argc, char* argv[]){
    
    void encryptor (char k[], char string[2000]);
    
    int sizeofa = strlen(argv);
    int k[sizeofa] = argv[];
    char phrase2e[2000];
    
    if (argc != 2 || k < 0) {
    printf("give me ONLY one positive int\n");
    return 1;
    }
    else
    printf("please give me a phrase to encrypt\n");
    gets(phrase2e);
    encryptor(k, phrase2e);
    
    
    
    return 0;
    }
    
    
    void encryptor (char k[],char string[]) {
    
    for (int i = 0, n = strlen(string); i < n; i++)
    for (int j = 0, m = strlen(k); j < m; j++)
    {
            if (isalpha(string[i]) {
                  string[i] = string[i] + k[j];
                  printf("%c", string[i])
    }
            else {
                 printf("%c", string[i]);
                 j--;
    }
    
            if (j = m - 1) 
            j = 0;
    }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There are quite a few problems:
    • #include <typec.h> probably should have been #include <ctype.h>
    • strlen(argv) does not make sense. argv is conceptually an array of strings. Perhaps you wanted strlen(argv[1]) instead, but you should check the value of argc before accessing that.
    • This line is syntactically incorrect:
      Code:
      int k[sizeofa] = argv[];
      It looks like you don't even need it: you can check argc to determine if the user entered too many arguments, and you can pass argv[1] to encryptor.
    • You should not use gets because it is inherently vulnerable to buffer overflow. fgets is a possible alternative, though keep in mind that fgets retains the newline entered if there is space.
    • This looks like a common mistake:
      Code:
      if (j = m - 1)
      You probably intended to use == instead of = here
    • You need to indent your code properly. This is especially important in the encryptor function because it helps to show the reader the structure of the code, e.g.,
      Code:
      void encryptor(char k[], char string[]) {
       
      for (int i = 0, n = strlen(string); i < n; i++)
          for (int j = 0, m = strlen(k); j < m; j++)
          {
              if (isalpha(string[i]) {
                  string[i] = string[i] + k[j];
                  printf("%c", string[i])
              }
              else {
                  printf("%c", string[i]);
                  j--;
              }
      
              if (j = m - 1) 
                  j = 0;
          }
      }
      Incidentally, since you are not changing the contents of k, k should have been declared as const char k[] instead. Also, it would be better to name it key instead of k.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My Encryption Algorithm...
    By Junior89 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-26-2007, 03:53 PM
  2. encryption algorithm
    By jtullo in forum C Programming
    Replies: 3
    Last Post: 04-21-2007, 10:48 PM
  3. encryption algorithm
    By xddxogm3 in forum C++ Programming
    Replies: 3
    Last Post: 05-21-2005, 04:30 PM
  4. Encryption Algorithm...
    By gcn_zelda in forum C++ Programming
    Replies: 21
    Last Post: 01-02-2005, 11:00 PM
  5. abt encryption algorithm
    By purIn in forum C Programming
    Replies: 9
    Last Post: 12-22-2003, 10:16 PM

Tags for this Thread