C Program to Implement the Monoalphabetic Cipher

This is a C Program to implement Monoalphabetic Cipher. This program performs encryption and decryption using a monoalphabetic cipher. Only alphabetic characters are encrypted and case is ignored. Input is from stdin; Output is to stdout; Error is to stderr. One command line parameter is required of the form -dkey or -ekey where -e specifies encryption mode and -d specifies decryption mode. The key is always 26 alphabetic characters which specify mapping from abcd… to key.

Here is source code of the C Program to Implement the Monoalphabetic 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. char monocipher_encr(char);
  3. char alpha[27][3] = { { 'a', 'f' }, { 'b', 'a' }, { 'c', 'g' }, { 'd', 'u' }, {
  4.         'e', 'n' }, { 'f', 'i' }, { 'g', 'j' }, { 'h', 'k' }, { 'i', 'l' }, {
  5.         'j', 'm' }, { 'k', 'o' }, { 'l', 'p' }, { 'm', 'q' }, { 'n', 'r' }, {
  6.         'o', 's' }, { 'p', 't' }, { 'q', 'v' }, { 'r', 'w' }, { 's', 'x' }, {
  7.         't', 'y' }, { 'u', 'z' }, { 'v', 'b' }, { 'w', 'c' }, { 'x', 'd' }, {
  8.         'y', 'e' }, { 'z', 'h' } };
  9. char str[20];
  10. int main() {
  11.     char str[20], str2[20];
  12.     int i;
  13.     printf("\n Enter String..");
  14.     gets(str);
  15.     for (i = 0; str[i]; i++) {
  16.         str2[i] = monocipher_encr(str[i]);
  17.     }
  18.     str2[i] = '\0';
  19.     printf("\n Before Decryption..%s", str);
  20.     printf("\n After Decryption..%s\n", str2);
  21. }
  22. char monocipher_encr(char a) {
  23.     int i;
  24.     for (i = 0; i < 27; i++) {
  25.         if (a == alpha[i][0])
  26.             break;
  27.     }
  28.     return alpha[i][1];
  29. }

Output:

$ gcc MonoalphabeticCipher.c
$ ./a.out
 
 Enter String..dharmendrahingu
 Before Decryption..dharmendrahingu
 After Decryption..ukfwqnruwfklrjz

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

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

If you find any mistake above, kindly email to [email protected]

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 & discussions at Telegram SanfoundryClasses.