Contact Learn C
Copy

Program 219: Vigenere Cipher using C

Program 219:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
main()
{
 int i,j,k,numstr[100],numkey[100],numcipher[100];
 char str[100],key[100];
 printf("Enter a string\n");
 gets(str);
 //converting entered string to Capital letters
 for(i=0,j=0;i<strlen(str);i++)
 {
  if(str[i]!=' ')
  {
   str[j]=toupper(str[i]);   
   j++;
  }
 }
 str[j]='\0';
 printf("Entered string is : %s \n",str);
 //Storing string in terms of ascii
 for(i=0;i<strlen(str);i++)
 {
  numstr[i]=str[i]-'A';
 }
 printf("Enter a key\n");
 gets(key);
    //converting entered key to Capital letters
 for(i=0,j=0;i<strlen(key);i++)
 {
  if(key[i]!=' ')
  {
   key[j]=toupper(key[i]);   
   j++;
  }
 }
 key[j]='\0';
     //Assigning key to the string
    for(i=0;i<strlen(str);)
    {
     for(j=0;(j<strlen(key))&&(i<strlen(str));j++)
     {
      numkey[i]=key[j]-'A';
      i++;
     }
     
    }
    
    for(i=0;i<strlen(str);i++)
    {
     numcipher[i]=numstr[i]+numkey[i];
    }
    for(i=0;i<strlen(str);i++)
    {
     if(numcipher[i]>25)
     {
      numcipher[i]=numcipher[i]-26;
     }
    }
    printf("Vigenere Cipher text is\n");   
    for(i=0;i<strlen(str);i++)
    {
      printf("%c",(numcipher[i]+'A')); 
    }
    
 printf("\n");
}
Explanation:
Pre Explanation:

Vigenere Cipher Explanation


//Coming Soon...

Output:

Vigenere Cipher

Vigenere Cipher

 

3 comments:

  1. I need to do these by seperating main function and encryption function. That's I have to define encryption part in other file. Can you help me pls?

    ReplyDelete
  2. wow great example but how does decryption work?

    ReplyDelete
  3. Watch and Download world's famous drama series Kurulus Osman in English on link below
    👇
    Kurulus Osman in English

    📢Get high quality backlinks for your
    Website with BacklinksIndexer
    BacklinksIndexer

    Crypto trading online course
    Join on link below
    Crypto quantum leap

    📒 Read Home doctor book online
    Then you will be a doctor for your family
    Home Doctor Book

    Join online YouTube course
    And be a professional YouTuber
    Tube Mastery and Monetization by matt

    🦷Steel Bite Pro
    Best product for
    Teeth pain, cavities,teeth whitening and other oral health issues with money back guarantee
    Steel Bite Pro

    ReplyDelete

Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide