C Program to Implement Affine Cipher

«
»
This is a C Program to implement Affine Cipher. Affine Cipher is a type of Monoalphabetic Substitution Cipher, where each letter is mapped to its numeric value and it is encrypted using mathematical function and converted back to alphabets.
Each letter is enciphered with the function (ax+b)mod(26), where b is the magnitude of the shift.

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

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5.  
  6. main(int argc, char *argv[])
  7. {  
  8.    FILE *fin, *fout;
  9.    char c, A[80], B[80];
  10.    int  j,k,nc, a,b,s,t, x,y, ptr[26];
  11.  
  12.    sscanf(argv[1],"%d", &a); 
  13.    sscanf(argv[2],"%d", &b); 
  14.    fin =fopen(argv[3],"r"); 
  15.    /* fout=fopen(argv[4],"w"); */
  16.  
  17.    for (j=0; j<26; j++) ptr[j]=0;
  18.    ptr[ 1]= 1;  ptr[ 3]= 9;  ptr[ 5]=21;  ptr[ 7]=15;  
  19.    ptr[ 9]= 3;  ptr[11]=19;  ptr[15]= 7;  ptr[17]=23;
  20.    ptr[19]=11;  ptr[21]= 5;  ptr[23]=17;  ptr[25]=25;
  21.  
  22.    nc=0;  
  23.    fscanf(fin,"%c",&c); A[0]=c;
  24.    while (c != '\n') {fscanf(fin,"%c",&c); A[++nc]=c;}
  25.    /* A[++nc]=c; */
  26.    printf("y=%2d x + %d (mod 26)\n", a,b); 
  27.    printf("plaintext:  "); 
  28.    for (j=0; j<nc; j++) printf("%c",A[j]); printf("\n");
  29.  
  30.    if (a%2==0 || a==13 || a>25 || b>25) 
  31.      printf("(a,b)=(%2d,%2d) is invalid\n",a,b);
  32.    else {s=ptr[a]; t=(s*(26-b))%26;}
  33.  
  34.    for (k=0; k<nc; k++) {
  35.      x=(int) A[k] - (int) 'a';
  36.      y=(a*x+b)%26; 
  37.      B[k]=(char) (y+65);
  38.    }
  39.    printf("ciphertext: "); 
  40.    for (j=0; j<nc; j++) printf("%c",B[j]); printf("\n");
  41.  
  42.    printf("Decipher: y=%2dx + %2d\n",s,t);
  43.    for (k=0; k<nc; k++) {
  44.      y=(int) B[k] - (int) 'A';
  45.      x=(s*y+t)%26; 
  46.      A[k]=((x==23)? ' ' : (char) (x+97));
  47.    }
  48.    printf("decrypted message: "); 
  49.    for (j=0; j<nc; j++) printf("%c",A[j]); printf("\n");
  50.    printf("\n");
  51. }

Output:

$ gcc AffineCipher.c
$ ./a.out
 
Enter the message: SANFOUNDRY
Message is :SANFOUNDRY
Encrypted Message is : VTGIJBGCSN
Decrypted Message is: SANFOUNDRY

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.