C Program to Implement the Vigenere Cipher

This is a C Program to implement Vigenere Cipher. Implement a Vigenère cypher, both encryption and decryption. The program should handle keys and text of unequal length, and should capitalize everything and discard non-alphabetic characters. If your program handles non-alphabetic characters in another way, make a note of it.

Here is source code of the C Program to Implement the Vigenere Cypher. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. void upper_case(char *src) {
  7.     while (*src != '\0') {
  8.         if (islower(*src))
  9.             *src &= ~0x20;
  10.         src++;
  11.     }
  12. }
  13.  
  14. char* encipher(const char *src, char *key, int is_encode) {
  15.     int i, klen, slen;
  16.     char *dest;
  17.  
  18.     dest = strdup(src);
  19.     upper_case(dest);
  20.     upper_case(key);
  21.  
  22.     /* strip out non-letters */
  23.     for (i = 0, slen = 0; dest[slen] != '\0'; slen++)
  24.         if (isupper(dest[slen]))
  25.             dest[i++] = dest[slen];
  26.  
  27.     dest[slen = i] = '\0'; /* null pad it, make it safe to use */
  28.  
  29.     klen = strlen(key);
  30.     for (i = 0; i < slen; i++) {
  31.         if (!isupper(dest[i]))
  32.             continue;
  33.         dest[i] = 'A' + (is_encode ? dest[i] - 'A' + key[i % klen] - 'A'
  34.                 : dest[i] - key[i % klen] + 26) % 26;
  35.     }
  36.  
  37.     return dest;
  38. }
  39.  
  40. int main() {
  41.     const char *str = "Beware the Jabberwock, my son! The jaws that bite, "
  42.         "the claws that catch!";
  43.     const char *cod, *dec;
  44.     char key[] = "VIGENERECIPHER";
  45.  
  46.     printf("Text: %s\n", str);
  47.     printf("key:  %s\n", key);
  48.  
  49.     cod = encipher(str, key, 1);
  50.     printf("Code: %s\n", cod);
  51.     dec = encipher(cod, key, 0);
  52.     printf("Back: %s\n", dec);
  53.  
  54.     /* free(dec); free(cod); *//* nah */
  55.     return 0;
  56. }

Output:

$ gcc VigenereCipher.c
$ ./a.out
 
Text: Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
key:  VIGENERECIPHER
Code: WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
Back: BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.