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.
#include<stdio.h>
char monocipher_encr(char);
char alpha[27][3] = { { 'a', 'f' }, { 'b', 'a' }, { 'c', 'g' }, { 'd', 'u' }, {
'e', 'n' }, { 'f', 'i' }, { 'g', 'j' }, { 'h', 'k' }, { 'i', 'l' }, {
'j', 'm' }, { 'k', 'o' }, { 'l', 'p' }, { 'm', 'q' }, { 'n', 'r' }, {
'o', 's' }, { 'p', 't' }, { 'q', 'v' }, { 'r', 'w' }, { 's', 'x' }, {
't', 'y' }, { 'u', 'z' }, { 'v', 'b' }, { 'w', 'c' }, { 'x', 'd' }, {
'y', 'e' }, { 'z', 'h' } };
char str[20];
int main() {
char str[20], str2[20];
int i;
printf("\n Enter String..");
gets(str);
for (i = 0; str[i]; i++) {
str2[i] = monocipher_encr(str[i]);
}
str2[i] = '\0';
printf("\n Before Decryption..%s", str);
printf("\n After Decryption..%s\n", str2);
}
char monocipher_encr(char a) {
int i;
for (i = 0; i < 27; i++) {
if (a == alpha[i][0])
break;
}
return alpha[i][1];
}
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.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Buy C Books
- Practice BCA MCQs
- Buy Computer Science Books
- Practice Computer Science MCQs
- Apply for Computer Science Internship