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:

advertisement
$ 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.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.